int
でもオーバーフローしないし、for
で を受け取って、そのまま 出力するだけ。#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 0, a, b; i < n; ++i) {
cin >> a >> b;
cout << a + b << '\n';
}
return 0;
}
vector
の、前から 個をソートしたいときは、sort(A.begin(), A.begin() + K);
sort(A, A + K);
vector
でも、配列でも、sort(&A[0], &A[K]);
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<string> s(k);
for (int i = 0; i < k; ++i) cin >> s[i];
sort(s.begin(), s.end());
for (int i = 0; i < k; ++i) cout << s[i] << '\n';
return 0;
}
#include <bits/stdc++.h>
using namespace std;
// Union-Find
class UnionFind {
vector<int> p;
public:
UnionFind(int n) : p(n, -1) {}
// 同じグループにする
void unite(int x, int y) {
x = find(x); y = find(y);
if (x == y) return;
if (p[x] < p[y]) {
p[x] += p[y];
p[y] = x;
} else {
p[y] += p[x];
p[x] = y;
}
return;
}
int find(int x) {
if (p[x] < 0) return x;
while (p[p[x]] >= 0) {
p[x] = p[p[x]];
x = p[x];
if (p[x] < 0) return x;
}
return p[x];
}
// 同じグループかどうかを返す
bool isSame(int x, int y) { return find(x) == find(y); }
};
int main() {
int n, m;
int ans = 0;
cin >> n >> m;
UnionFind uni(n);
for (int i = 0, a, b; i < m; ++i) {
cin >> a >> b;
--a; --b;
if (uni.isSame(a, b)) ++ans;
else uni.unite(a, b);
}
cout << ans;
return 0;
}
Yes
、違うなら No
。累積和を前計算しておけば、各クエリ で求められる。#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n, k, q;
cin >> n >> k;
vector<ll> a(n + 1);
for (int i = 1; i <= n; ++i) cin >> a[i];
// k で割った余りごとの累積和
for (int i = k; i <= n; ++i) a[i] += a[i - k];
cin >> q;
for (int query = 0, l, r; query < q; ++query) {
cin >> l >> r;
--l;
// len は (r - i) - len <= l となるような最小の k の倍数
int len = ((r - l) + k - 1) / k * k;
ll sum = a[r] - a[max(0, r - len)];
bool flag = true;
for (int i = 1; i < k; ++i) {
len = (((r - i) - l) + k - 1) / k * k;
ll sum2 = a[r - i] - a[max(0, r - i - len)];
if (sum != sum2) {
flag = false;
break;
}
}
cout << (flag ? "Yes" : "No") << '\n';
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T>
class SparseTable {
const vector<T> &arr;
const int sz;
vector<vector<T>> table;
void solve() {
table[0].resize(sz);
for (int i = 0; i < sz; ++i) table[0][i] = arr[i];
for (int i = 1, tsz = table.size(); i < tsz; ++i) {
int range = 1 << i, tisz = sz + 1 - range;
table[i].resize(tisz);
range >>= 1;
for (int j = 0; j < tisz; ++j) {
table[i][j] = op(table[i - 1][j], table[i - 1][j + range]);
}
}
}
T op(T a, T b) { return a < b ? a : b; }
public:
SparseTable(const vector<T> &arr)
: arr(arr), sz(arr.size()), table(32 - __builtin_clz(sz)) {
solve();
}
T get(int L, int R) { // [L, R)
int lg = 31 - __builtin_clz(R - L);
return op(table[lg][L], table[lg][R - (1 << lg)]);
}
};
int main() {
int n, m;
const ll INF = 1ll << 60;
cin >> n >> m;
vector<ll> a(n + 1), c(n + 1);
c[0] = INF;
vector<bool> need(n + 1);
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) cin >> c[i];
for (int i = 0, x; i < n; ++i) cin >> x, need[x] = true;
SparseTable sp(c);
// 無理なら INF
vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, INF));
dp[0][0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + sp.get(i - j + 1, i + 1) + a[i]);
}
if (!need[i]) {
for (int j = 0; j < i; ++j) {
dp[i][j] = min(dp[i][j], dp[i - 1][j]);
}
}
}
ll ans = INF;
for (int i = m; i <= n; ++i) ans = min(ans, dp[n][i]);
cout << ans;
return 0;
}
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
int main() {
int n;
string x;
cin >> n >> x;
vector<mint> dp(n + 1);
mint sum = 1;
for (int i = 1; i <= n; ++i) {
dp[i] = dp[i - 1] * 10 + sum * (x[i - 1] - '0');
sum += dp[i];
}
cout << dp[n].val();
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> pow3(n + 1); // 3 の i 乗
pow3[0] = 1;
for (int i = 1; i <= n; ++i) pow3[i] = pow3[i - 1] * 3;
vector<int> a(pow3[n]);
for (int i = 0; i < pow3[n]; ++i) cin >> a[i];
// dp[i] は a から i 次元分を復元したもの
vector<vector<int>> dp(n + 1, vector<int>(pow3[n]));
dp[0] = a;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < pow3[n - 1]; ++j) {
int rem = j % pow3[i - 1], div = j / pow3[i - 1];
// i 桁目 (1-indexed) に 0, 1, 2 を挿入
int n0 = (div * 3 + 0) * pow3[i - 1] + rem;
int n1 = (div * 3 + 1) * pow3[i - 1] + rem;
int n2 = (div * 3 + 2) * pow3[i - 1] + rem;
dp[i][n0] = dp[i - 1][n1] - dp[i - 1][n2];
dp[i][n1] = dp[i - 1][n0] + dp[i - 1][n2] - dp[i - 1][n1];
dp[i][n2] = dp[i - 1][n1] - dp[i - 1][n0];
}
}
for (int i = 0; i < pow3[n]; ++i) cout << dp[n][i] << ' ';
return 0;
}