func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int list[n]; int i; for (i = 0; i < n; i++) cin >> list[i]; int total = accumulate(list, list + n, 0); n++; int count = 0; for (i = 1; i <= 5; i++) { if ((total + i) % n != 1) { count++; } } cout << count; }
#include <bits/stdc++.h> using namespace std; struct node { long long l; long long r; long long s; node* ln; node* rn; void change(long long n, long long k) { if (l == r) s += k; else { if (n <= (l + r) / 2) { if (ln == 0) { ln = new node; *ln = {l, (l + r) / 2, 0}; } ln->change(n, k); } else { if (rn == 0) { rn = new node; *rn = {(l + r) / 2 + 1, r, 0}; } rn->change(n, k); } s = 0; if (ln != 0) s += ln->s; if (rn != 0) s += rn->s; } } long long query(long long a, long long b) { if (a == l && b == r) return s; if (b < l || a > r) return 0; long long v = 0; if (ln != 0) v += ln->query(a, min(b, (l + r) / 2)); if (rn != 0) v += rn->query(max(a, (l + r) / 2 + 1), b); return v; } }; int main() { cin.sync_with_stdio(false); cin.tie(0); int n; cin >> n; unordered_map<int, node> m; for (int i = 0; i < n; i++) { long long a, b, c; cin >> a >> b >> c; if (!m.count(c)) m[c] = {1, 1 << 30, 0}; if (a == 1) m[c].change(b, 1); else if (a == 2) m[c].change(b, -1); else cout << m[c].query(1, b) << n ; } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long int x, y, k; cin >> x >> y >> k; long long int tot = y * k + k; if (x >= tot) { cout << 1 + k << endl; continue; } else if (x == 2) { cout << tot - 1 + k << endl; } else { if ((tot - x) % (x - 1) != 0) { cout << ((tot - x) / (x - 1)) + 2 + k << endl; } else { cout << ((tot - x) / (x - 1)) + 1 + k << endl; } } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, k, c[1001], i, x, a, b; int main() { cin >> n >> k; for (int x, i = 1; i <= n; ++i) { cin >> x; c[x]++; } for (i = 1; i <= k; ++i) { a = a + c[i] / 2 * 2; b = b + c[i] % 2; } cout << a + (b + 1) / 2; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e6; const int maxv = 4e5; const long long INF = 3e14 + 333; struct Node { int u, v, c; void in() { scanf( %d%d%d , &u, &v, &c); } } wo[maxn]; vector<int> G[maxv], st[maxv], ed[maxv]; int n, m; int dfn = 0; int d[maxv][2]; int wd[maxv]; void dfs(int v, int f) { d[v][0] = dfn; for (int i = 0; i < G[v].size(); i++) { int u = G[v][i]; if (u == f) continue; dfs(u, v); } for (int i = 0; i < st[v].size(); i++) wd[st[v][i]] = dfn++; d[v][1] = dfn; } unsigned long long dp[maxn]; unsigned long long val[maxn], add[maxn]; void push(int o, int l, int r) { if (r - l <= 1 || !add[o]) return; val[((o << 1))] += add[o]; val[((o << 1) | 1)] += add[o]; add[((o << 1))] += add[o]; add[((o << 1) | 1)] += add[o]; add[o] = 0; } void change(int o, int p, int l, int r, unsigned long long x) { push(o, l, r); if (r - l <= 1) { val[o] = x; return; } if (p < ((r + l) >> 1)) change(((o << 1)), p, l, ((r + l) >> 1), x); else change(((o << 1) | 1), p, ((r + l) >> 1), r, x); val[o] = min(val[((o << 1))], val[((o << 1) | 1)]); } void Add(int o, int a, int b, int l, int r, unsigned long long x) { if (a >= r || b <= l) return; push(o, l, r); if (l >= a && r <= b) { val[o] += x; add[o] += x; return; } Add(((o << 1)), a, b, l, ((r + l) >> 1), x); Add(((o << 1) | 1), a, b, ((r + l) >> 1), r, x); val[o] = min(val[((o << 1))], val[((o << 1) | 1)]); } unsigned long long rmq(int o, int a, int b, int l, int r) { if (a >= r || b <= l) return INF; push(o, l, r); if (l >= a && r <= b) return val[o]; return min(rmq(((o << 1)), a, b, l, ((r + l) >> 1)), rmq(((o << 1) | 1), a, b, ((r + l) >> 1), r)); } unsigned long long ans = 0; void DP(int v, int f) { unsigned long long sum = 0; for (int i = 0; i < G[v].size(); i++) { int u = G[v][i]; if (u == f) continue; DP(u, v); sum += dp[u]; } for (int i = 0; i < G[v].size(); i++) { int u = G[v][i]; if (u == f) continue; Add(1, d[u][0], d[u][1], 0, m, sum - dp[u]); } for (int i = 0; i < st[v].size(); i++) { int w = st[v][i]; change(1, wd[w], 0, m, sum + wo[w].c); } for (int i = 0; i < ed[v].size(); i++) { int w = ed[v][i]; change(1, wd[w], 0, m, INF); } if (v == 1) ans = sum; dp[v] = rmq(1, d[v][0], d[v][1], 0, m); } int main() { cin >> n >> m; for (int i = 0; i < n - 1; i++) { int x, y; scanf( %d%d , &x, &y); G[x].push_back(y); G[y].push_back(x); } for (int i = 0; i < m; i++) { wo[i].in(); st[wo[i].u].push_back(i); ed[wo[i].v].push_back(i); } for (int i = 0; i < m; i++) change(1, i, 0, m, INF); dfs(1, -1); DP(1, -1); if (ans >= INF) cout << -1 << endl; else cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; int pa = a, pb = b, pc = c, pd = d; a = pa; b = pb; c = pc; d = pd; vector<int> ans; if (a > 0) { ans.push_back(0); a--; while (a + b + c + d != 0) { if (ans.back() == 0) { if (b != 0) { b--; ans.push_back(1); } else { break; } } else if (ans.back() == 1) { if (a != 0) { ans.push_back(0); a--; } else if (c != 0) { ans.push_back(2); c--; } else { break; } } else if (ans.back() == 2) { if (d != 0) { ans.push_back(3); d--; } else if (b != 0) { ans.push_back(1); b--; } else { break; } } else { if (c != 0) { ans.push_back(2); c--; } else { break; } } } if (a + b + c + d == 0) { cout << YES << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i] << ; } return 0; } } ans.resize(0); a = pa; b = pb; c = pc; d = pd; if (b >= 1) { ans.push_back(1); b--; while (a + b + c + d != 0) { if (ans.back() == 0) { if (b != 0) { b--; ans.push_back(1); } else { break; } } else if (ans.back() == 1) { if (a != 0) { ans.push_back(0); a--; } else if (c != 0) { ans.push_back(2); c--; } else { break; } } else if (ans.back() == 2) { if (d != 0) { ans.push_back(3); d--; } else if (b != 0) { ans.push_back(1); b--; } else { break; } } else { if (c != 0) { ans.push_back(2); c--; } else { break; } } } if (a + b + c + d == 0) { cout << YES << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i] << ; } return 0; } } a = pa; b = pb; c = pc; d = pd; ans.resize(0); if (c >= 1) { ans.push_back(2); c--; while (a + b + c + d != 0) { if (ans.back() == 0) { if (b != 0) { b--; ans.push_back(1); } else { break; } } else if (ans.back() == 1) { if (a != 0) { ans.push_back(0); a--; } else if (c != 0) { ans.push_back(2); c--; } else { break; } } else if (ans.back() == 2) { if (d != 0) { ans.push_back(3); d--; } else if (b != 0) { ans.push_back(1); b--; } else { break; } } else { if (c != 0) { ans.push_back(2); c--; } else { break; } } } if (a + b + c + d == 0) { cout << YES << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i] << ; } return 0; } } a = pa; b = pb; c = pc; d = pd; ans.resize(0); if (d >= 1) { ans.push_back(3); d--; while (a + b + c + d != 0) { if (ans.back() == 0) { if (b != 0) { b--; ans.push_back(1); } else { break; } } else if (ans.back() == 1) { if (a != 0) { ans.push_back(0); a--; } else if (c != 0) { ans.push_back(2); c--; } else { break; } } else if (ans.back() == 2) { if (d != 0) { ans.push_back(3); d--; } else if (b != 0) { ans.push_back(1); b--; } else { break; } } else { if (c != 0) { ans.push_back(2); c--; } else { break; } } } if (a + b + c + d == 0) { cout << YES << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i] << ; } return 0; } } cout << NO ; return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-12; int main() { int n; double r, v, T; scanf( %d%lf%lf , &n, &r, &v); T = 2 * acos(-1) * r / v; for (int i = 0; i < n; i++) { double s, f, tim, tot = 0; scanf( %lf%lf , &s, &f); tim = (f - s) / v; tot = T * floor(tim / T); tim -= tot; double lef = 0.0, rig = T; for (int j = 0; j < 100; j++) { double t = (lef + rig) / 2.0; double g = t + (2.0 * r / v) * sin(v * t / (2.0 * r)); if (g - tim >= -eps) rig = t; else lef = t; } tot += lef; printf( %.12lf n , tot); } return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { int n, k; cin >> n >> k; int ans = n; if (k >= n) { cout << 1; } else { for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) { if (i <= k) { ans = min(ans, n / i); } if (n / i <= k) { ans = min(ans, i); } } } cout << ans; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int test; cin >> test; for (int i = 1; i <= test; i++) { solve(); cout << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int n; char c[1010]; int main() { scanf( %d , &n); scanf( %s , c + 1); c[0] = 0 , c[n + 1] = 0 ; if (n == 1) { if (c[1] == 0 ) return 0 * puts( No ); return 0 * puts( Yes ); } for (int i = 1; i < n; i++) { if (c[i] == 1 && c[i + 1] == 1 ) return 0 * puts( No ); } for (int i = 1; i <= n; i++) { if (c[i - 1] == 0 && c[i] == 0 && c[i + 1] == 0 ) return 0 * puts( No ); } puts( Yes ); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { int n, x; cin >> n >> x; int arr[n]; int o, e; o = e = 0; int i = 0; for (int j = 0; j < 10; j++) { } for (int j = 0; j < 10; j++) { } for (int j = 0; j < 10; j++) { } while (i < n) { int x; cin >> x; if (x % 2 == 0) { e++; } else o++; i++; } if (o == 0) cout << No << endl; else { bool pos = false; i = 1; while (i <= o) { if (i > x) break; if ((x - i) <= e) { pos = true; break; } i = i + 2; } if (!pos) cout << No << endl; else cout << Yes << endl; } } }
#include <bits/stdc++.h> using namespace std; int main() { int n, a, mx = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a; if (a > mx) mx = a; } if (mx - 25 > 0) cout << mx - 25 << endl; else cout << 0 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, dp[25][1100000], ans = 1e9; char num[25][100005]; int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) scanf( %s , num[i] + 1); for (int i = 1; i <= m; ++i) { int t = 0; for (int j = 1; j <= n; ++j) t = t * 2 + num[j][i] - 0 ; ++dp[0][t]; } for (int i = 1; i <= n; ++i) for (int j = 0; j < 1 << n; ++j) { for (int k = 1; k < 1 << n; k <<= 1) dp[i][j] = dp[i][j] + dp[i - 1][j ^ k]; if (i >= 2) dp[i][j] = dp[i][j] + (i - 2 - n) * dp[i - 2][j]; dp[i][j] /= i; } for (int i = 1; i < 1 << n; ++i) { int t = 0; for (int j = 0; j <= n; ++j) t += min(j, n - j) * dp[j][i]; ans = min(ans, t); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long c, w, v, a, l, pages, days = 1; cin >> c >> v >> w >> a >> l; pages = v; while (pages < c) { v += a; if (v > w) v = w; pages = pages - l + v; days++; } cout << days; }
#include <bits/stdc++.h> using namespace std; int n, k, x, y; struct node { int L, R, e; long long int Lsum, Rsum, Ls, Rs; int mid() { return (L + R) / 2; } } a[200005 * 4]; void BuildTree(int r, int L, int R) { a[r].L = L; a[r].R = R; a[r].e = 0; a[r].Lsum = 0; a[r].Rsum = 0; a[r].Ls = 0; a[r].Rs = 0; if (L == R) return; BuildTree(r << 1 | 1, L, a[r].mid()); BuildTree(r << 1, a[r].mid() + 1, R); a[r].Lsum = a[r << 1 | 1].Lsum + a[r << 1].Lsum; a[r].Rsum = a[r << 1 | 1].Rsum + a[r << 1].Rsum; a[r].Ls = a[r << 1 | 1].Ls + a[r << 1].Ls; a[r].Rs = a[r << 1 | 1].Rs + a[r << 1].Rs; } void Update(int r, int L, int v) { if (a[r].L == a[r].R && a[r].L == L) { int kk = a[r].e; a[r].e += v; if (a[r].e >= y && kk < y) { a[r].Lsum++; a[r].Ls -= kk; } if (a[r].e >= x && kk < x) { a[r].Rs -= kk; a[r].Rsum++; } if (a[r].e < y) a[r].Ls = a[r].e; if (a[r].e < x) a[r].Rs = a[r].e; return; } if (L > a[r].mid()) Update(r << 1, L, v); else Update(r << 1 | 1, L, v); a[r].Lsum = a[r << 1 | 1].Lsum + a[r << 1].Lsum; a[r].Rsum = a[r << 1 | 1].Rsum + a[r << 1].Rsum; a[r].Ls = a[r << 1 | 1].Ls + a[r << 1].Ls; a[r].Rs = a[r << 1 | 1].Rs + a[r << 1].Rs; } long long int Qurry(int r, int L, int R, int b) { if (L > R) return 0; if (a[r].L == L && a[r].R == R) { if (b == 1) return a[r].Lsum * y + a[r].Ls; else if (b == 2) return a[r].Rsum * x + a[r].Rs; } if (L > a[r].mid()) return Qurry(r << 1, L, R, b); else if (R <= a[r].mid()) return Qurry(r << 1 | 1, L, R, b); else { long long a1 = Qurry(r << 1 | 1, L, a[r].mid(), b); long long a2 = Qurry(r << 1, a[r].mid() + 1, R, b); return a1 + a2; } } int main() { int q; while (scanf( %d , &n) != EOF) { memset(a, 0, sizeof(0)); int u, v; BuildTree(1, 1, n); scanf( %d %d %d %d , &k, &x, &y, &q); while (q--) { int kk; scanf( %d , &kk); if (kk == 1) { scanf( %d %d , &u, &v); Update(1, u, v); } else { scanf( %d , &u); long long int sum1 = Qurry(1, 1, u - 1, 1); long long int sum2 = Qurry(1, u + k, n, 2); printf( %lld n , sum1 + sum2); } } } return 0; }
#include <bits/stdc++.h> using namespace std; const long long int MOD = 1000000007; const long long BIG = 1446803456761533460LL; const int Big = 336860180; const long long int INF = LONG_LONG_MAX; const long long int adj4[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; const long long int adj8[8][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}}; long long int clk_ar[10]; long long int gcd(long long int a, long long int b) { return b == 0 ? a : gcd(b, a % b); } long long int powMod(long long int a, long long int b) { long long int n = 1; long long int p = a; while (b > 0) { if (b % 2 == 1) { n *= p; n %= MOD; } p *= p; p %= MOD; b /= 2; } return n; } long long int modularInverse(long long int a) { return powMod(a, MOD - 2); } stringstream sss; const long long int maxn = 150010; const long long int maxm = 310; long long int dp[2][maxn]; long long int ts[maxm]; long long int ai[maxm]; void MAIN() { long long int n, m, V; cin >> n >> m >> V; long long int sb = 0; for (long long int i = 0; i < (m); ++i) { long long int b; cin >> ai[i + 1] >> b >> ts[i + 1]; --ai[i + 1]; sb += b; } for (long long int i = (1); i < (m + 1); ++i) { long long int d = (ts[i] - ts[i - 1]) * V; deque<long long int> q; for (long long int j = 0; j < (min(d, n)); ++j) { while (!q.empty() && dp[i & 1 ^ 1][j] >= dp[i & 1 ^ 1][q.back()]) { q.pop_back(); } q.push_back(j); } for (long long int j = 0; j < (n); ++j) { if (j + d < n) { while (!q.empty() && dp[i & 1 ^ 1][j + d] >= dp[i & 1 ^ 1][q.back()]) { q.pop_back(); } q.push_back(j + d); } dp[i & 1][j] = dp[i & 1 ^ 1][q.front()] - abs(j - ai[i]); if (q.front() == j - d) { q.pop_front(); } } } cout << (sb + *max_element(dp[m & 1], dp[m & 1] + n)) << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout << fixed << setprecision(10); sss << R ( 50 3 1 49 1 1 26 1 4 6 1 10 ) ; MAIN(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf( %d , &n); int* x = new int[2 * n]; int odd = 0; int even = n; int len = n - 1; for (int len = n - 1, i = 1; len > 0; len--, i++) if (i % 2 == 1) { x[odd] = i; x[odd + len] = i; odd++; } else { x[even] = i; x[even + len] = i; even++; } x[2 * n - 1] = n; if (n % 2 == 0) x[even] = n; else x[odd] = n; int s = 0; for (int i = 0; i < 2 * n; i++) printf( %d , x[i]); return 0; }
#include <bits/stdc++.h> using namespace std; bool issorted(long long int *arr, int n) { int flag = 0; for (int i = 1; i < n; i++) { if (arr[i - 1] > arr[i]) { flag = 1; break; } } if (flag == 0) return true; flag = 0; for (int i = n - 2; i >= 0; i--) { if (arr[i + 1] > arr[i]) { flag = 1; break; } } if (flag == 0) return true; return false; } int main(void) { int n; cin >> n; long long int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } long long int flag = 0; for (int i = 1; i < n; i++) { if (arr[i] != arr[i - 1]) { flag = 1; break; } } if (flag == 0 || n <= 2) cout << -1 << endl; else if (n == 3 && arr[0] == arr[2]) cout << -1 << endl; else { flag = 0; for (int i = 1; i < n; i++) { if (arr[i] != arr[i - 1]) { long long int swap = arr[i]; arr[i] = arr[i - 1]; arr[i - 1] = swap; if (!issorted(arr, n)) { cout << i + 1 << << i << endl; break; } else { swap = arr[i]; arr[i] = arr[i - 1]; arr[i - 1] = swap; } } } } }
#include <bits/stdc++.h> using namespace std; int MDA[10000010]; int MDB[10000010]; int main() { long long a, b, m; int i; cin >> a >> b >> m; for (i = 0; i < (min(m, a + 1)); i++) { MDA[i] = (i * 1000000000) % m; } for (i = 0; i < (min(m, b + 1)); i++) { MDB[(i) % m] = 1; } int ans = 2, mov; for (i = 0; i < (min(m, a + 1)); i++) { if (MDB[(m - MDA[i]) % m] == 0) { ans = 1; mov = i; break; } } cout << ans; if (ans == 1) { cout << ; char str[100]; sprintf(str, %ld , mov); string s = str; while (s.size() != 9) s = 0 + s; cout << s; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; int main() { int n, k; cin >> n >> k; if (k / n > 2) cout << 0 n ; else cout << n - (k % n) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; int num[maxn]; map<int, int> mp; int sum[maxn]; int main() { int n, i, j, k; cin >> n; for (i = 1; i <= n; i++) cin >> num[i], sum[i] = sum[i - 1] + num[i]; int s = 0; long long ans = 0; for (i = 1; i < n; i++) { int tmp = sum[n] - sum[i]; if (sum[i] == 2 * tmp) { ans += mp[tmp]; } mp[sum[i]]++; } printf( %I64d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const long long int MOD = 1e9 + 7; int a[100001], n, num, siz = 0, sq; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; vector<int> kill(n, 1); stack<int> s; s.push(0); kill[0] = 1000000000; for (int i = 1; i < n; i++) { while (!s.empty() && a[i] > a[s.top()]) { kill[i] = max(kill[s.top()] + 1, kill[i]); s.pop(); } s.push(i); } num = 0; for (int i = 0; i < n; i++) if (kill[i] < 1000000000) num = max(num, kill[i]); cout << num; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; int t, n, m, sum; int fa[1000010], sz[1000010]; vector<int> vec[1000010]; int find(int x) { if (x == fa[x]) return x; else return fa[x] = find(fa[x]); } int main() { ios_base::sync_with_stdio(false); t = 1; while (t--) { cin >> n >> m; for (int i = 1; i <= m; ++i) { vec[i].clear(); sz[i] = 0; fa[n + i] = n + i; } for (int i = 1; i <= n; ++i) { int x; cin >> x; sz[x + n]++; vec[x + n].push_back(i); fa[i] = x + n; } sum = 0; for (int i = 2; i <= n; ++i) { if (find(i) == find(i - 1)) { sum++; } } cout << n - sum - 1 << n ; for (int i = 1; i <= m - 1; ++i) { int x, y; cin >> x >> y; int fx = find(x + n), fy = find(y + n); if (sz[fx] < sz[fy]) { for (int j = 0; j < vec[fx].size(); ++j) { if ((vec[fx][j] > 1 && find(vec[fx][j] - 1) == fy)) sum++; if ((vec[fx][j] < n && find(vec[fx][j] + 1) == fy)) sum++; vec[fy].push_back(vec[fx][j]); sz[fy]++; } sz[fx] = 0; fa[fx] = fy; } else { for (int j = 0; j < vec[fy].size(); ++j) { if ((vec[fy][j] > 1 && find(vec[fy][j] - 1) == fx)) sum++; if ((vec[fy][j] < n && find(vec[fy][j] + 1) == fx)) sum++; vec[fx].push_back(vec[fy][j]); sz[fx]++; } sz[fy] = 0; fa[fy] = fx; } cout << n - sum - 1 << n ; } } return 0; }
#include <bits/stdc++.h> using std ::vector; template <typename Tp> Tp Max(const Tp &a, const Tp &b) { return a > b ? a : b; } template <typename Tp> Tp Min(const Tp &a, const Tp &b) { return a < b ? a : b; } template <typename Tp> Tp Abs(const Tp &a) { return a > 0 ? a : -a; } template <typename Tp> void Read(Tp &x) { Tp in = 0, f = 1; char ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) { in = in * 10 + ch - 0 ; ch = getchar(); } x = in * f; } const int SN = 200000 + 10; vector<int> G[SN]; int a[SN], b[SN], c[SN], min[SN], n; int res0[SN], res1[SN]; long long ans; void pre(int u, int fa) { for (auto v : G[u]) if (v != fa) min[v] = Min(min[v], min[u]); for (auto v : G[u]) if (v != fa) pre(v, u); } void Dfs(int u, int fa) { if (b[u] != c[u]) if (b[u] == 0) res0[u]++; else res1[u]++; for (auto v : G[u]) if (v != fa) Dfs(v, u), res1[u] += res1[v], res0[u] += res0[v]; int K = Min(res0[u], res1[u]); ans += 2LL * K * min[u]; res0[u] -= K, res1[u] -= K; } int main(int argc, const char *argv[]) { Read(n); for (int i = 1; i <= n; i++) Read(a[i]), Read(b[i]), Read(c[i]), min[i] = a[i]; int u, v; for (int i = 1; i < n; i++) { Read(u), Read(v); G[u].push_back(v); G[v].push_back(u); } u = 0, v = 0; for (int i = 1; i <= n; i++) { if (b[i] == 0) u++; if (c[i] == 0) v++; } if (u != v) { printf( -1 n ); return 0; } pre(1, 0); Dfs(1, 0); printf( %lld n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int MAXN = (int)3e5 + 3; const int infint = (int)1e9 + 3; const long long inf = (long long)2e18; struct node { int minf, tedf, mimm, tmimm, mipm, tmipm, min, max, lazymax, lazymin; node() { minf = infint, tedf = 0; mimm = infint, tmimm = 0; mipm = infint, tmipm = 0; min = infint, max = -infint; lazymax = -1, lazymin = -1; } } seg[4 * MAXN]; void updmax(int node, int st, int en, int x) { seg[node].max = x; seg[node].mipm = st + seg[node].max, seg[node].tmipm = 1; seg[node].minf = seg[node].mimm + seg[node].max, seg[node].tedf = seg[node].tmimm; seg[node].lazymax = x; return; } void updmin(int node, int st, int en, int x) { seg[node].min = x; seg[node].mimm = st - seg[node].min, seg[node].tmimm = 1; seg[node].minf = seg[node].mipm - seg[node].min, seg[node].tedf = seg[node].tmipm; seg[node].lazymin = x; } void shift(int node, int st, int en) { int mid = (st + en) >> 1; if (seg[node].lazymax != -1) updmax(node << 1, st, mid, seg[node].lazymax), updmax(node << 1 | 1, mid, en, seg[node].lazymax); if (seg[node].lazymin != -1) updmin(node << 1, st, mid, seg[node].lazymin), updmin(node << 1 | 1, mid, en, seg[node].lazymin); seg[node].lazymax = seg[node].lazymin = -1; } node merge(node a, node b) { node c; c.min = min(a.min, b.min); c.max = max(a.max, b.max); c.minf = a.minf, c.tedf = a.tedf; if (b.minf < c.minf) c.minf = b.minf, c.tedf = b.tedf; else if (b.minf == c.minf) c.tedf += b.tedf; c.mimm = a.mimm, c.tmimm = a.tmimm; if (b.mimm < c.mimm) c.mimm = b.mimm, c.tmimm = b.tmimm; else if (b.mimm == c.mimm) c.tmimm += b.tmimm; c.mipm = a.mipm, c.tmipm = a.tmipm; if (b.mipm < c.mipm) c.mipm = b.mipm, c.tmipm = b.tmipm; else if (b.mipm == c.mipm) c.tmipm += b.tmipm; c.lazymax = -1, c.lazymin = -1; return c; } void updatemax(int node, int st, int en, int l, int r, int x) { if (l >= en || st >= r) return; if (l <= st && en <= r) { updmax(node, st, en, x); return; } int mid = (st + en) >> 1; shift(node, st, en); updatemax(node << 1, st, mid, l, r, x); updatemax(node << 1 | 1, mid, en, l, r, x); seg[node] = merge(seg[node << 1], seg[node << 1 | 1]); } void updatemin(int node, int st, int en, int l, int r, int x) { if (l >= en || st >= r) return; if (l <= st && en <= r) { updmin(node, st, en, x); return; } int mid = (st + en) >> 1; shift(node, st, en); updatemin(node << 1, st, mid, l, r, x); updatemin(node << 1 | 1, mid, en, l, r, x); seg[node] = merge(seg[node << 1], seg[node << 1 | 1]); } node emp; node get(int node, int st, int en, int l, int r) { if (l >= en || st >= r) { emp.minf = infint, emp.tedf = 0; emp.mimm = infint, emp.tmimm = 0; emp.mipm = infint, emp.tmipm = 0; emp.min = infint, emp.max = -infint; emp.lazymax = -1, emp.lazymin = -1; return emp; } if (l <= st && en <= r) return seg[node]; int mid = (st + en) >> 1; shift(node, st, en); return merge(get(node << 1, st, mid, l, r), get(node << 1 | 1, mid, en, l, r)); } int n, a[MAXN], s[MAXN], g[MAXN]; int zero[MAXN], oc[MAXN], prv[MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < 4 * MAXN; i++) seg[i] = node(); for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } for (int i = 0; i < n; i++) if (a[i] == 0) zero[i] = i; else if (i == 0) zero[i] = -1; else zero[i] = zero[i - 1]; memset(oc, -1, sizeof oc); for (int i = 0; i < n; i++) { prv[i] = oc[a[i]], oc[a[i]] = i; if (i) prv[i] = max(prv[i], prv[i - 1]); } for (int i = 0; i < n; i++) prv[i]++; stack<int> S; for (int i = 0; i < n; i++) { while (!S.empty() && a[S.top()] > a[i]) S.pop(); if (S.size()) s[i] = S.top(); else s[i] = -1; S.push(i); } while (S.size()) S.pop(); for (int i = 0; i < n; i++) { while (!S.empty() && a[S.top()] < a[i]) S.pop(); if (S.size()) g[i] = S.top(); else g[i] = -1; S.push(i); } long long ans = 0; for (int i = 0; i < n; i++) { updatemin(1, 0, n, s[i] + 1, i + 1, a[i]); updatemax(1, 0, n, g[i] + 1, i + 1, a[i]); if (prv[i] <= zero[i]) { node cur = get(1, 0, n, prv[i], zero[i] + 1); if (cur.minf == i) ans += cur.tedf; } } cout << ans; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1000010; const int s = 1000000; int n; int b[maxn]; int p[maxn]; int c[maxn]; vector<int> ans; int main() { scanf( %d , &n); int t; for (int i = 0; i < n; ++i) { scanf( %d , &t); b[t] = 1; p[i] = t; } int l = 1; for (int i = 0; i < n; ++i) { if (c[p[i]] == 0) { if (b[s - p[i] + 1] == 0) { ans.push_back(s - p[i] + 1); b[s - p[i] + 1] = 1; } else { while (b[l] || b[s - l + 1]) { l++; } ans.push_back(l); b[l] = 1; ans.push_back(s - l + 1); b[s - l + 1] = 1; c[s - p[i] + 1] = 1; } } } printf( %ld n , ans.size()); for (int i = 0; i < ans.size(); ++i) { if (i) printf( ); printf( %d , ans[i]); } printf( n ); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const int MOD = 1e9 + 7; const int N = 0; string s; long long solve(string s) { int n = s.length(); long long ans = 1988; for (auto i = (n - 1); i >= (4); i--) { long long mul = 1, add = 0; for (auto j = (i); j < (n); j++) { mul = 10 * mul; add = 10 * add + s[j] - 0 ; } long long nx = max(mul + ans - add, 0LL) / mul; ans = nx * mul + add; } return ans; } int main() { int test; cin >> test; while (test--) { cin >> s; cout << solve(s) << n ; } }
#include <bits/stdc++.h> using namespace std; string name[105], body[105]; int n; string s; int d[105]; map<string, int> macro_id; int add(string& s, int& ptr); void go(int k); int un(string& s, int& ptr) { if (s[ptr] == ( ) { ptr++; int r = add(s, ptr); if (r & 2) r ^= 2; if (r & 4) r ^= 4; ptr++; return r; } string id = ; while (ptr < s.size() && (isalpha(s[ptr]) || isdigit(s[ptr]))) { id += s[ptr++]; } if (macro_id.count(id)) { int k = macro_id[id]; go(k); return d[k]; } else { return 0; } } int mul(string& s, int& ptr) { int ret = un(s, ptr); int cnt = 1; char last_op = * ; bool f = true; while (ptr < s.size() && (s[ptr] == * || s[ptr] == / )) { if (f && (ret & 2)) { ret |= 1; } f = false; last_op = s[ptr]; ptr++; int next = un(s, ptr); if (next & 4) ret |= 4; if (next & 1) ret |= 1; if (next & 2) ret |= 1; if (last_op == / && (next & 4)) ret |= 1; cnt++; } if (cnt > 1) ret |= 4; return ret; } int add(string& s, int& ptr) { int ret = mul(s, ptr); int cnt = 1; char last_op = + ; while (ptr < s.size() && (s[ptr] == + || s[ptr] == - )) { last_op = s[ptr]; ptr++; int next = mul(s, ptr); if (next & 2) ret |= 2; if (next & 1) ret |= 1; if (last_op == - && (next & 2)) ret |= 1; cnt++; } if (cnt > 1) { if (ret & 4) ret ^= 4; ret |= 2; } return ret; } void go(int k) { if (d[k] != -1) { return; } string str = body[k]; int ptr = 0; int code = add(str, ptr); d[k] = code; } int main() { scanf( %d n , &n); for (int i = 0; i < int(n); i++) { getline(cin, s); while (s[0] == ) s.erase(0, 1); s.erase(0, 1); while (s[0] == ) s.erase(0, 1); s.erase(0, 7); while (s[0] == ) s.erase(0, 1); int p = s.find( ); name[i] = s.substr(0, p); s.erase(0, p + 1); for (int j = 0; j < int(s.size()); j++) { if (s[j] != ) { body[i] += s[j]; } } macro_id[name[i]] = i; } getline(cin, s); for (int j = 0; j < int(s.size()); j++) { if (s[j] != ) { body[n] += s[j]; } } for (int i = 0; i < int(n + 1); i++) { d[i] = -1; } go(n); if (d[n] & 1) { printf( Suspicious n ); } else { printf( OK n ); } return 0; }
#include <bits/stdc++.h> using namespace std; const int NMAX = 200005; int n; char str[NMAX]; int in, allin; int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); cin >> n; cin >> str; for (int i = 0; i < n; ++i) if (str[i] == A ) ++allin; else if (str[i] == I ) ++in; if (!in) cout << allin << n ; else cout << (in == 1 ? 1 : 0) << n ; return 0; }
#include <bits/stdc++.h> int a, b, n; long long ans; int chk[10000010]; int np, pri[10000010]; int lis[10000010], mon[10000010]; void go(int now, int poi) { lis[now] = 1; for (int i = poi; i < np; ++i) { long long t = (long long)now * pri[i]; if (t > n) break; go(t, i + 1); } } void solve() { for (int i = 2; i * i <= n; ++i) if (chk[i] == 0) for (int j = i * i; j <= n; j += i) chk[j] = 1; for (int i = 2; i <= n; ++i) if (chk[i] == 0) { pri[np] = i; ++np; } go(1, 0); for (int i = n, j = -1; i >= 1; --i) { if (lis[i] == 1) { lis[i] = j; j = i; } } for (int i = 1; i != -1; i = lis[i]) { for (int j = 1;; ++j) { int t = j * j * i; if (j * j * i > n) break; mon[t] = i; } } } int main(void) { scanf( %d%d , &a, &n); b = a + n - 1; n = b; solve(); for (int i = a; i <= b; ++i) ans += mon[i]; printf( %I64d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 3009; int box[N], kk; struct node { int t, next; } e[N << 1]; int n; void add(int s, int t) { e[kk].t = t, e[kk].next = box[s]; box[s] = kk++; } int tot; void pre_dfs(int r, int fa) { for (int i = box[r]; i + 1; i = e[i].next) if (e[i].t != fa) { pre_dfs(e[i].t, r); tot += i & 1; } } int cut; void dfs(int r, int fa, int now) { cut = max(now, cut); for (int i = box[r]; i + 1; i = e[i].next) if (e[i].t != fa) { if (i & 1) dfs(e[i].t, r, now + 1); else dfs(e[i].t, r, max(now - 1, 0)); } } int main() { memset(box, -1, sizeof(box)); cin >> n; for (int i = 1; i < n; i++) { int s, t; cin >> s >> t; add(s, t); add(t, s); } int ans = N; for (int i = 1; i <= n; i++) { tot = 0; pre_dfs(i, i); cut = 0; dfs(i, i, 0); ans = min(tot - cut, ans); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 5; int a[maxn], n, x; int main() { int k; cin >> k; while (k--) { cin >> n >> x; int sum = 0; bool f = 1, f1 = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; if (a[i] != x) f = 0; if (a[i] == x) f1 = 1; } if (f == 1) cout << 0 << endl; else if (sum == n * x || f1 == 1) cout << 1 << endl; else cout << 2 << endl; } return 0; }
#include<bits/stdc++.h> typedef long long ll; typedef long double ld; using namespace std; mt19937_64 mrand(chrono::steady_clock::now().time_since_epoch().count()); #define ii for(int i=1;i<=n;++i) #define ji for(int j=1;j<=n;++j) #define jj for(int j=1;j<=m;++j) #define ij for(int i=1;i<=m;++i) #define all(x) x.begin(),x.end() #define al(x) x+1,x+1+n #define asd cout<< ok <<endl; #define vi vector<int> #define vvi vector<vector<int>> #define vl vector<ll> #define vii vector<pair<int,int>> #define pc(x) __builtin_popcount(x) #define pb push_back vector<int> a[200005]; vector<pair<int,int>> r[200005]; int cnt[200005]; inline int read() { char c=getchar();int x=0,f=1; while(c< 0 ||c> 9 ) {if(c== - ) f=-1;c=getchar();} while(c>= 0 &&c<= 9 )x=x*10+c- 0 ,c=getchar(); return x*f; } int main() { int t = read(); while(t--) { int n = read(); int sz = 0; for(int i=1;i<=n;++i) a[i].clear(); vector<int> aa; for(int i=1;i<=n;++i) { int x = read(); sz += x; while(x--) { int y = read(); a[i].push_back(y); aa.push_back(y); } } sort(aa.begin(), aa.end()); aa.resize(unique(aa.begin(), aa.end()) - aa.begin()); auto f = [&](int x) { return lower_bound(aa.begin(), aa.end(), x) - aa.begin() + 1; }; for(int i=1;i<=n;++i) { for(int &j:a[i]) j=f(j); sort(all(a[i])); } int sq = pow(sz, 0.4), ok = 0; for(int i=1;i<=n;++i) { if(a[i].size() > sq) { for(int j:a[i]) cnt[j]++; for(int j=1;j<=n;++j) { if(i==j) continue; int cnt2=0; for(int k:a[j]) cnt2+=cnt[k]; if(cnt2>1) { printf( %d %d n , i, j); ok=1; break; } } for(int j:a[i]) cnt[j]--; } else { for(int j=0;j<a[i].size();++j) { for(int k=j+1;k<a[i].size();++k) { r[a[i][j]].push_back({a[i][k], i}); } } } if(ok) break; } if(!ok) { for(int i=1;i<=aa.size();++i) { sort(all(r[i])); for(int j=1;j<r[i].size();++j) { if(r[i][j].first == r[i][j-1].first) { ok = 1; printf( %d %d n , r[i][j].second, r[i][j-1].second); break; } } if(ok) break; } } for(int i=1;i<=aa.size();++i) r[i].clear(); if(!ok) puts( -1 ); } }
#include <bits/stdc++.h> using namespace std; const int maxn = 1010; int c1, c2; char str[maxn]; int main() { scanf( %s , str); int len = int(strlen(str)); for (int i = 0; i < len; i++) { int t = str[i] - 0 ; if (t == 0) { if (!c1) { puts( 1 1 ); } else puts( 3 1 ); c1 ^= 1; } else { if (!c2) { puts( 4 3 ); } else puts( 4 1 ); c2 ^= 1; } } return 0; }
#include <bits/stdc++.h> using namespace std; long long labs(long long a) { return a < 0 ? (-a) : a; } long long max(long long a, long long b) { return a > b ? a : b; } long long min(long long a, long long b) { return a < b ? a : b; } const int MAXN = 1005; const int UNDEF = -1; const int IMPOS = INT_MAX - 2; char f[MAXN][MAXN]; int dph[MAXN], dpv[MAXN]; int n, m; int RecV(int i); int RecH(int j) { if (dph[j] != UNDEF) return dph[j]; dph[j] = IMPOS; for (int i = 0; i < n; i++) { if (f[i][j] == # ) { dph[j] = min(dph[j], RecV(i) + 1); } } return dph[j]; } int RecV(int i) { if (dpv[i] != UNDEF) return dpv[i]; dpv[i] = IMPOS; for (int j = 0; j < m; j++) { if (f[i][j] == # ) { dpv[i] = min(dpv[i], RecH(j) + 1); } } return dpv[i]; } int main() { cin >> n >> m; fill(dph, dph + MAXN, UNDEF); fill(dpv, dpv + MAXN, UNDEF); for (int(i) = 0; (i) < (n); (i)++) { cin >> f[i]; } dpv[n - 1] = 0; int res = RecV(0); if (res == IMPOS) { res = -1; } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long a[503400], b[477574], c[534567], d, e, i, j, r, m, y, k, l, n, s, z, x, t, tt; string p[3456], q, qq; map<long long, long long> zz; int main() { cin >> n >> m; for (i = 1; i <= n; i++) cin >> p[i]; cin >> x; q = p[x]; a[x] = 1; t = q.size(); for (i = 1; i < m; i++) { cin >> y; a[y] = 1; if (p[y].size() != t) { cout << No ; return 0; } for (j = 0; j < t; j++) { if (q[j] == ? ) continue; if (p[y][j] != q[j]) q[j] = ? ; } } for (i = 1; i <= n; i++) { if (a[i] == 1) continue; if (p[i].size() != t) continue; x = 0; for (j = 0; j < t; j++) { if (q[j] == ? ) continue; if (q[j] != p[i][j]) x = 1; } if (x == 0) { cout << No ; return 0; } } cout << Yes << endl << q; }
#include <iostream> #include <algorithm> #include <vector> using namespace std; typedef vector<int> vi; int n,a[300010],res[300010],gtm[300010]; vi L,R,rm; void solve() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; L.assign(n,0); R.assign(n,0); rm.assign(n + 1, 0); L[0] = 0; for (int i = 1; i < n; i++) { if (a[i] > a[i - 1]) L[i] = i; else { int j = L[i - 1]; while (j > 0 && a[j - 1] >= a[i]) j = L[j - 1]; L[i] = j; } } R[n - 1] = n - 1; for (int i = n - 2; i >= 0; i--) { if (a[i] > a[i + 1]) R[i] = i; else { int j = R[i + 1]; while (j < n - 1 && a[j + 1] >= a[i]) j = R[j + 1]; R[i] = j; } } for (int i = 0; i < n; i++) { int x = R[i] - L[i] + 1; rm[a[i]] = max(rm[a[i]], x); } gtm[1] = rm[1]; for (int i = 2; i <= n; i++) { gtm[i] = min(gtm[i - 1], rm[i]); } for (int i = n; i >= 1; i--) { int k = n - i + 1; res[k] = (gtm[i] >= k); } for (int i = 1; i <= n; i++) cout << res[i]; cout << endl; } int main() { int t; cin >> t; while(t--) solve(); // system( pause ); return 0; }
#include <bits/stdc++.h> using namespace std; bool need[8000010], rem[8000010]; int n, k, b[8000010], sum[8000010], minl = (int)1e9, out; vector<int> ans; int main() { scanf( %d%d , &k, &n); for (int i = 1; i <= k; i++) scanf( %d , &sum[i]), sum[i] += sum[i - 1]; for (int i = 1; i <= n; i++) scanf( %d , &b[i]), minl = min(b[i], minl); for (int i = 1; i <= k; i++) ans.push_back(minl - sum[i]); for (int i = 0; i < ans.size(); i++) { for (int j = 1; j <= n; j++) need[b[j] + 4000000] = 1; int x = ans[i], tot = 0; if (rem[x] == 1) continue; rem[x] = 1; for (int j = 1; j <= k; j++) { tot += need[x + sum[j] + 4000000]; need[x + sum[j] + 4000000] = 0; } if (tot == n) out++; } cout << out << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int k, t = 0, tt = 0, j = -1; int kk; int main() { cin >> k >> kk; if (k == 5) { cout << >...v << endl; cout << v.<.. << endl; cout << ..^.. << endl; cout << >.... << endl; cout << ..^.< << endl; cout << 1 << << 1; } else if (k == 3) { cout << >vv << endl; cout << ^<. << endl; cout << ^.< << endl; cout << 1 << << 3; } else { for (int j = 0; j < 100; j++) { if (j == 0) { for (int i = 0; i < 50; i++) { cout << > ; } for (int i = 50; i < 74; i++) { cout << > ; cout << . ; } cout << > ; cout << v ; } else if (j == 99) { cout << ^ ; cout << . ; cout << . ; cout << < ; for (int i = 0; i < 23; i++) { cout << . ; cout << < ; } for (int i = 50; i < 100; i++) { cout << < ; } } else if (j % 2 == 1) { cout << ^ ; cout << v ; cout << . ; cout << < ; for (int i = 0; i < 23; i++) { cout << . ; cout << < ; } for (int i = 50; i < 100; i++) { cout << < ; } } else if (j % 2 == 0) { cout << ^ ; for (int i = 1; i < 50; i++) { cout << > ; } for (int i = 50; i < 74; i++) { cout << . ; cout << > ; } cout << . ; cout << v ; } cout << endl; } cout << 1 << << 1; } }
#include <bits/stdc++.h> using namespace std; signed long long int a, b; long long int a1, b1, y, s; int main() { cin >> a >> b >> s; if (a >= 0) a1 = a; else a1 = 0 - a; if (b >= 0) b1 = b; else b1 = 0 - b; y = a1 + b1; if (y - s == 0 || (y - s) % 2 == 0) { if (s >= y) cout << Yes ; else cout << No ; } else cout << No ; }
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T--) { int S; cin >> S; string str; cin >> str; for (int i = 0; i < S; i++) cout << str[S - 1]; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int INF = (int)1e9; const long long LINF = (long long)1e18; const long double PI = acos((long double)-1); const long double EPS = 1e-9; long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r; } return a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } long long fpow(long long n, long long k, int p = MOD) { long long r = 1; for (; k; k >>= 1) { if (k & 1) r = r * n % p; n = n * n % p; } return r; } void addmod(int& a, int val, int p = MOD) { if ((a = (a + val)) >= p) a -= p; } void submod(int& a, int val, int p = MOD) { if ((a = (a - val)) < 0) a += p; } int mult(int a, int b, int p = MOD) { return (long long)a * b % p; } int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } int arr[2000005] = {0}; int foo(int n) { int maxsize = -1, startindex; int sumleft[n]; int min, max; int i; sumleft[0] = ((arr[0] == 0) ? -1 : 1); min = arr[0]; max = arr[0]; for (i = 1; i < n; i++) { sumleft[i] = sumleft[i - 1] + ((arr[i] == 0) ? -1 : 1); if (sumleft[i] < min) min = sumleft[i]; if (sumleft[i] > max) max = sumleft[i]; } int hash[max - min + 1]; for (i = 0; i < max - min + 1; i++) hash[i] = -1; for (i = 0; i < n; i++) { if (sumleft[i] == 0) { maxsize = i + 1; startindex = 0; } if (hash[sumleft[i] - min] == -1) hash[sumleft[i] - min] = i; else { if ((i - hash[sumleft[i] - min]) > maxsize) { maxsize = i - hash[sumleft[i] - min]; startindex = hash[sumleft[i] - min] + 1; } } } if (maxsize == -1) maxsize = 0; return maxsize; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; string s; cin >> s; for (int i = 0; i < n; i++) { if (s[i] == 1 ) arr[i] = 1; } cout << foo(n); return 0; }
#include <bits/stdc++.h> using namespace std; long long int pow1(long long int a, long long int b, long long int m = 1000000007) { if (b == 0) return 1ll; else if (b == 1) return a; else { long long int x = pow1(a, b / 2, m); x *= x; x %= m; if (b % 2) { x *= a; x %= m; } return x; } } template <class avgType> avgType avg(avgType a, avgType b) { return (a + b) / 2; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count()); int randInt() { return rng() % INT_MAX; } long long int randLL() { return rng64() % LLONG_MAX; } vector<int> hashmods = {1000000007, 1000000009, 1000000021, 1000000033, 1000000087, 1000000093}; long long int n, k, a[110000], t; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> t; while (t--) { cin >> n >> k; int pres = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; pres += (a[i] == k); } if (pres == 0) { cout << no n ; continue; } if (n == 1) { cout << yes n ; continue; } else if (n == 2 and a[1] >= k and a[2] >= k) { cout << yes n ; continue; } int yes = 0, less = 0, more = 0, presk = 0; for (int i = 1; i <= n - 2; i++) { int m = (a[i] >= k) + (a[i + 1] >= k) + (a[i + 2] >= k); if (m >= 2) { yes++; } } cout << (yes ? yes n : no n ); } return 0; }
#include <bits/stdc++.h> using namespace std; int t; int a[4][100100]; vector<int> beg[4], en[4]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> t; while (t--) { bool out = 1; int n, noob; cin >> n >> noob; for (int i = 1; i <= 3; i++) { for (int j = 1; j <= n; j++) { char x; cin >> x; if (x == s ) a[i][j] = 1; else if (x == . ) a[i][j] = 0; else a[i][j] = 3; } } for (int i = 1; i <= 3; i++) for (int j = 1; j <= n; j++) { if (a[i][j] == 3 && (j == 1 || a[i][j - 1] != 3)) beg[i].push_back(j); if (a[i][j] == 3 && (j == n || a[i][j + 1] != 3)) en[i].push_back(j); } for (int j = 1; j < n; j++) { for (int i = 1; i <= 3; i++) if (a[i][j] == 1 && a[i][j + 1] != 3) a[i][j + 1] = 1, a[i][j] = 0; if (a[2][j + 1] == 1) { if (a[1][j + 1] == 0) a[1][j + 1] = 1; if (a[3][j + 1] == 0) a[3][j + 1] = 1; } else if (a[1][j + 1] == 1 && a[2][j + 1] == 0) a[2][j + 1] = 1; else if (a[3][j + 1] == 1 && a[2][j + 1] == 0) a[2][j + 1] = 1; for (int i = 1; i <= 3; i++) { for (int k = 0; k < en[i].size(); k++) { int e = en[i][k]; a[i][e] = 0; if (e > 1) a[i][e - 1] = 0; en[i][k] = max(0, e - 2); } for (int k = 0; k < beg[i].size(); k++) { int b = beg[i][k]; if (b > 1) a[i][b - 1] = 3; if (b > 2) a[i][b - 2] = 3; beg[i][k] = max(0, b - 2); } } bool check = 0; for (int i = 1; i <= 3; i++) if (a[i][j + 1] == 1) check = 1; if (check == 0) { out = 0; } } if (out) cout << YES << endl; else cout << NO << endl; for (int i = 1; i <= 3; i++) { beg[i].clear(); en[i].clear(); } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, ans[1500], t, i, j, v[1100]; memset(v, 0, sizeof(v)); for (i = 2; i * i <= 1100; i++) { if (v[i] == 0) { for (j = i + i; j <= 1100; j += i) v[j] = 1; } } while (scanf( %d , &n) != EOF) { if (n == 1) printf( 0 n ); else { t = 0; for (j = 2; j <= n; j++) { if (v[j] == 0) { for (i = j; i <= n; i *= j) { ans[t] = i; t++; } } } printf( %d n , t); for (i = 0; i < t - 1; i++) printf( %d , ans[i]); printf( %d n , ans[t - 1]); } } return 0; }
#include <bits/stdc++.h> using namespace std; const long long int M = 1000000007; long long int mod(long long int x) { return (x % M + M) % M; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, t, c, ans = 0, g = 0, cnt = 0; cin >> n >> t >> c; long long int arr[n]; for (long long int i = 0; i < n; ++i) { cin >> arr[i]; } for (long long int i = 0; i < n; i++) { g++; if (arr[i] > t) { g = 0; } else { if (g >= c) ans += 1; } } cout << ans << endl; cerr << fixed << setprecision(10); cerr << time taken : << (float)clock() / CLOCKS_PER_SEC << secs << endl; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool upmin(T &x, T y) { return y < x ? x = y, 1 : 0; } template <typename T> inline bool upmax(T &x, T y) { return x < y ? x = y, 1 : 0; } const long double eps = 1e-11; const long double pi = acos(-1); const long long oo = 1 << 30; const long long loo = 1ll << 62; const long long mods = 998244353; const long long MAXN = 200005; const long long INF = 0x3f3f3f3f; inline long long read() { long long f = 1, x = 0; char c = getchar(); while (c < 0 || c > 9 ) { if (c == - ) f = -1; c = getchar(); } while (c >= 0 && c <= 9 ) { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); } return x * f; } long long Ans[1005], num = 0; long long solve(long long x, long long y) { if (!y) return x; Ans[++num] = x / y; long long t = solve(y, x % y); return t; } signed main() { long long x = read(), y = read(); if (solve(x, y) != 1) { puts( Impossible ); return 0; } Ans[num]--; for (long long i = 1; i <= num; i++) if (Ans[i] > 0) printf( %lld , Ans[i]), putchar(66 - (i & 1)); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m, i, j, k, d; cin >> n >> m; vector<int> arr(101, 0); for (i = 0; i < m; i++) { int no; cin >> no; arr[no]++; } for (d = m; d >= 1; d--) { vector<int> cc(arr); int k = 0; for (i = 0; i < cc.size(); i++) { if (cc[i] >= d) { k += (floor(cc[i] / d)); } } if (k >= n) { cout << d << endl; return 0; } } cout << 0 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using LL = long long; constexpr int N = 5002; constexpr int mod = 1e9 + 7; string x, y; int n, m; int cache[N][N]; int dp(int a, int b) { if (b == m) return 0; int &rv = cache[a][b]; if (~rv) return rv; rv = dp(a, b + 1); if (x[a] == y[b]) rv += 1 + dp(a + 1, b + 1), rv %= mod; return rv; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> x >> y; n = x.size(), m = y.size(); memset(cache, -1, sizeof cache); dp(0, 0); int result = 0; for (int i = 0; i < n; i++) result += dp(i, 0), result %= mod; cout << result << endl; }
#include <bits/stdc++.h> const int inf = 0x3f3f3f3f, Inf = 0x7fffffff; const long long INF = 0x7fffffffffffffff; const double eps = 1e-10; unsigned int seed = 19260817; const unsigned int _RAND_MAX_ = 4294967295u; __inline__ __attribute__((always_inline)) unsigned int Rand() { return seed = seed * 998244353u + 1000000007u; } template <typename _Tp> _Tp gcd(const _Tp &a, const _Tp &b) { return (!b) ? a : gcd(b, a % b); } template <typename _Tp> __inline__ __attribute__((always_inline)) _Tp abs(const _Tp &a) { return a >= 0 ? a : -a; } template <typename _Tp> __inline__ __attribute__((always_inline)) _Tp max(const _Tp &a, const _Tp &b) { return a < b ? b : a; } template <typename _Tp> __inline__ __attribute__((always_inline)) _Tp min(const _Tp &a, const _Tp &b) { return a < b ? a : b; } template <typename _Tp> __inline__ __attribute__((always_inline)) void chmax(_Tp &a, const _Tp &b) { (a < b) && (a = b); } template <typename _Tp> __inline__ __attribute__((always_inline)) void chmin(_Tp &a, const _Tp &b) { (b < a) && (a = b); } template <typename _Tp> __inline__ __attribute__((always_inline)) bool _cmp(const _Tp &a, const _Tp &b) { return abs(a - b) <= eps; } template <typename _Tp> __inline__ __attribute__((always_inline)) void read(_Tp &x) { register char ch(getchar()); bool f(false); while (ch < 48 || ch > 57) f |= ch == 45, ch = getchar(); x = ch & 15, ch = getchar(); while (ch >= 48 && ch <= 57) x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar(); if (f) x = -x; } template <typename _Tp, typename... Args> __inline__ __attribute__((always_inline)) void read(_Tp &t, Args &...args) { read(t); read(args...); } __inline__ __attribute__((always_inline)) int read_str(char *s) { register char ch(getchar()); while (ch == || ch == r || ch == n ) ch = getchar(); register char *tar = s; *tar = ch, ch = getchar(); while (ch != && ch != r && ch != n && ch != EOF) *(++tar) = ch, ch = getchar(); return tar - s + 1; } const int N = 300005; const int mod = 1000000007; const long long inv2 = 500000004; struct matrix { long long a, b, c, d, e, f; __inline__ __attribute__((always_inline)) matrix operator*( const matrix &o) const { return (matrix){a * o.a % mod, (b * o.a + c * o.b) % mod, c * o.c % mod, (d * o.a + e * o.b + f * o.d) % mod, (e * o.c + f * o.e) % mod, f * o.f % mod}; } } I; __inline__ __attribute__((always_inline)) matrix get1(long long x) { return (matrix){1ll, x * inv2 % mod, inv2, 0, x * inv2 % mod, 1ll}; } __inline__ __attribute__((always_inline)) matrix get2(long long x) { return (matrix){1ll, mod - x, 2ll, (x * x) % mod * inv2 % mod, mod - x, 1ll}; } int t[N << 1]; int a[N]; int b[N << 1]; struct seg_tr { struct Node { int ls, rs; matrix val; } f[N << 2]; int node_cnt; __inline__ __attribute__((always_inline)) void PushUp(int x) { f[x].val = f[f[x].ls].val * f[f[x].rs].val; } int build(int l, int r) { int cur = ++node_cnt; if (l == r) { matrix res(I), a = get1(t[l]); while (b[l]) res = res * a, --b[l]; f[cur].val = res; return cur; } int mid = (l + r) >> 1; f[cur].ls = build(l, mid); f[cur].rs = build(mid + 1, r); PushUp(cur); return cur; } void Update1(int l, int r, int pos, int cur) { if (l == r) { f[cur].val = f[cur].val * get1(t[l]); return; } int mid = (l + r) >> 1; if (pos <= mid) Update1(l, mid, pos, f[cur].ls); else Update1(mid + 1, r, pos, f[cur].rs); PushUp(cur); } void Update2(int l, int r, int pos, int cur) { if (l == r) { f[cur].val = f[cur].val * get2(t[l]); return; } int mid = (l + r) >> 1; if (pos <= mid) Update2(l, mid, pos, f[cur].ls); else Update2(mid + 1, r, pos, f[cur].rs); PushUp(cur); } __inline__ __attribute__((always_inline)) long long Query() { return f[1].val.d; } } tr; int Q[N][2]; int main() { I = (matrix){1ll, 0ll, 1ll, 0ll, 0ll, 1ll}; int n; read(n); int pos = 0; for (int i = 1; i <= n; ++i) { read(a[i]); t[++pos] = a[i]; } int q; read(q); for (int i = 1; i <= q; ++i) { read(Q[i][0], Q[i][1]); t[++pos] = Q[i][1]; } std::sort(t + 1, t + pos + 1); pos = std::unique(t + 1, t + pos + 1) - t - 1; for (int i = 1; i <= n; ++i) { a[i] = std::lower_bound(t + 1, t + pos + 1, a[i]) - t; ++b[a[i]]; } tr.build(1, pos); printf( %lld n , tr.Query()); for (int i = 1; i <= q; ++i) { Q[i][1] = std::lower_bound(t + 1, t + pos + 1, Q[i][1]) - t; tr.Update2(1, pos, a[Q[i][0]], 1); tr.Update1(1, pos, Q[i][1], 1); a[Q[i][0]] = Q[i][1]; printf( %lld n , tr.Query()); } return 0; }
#include <bits/stdc++.h> using namespace std; struct $ { $() { ios_base::sync_with_stdio(false); cin.tie(NULL); } } $; const int Maxn = 202005; class BIT { int *tree, sz; public: BIT() { sz = 202005; tree = new int[sz + 1]; } BIT(int N) { sz = N; tree = new int[N + 1]; } ~BIT() { delete[] tree; } void update(int idx, int Val) { if (idx < 1 || idx > sz) return; while (idx <= sz) { tree[idx] += Val; idx += idx & -idx; } } int read(int idx) { int Sum = 0; while (idx > 0) { Sum += tree[idx]; idx -= (idx & -idx); } return Sum; } int rangeSum(int l, int r) { int Ret = read(r) - read(l - 1); Ret = max(0, Ret); return Ret; } int readSingle(int idx) { int sum = tree[idx]; if (idx > 0) { int z = idx - (idx & -idx); idx--; while (idx != z) { sum -= tree[idx]; idx -= (idx & -idx); } } return sum; } }; int T1[Maxn], T2[Maxn]; int main() { int n, k, a, b, q, type, d, val; cin >> n >> k >> a >> b >> q; BIT B1, B2; while (q--) { cin >> type; if (type == 1) { cin >> d >> val; if (T1[d] < a) { int temp = min(val, a - T1[d]); T1[d] += temp; B1.update(d, temp); } if (T2[d] < b) { int temp = min(val, b - T2[d]); T2[d] += temp; B2.update(d, temp); } } else { cin >> d; int Ans = 0; Ans = B2.rangeSum(1, d - 1); int z = B1.rangeSum(min(d + k, Maxn - 1), Maxn - 1); Ans += z; cout << Ans << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3fffffff; const int SINF = 0x7fffffff; const long long LINF = 0x3fffffffffffffff; const long long SLINF = 0x7fffffffffffffff; const int MAXN = 65; const int MAXB = 10; const int MAXS = (1 << 10) + 7; int q; int b; long long l, r; int a[MAXN]; long long dp[MAXB + 3][MAXN][MAXS]; void init(); void input(); void inputq(); void work(); long long getans(long long x); int main() { init(); input(); work(); } void init() { ios::sync_with_stdio(false); for (int i = 2; i <= MAXB; ++i) { int ms = (1 << i); dp[i][0][0] = 1; for (int j = 1; j < MAXN; ++j) { for (int k = 0; k < ms; ++k) { for (int x = 0; x < i; ++x) { dp[i][j][k ^ (1 << x)] += dp[i][j - 1][k]; } } } } } void input() { scanf( %d , &q); } void inputq() { scanf( %d%I64d%I64d , &b, &l, &r); } void work() { for (int i = 0; i < q; ++i) { inputq(); printf( %I64d n , getans(r) - getans(l - 1)); } } long long getans(long long x) { if (!x) return 0; int n = 0; for (long long t = x; t; t /= b) { a[n++] = t % b; } long long ans = 0; int beg, ns = 0; for (int i = n - 1; i >= 0; --i) { beg = (i == n - 1) ? 1 : 0; for (int j = beg; j < a[i]; ++j) { ans += dp[b][i][ns ^ (1 << j)]; } ns ^= (1 << a[i]); } if (!ns) ++ans; for (int i = n - 2; i >= 0; --i) { for (int j = 1; j < b; ++j) ans += dp[b][i][1 << j]; } return ans; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e3 + 7; const int mod = 1e9 + 7; map<int, int> mp; int n, x, ma, ans; int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d , &x); mp[x]++; } for (auto p : mp) { int x = p.first, y = p.second; for (int i = 1; i <= 20; i++) { if ((y >> i) & 1) { mp[x + i]++; mp[x] -= (1 << i); } } } for (auto p : mp) { int x = p.first, y = p.second; for (int i = 1; i <= 20; i++) { if ((y >> i) & 1) { mp[x + i]++; mp[x] -= (1 << i); } } } for (auto p : mp) { int x = p.first, y = p.second; ; if (y != 0) { ans++; ma = max(ma, x); } } printf( %d n , ma + 1 - ans); }
#include<iostream> #include<cstdio> #include<fstream> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue> #include<bitset> #include<cmath> #include<cstring> #include<cstdlib> using namespace std; typedef long long LL; typedef double DB; const int N = 222222; const int W = 1111111; int n,m,q,a[N],b[N]; LL sa,sb; int w=1e6+1,t[W]; LL g[W]; multiset<int> A,B; multiset<int>::iterator it; void ad(int x,int y){ int z=x*y; x++; while(x<=w){ t[x]+=y; g[x]+=z; x+=x&-x; } } void qu(int x,int&y,LL&z){ x++; while(x){ y+=t[x]; z+=g[x]; x-=x&-x; } } LL ansb(int p){ int o=min(*A.begin(),*B.begin()); if(p<=o) return sa-(LL)p*n-sb+(LL)p*m; int x=0; LL y=0; qu(p,x,y); return sa-(LL)o*n-sb+(LL)p*(m-x)+y; } LL ansa(int p){ int o=min(*A.begin(),*B.begin()); if(p<=o) return ansb(p); return ansb(p)-(p-o); } int main() { int i,x,y,o; LL s; scanf( %d%d%d ,&n,&m,&q); for(i=1;i<=n;i=i+1){ scanf( %d ,a+i); sa+=a[i]; A.insert(a[i]); } for(i=1;i<=m;i=i+1){ scanf( %d ,b+i); sb+=b[i]; ad(b[i],1); B.insert(b[i]); } while(q--){ scanf( %d%d ,&i,&x); if(i<=2){ scanf( %d ,&y); if(i==1){ A.erase(A.find(a[x])); sa-=a[x]; A.insert(y); sa+=y; a[x]=y; } else{ B.erase(B.find(b[x])); sb-=b[x]; ad(b[x],-1); B.insert(y); sb+=y; ad(y,1); b[x]=y; } } else{ s=max(ansb(*B.begin()-x),ansb(*B.rbegin()-x)); s=max(s,max(ansa(*A.begin()-x),ansa(*A.rbegin()-x))); o=min(*A.begin(),*B.begin()); it=A.lower_bound(o+x); if(it!=A.end()) s=max(s,ansa(*it-x)); if(it!=A.begin()){ it--; s=max(s,ansa(*it-x)); } it=A.lower_bound(*B.rbegin()+x); if(it!=A.end()) s=max(s,ansa(*it-x)); if(it!=A.begin()){ it--; s=max(s,ansa(*it-x)); } printf( %lld n ,s); } } return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const long long N = 200005; int dx[] = {-1, 0, 1, 0, 1, 1, -1, -1}; int dy[] = {0, -1, 0, 1, 1, -1, 1, -1}; long long powmod(long long a, long long b) { long long res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } long long powb(long long a, long long b) { long long res = 1; a; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a; a = a * a; } return res; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } void test() { int n, k; cin >> n >> k; vector<int> a(n), b(k), inda(n), vis(n); set<pair<int, int> > s; for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; inda[a[i]] = i; s.insert({i, a[i]}); } for (int i = 0; i < k; i++) { cin >> b[i]; b[i]--; vis[b[i]] = 1; } long long ans = 1; for (int i = 0; i < k; i++) { int idx = inda[b[i]]; auto pos = s.find({idx, a[idx]}); int inc = 0; if (pos != s.begin()) { if (vis[(*(prev(pos))).second] == 0) { inc++; } } if (next(pos) != s.end()) { if (vis[(*(next(pos))).second] == 0) inc++; } (ans *= 1LL * inc) %= mod; vis[b[i]] = 0; } cout << ans << n ; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; cin >> t; while (t--) { test(); } }
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1e7 + 10; int n, fac[MAX_N], pri[MAX_N], pri_n, fac_cnt[MAX_N], phi[MAX_N]; long long ans; void init() { scanf( %d , &n); for (int i = 2; i <= n; i++) { if (!fac[i]) { phi[i] = (fac[i] = pri[++pri_n] = i) - 1; fac_cnt[i] = 1; } for (int j = 1; j <= pri_n && pri[j] * i <= n && pri[j] <= fac[i]; j++) { fac[pri[j] * i] = pri[j]; fac_cnt[pri[j] * i] = fac_cnt[i] + (pri[j] == fac[i] ? 0 : 1); phi[pri[j] * i] = phi[i] * (pri[j] - (pri[j] == fac[i] ? 0 : 1)); } } } long long get_pair_suc() { long long suc = 0; for (int i = 2; i <= n; i++) if (i * 2 <= n || fac[i] != i) suc++; return suc * (suc - 1) / 2; } long long get_pair_1() { long long res = 0; for (int i = 2; i <= n; i++) res += i - 1 - phi[i]; return res; } long long get_pair_2() { static int pref_fac[MAX_N]; int pref_pri = 0; long long res = 0; for (int i = 2; i <= n; i++) { if (fac[i] == i) pref_pri++; else res += phi[i] - (pref_pri - fac_cnt[i]) - 1; } for (int i = 2; i <= n; i++) pref_fac[fac[i]]++; for (int i = 2; i <= n; i++) pref_fac[i] += pref_fac[i - 1]; for (int i = 2; i <= n; i++) { if (fac[i] != i) continue; int lim = n / fac[i]; res += pref_fac[lim]; for (int j = i; j <= n; j += i) if (fac[j] <= lim) res--; } for (int i = 2; i <= n; i++) if (fac_cnt[i] == 2 && fac[i / fac[i]] == i / fac[i]) res--; return res; } void solve() { long long pair_suc = get_pair_suc(); long long pair_1 = get_pair_1(); long long pair_2 = get_pair_2(); long long pair_3 = pair_suc - pair_1 - pair_2; long long ans = pair_1 * 1 + pair_2 * 2 + pair_3 * 3; printf( %lld n , ans); } int main() { init(); solve(); }
#include <bits/stdc++.h> using namespace std; struct ASDF { ASDF& operator,(int& a) { scanf( %d , &a); return *this; } ASDF& operator,(long int& a) { scanf( %ld , &a); return *this; } ASDF& operator,(long long int& a) { scanf( %lld , &a); return *this; } ASDF& operator,(char& c) { scanf( %c , &c); return *this; } ASDF& operator,(double& d) { scanf( %lf , &d); return *this; } template <typename T> ASDF& operator,(T& a) { cin >> a; return *this; } } asdf; template <typename T> ostream& operator<<(ostream& output, vector<T>& v) { output << [ ; if (int(v.size())) { output << v[0]; } for (int i = 1; i < int(v.size()); i++) { output << , << v[i]; } output << ] ; return output; } template <typename T1, typename T2> ostream& operator<<(ostream& output, pair<T1, T2>& p) { output << ( << p.first << , << p.second << ) ; return output; } int main() { int n, k; vector<int> v; cin >> n >> k; v.resize(n); for (int i = 0; i < n; i++) cin >> v[i]; long long sum = 0; for (int i = 0; i < n; i++) { if (k > 0 && v[i] < 0) { v[i] = -v[i]; k--; } } if (k & 1) { sort(v.begin(), v.end()); v[0] = -v[0]; } for (int i = 0; i < n; i++) sum += v[i]; cout << sum << endl; }
#include <bits/stdc++.h> using namespace std; long long n, m, k; long long read() { long long s = 0, f = 1; char ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) { s = s * 10 + ch - 0 ; ch = getchar(); } return s * f; } long long cnt = 0; struct trie { long long ch[26]; long long fail; long long tail; } t[10010]; long long f(char c) { if (c == A ) return 0; if (c == C ) return 1; if (c == T ) return 2; else return 3; } void insert(string s) { long long l = s.size(); long long nw = 0; for (long long i = 0, iend = l - 1; i <= iend; ++i) { if (t[nw].ch[f(s[i])] == 0) { t[nw].ch[f(s[i])] = ++cnt; } nw = t[nw].ch[f(s[i])]; } t[nw].tail = l; } void bfs() { queue<long long> q; for (long long i = 0, iend = 25; i <= iend; ++i) { if (t[0].ch[i]) { t[t[0].ch[i]].fail = 0; q.push(t[0].ch[i]); } } while (!q.empty()) { long long nw = q.front(); q.pop(); for (long long i = 0, iend = 25; i <= iend; ++i) { if (t[nw].ch[i] != 0) { t[t[nw].ch[i]].fail = t[t[nw].fail].ch[i]; t[t[nw].ch[i]].tail = max(t[t[nw].ch[i]].tail, t[t[t[nw].ch[i]].fail].tail); q.push(t[nw].ch[i]); } else { t[nw].ch[i] = t[t[nw].fail].ch[i]; } } } } const long long inf = 1e16; long long dp[1010][1010][11]; const long long mod = 1e9 + 9; signed main() { n = read(), m = read(); long long l = 0; for (long long i = 1, iend = m; i <= iend; ++i) { string s; cin >> s; long long ss = s.size(); l = max(l, ss); insert(s); } bfs(); dp[0][0][0] = 1; for (long long i = 0, iend = n - 1; i <= iend; ++i) { for (long long j = 0, jend = cnt; j <= jend; ++j) { long long v; for (long long k = 0, kend = l; k <= kend; ++k) { v = dp[i][j][k]; if (v) { for (long long p = 0, pend = 3; p <= pend; ++p) { long long nw = t[j].ch[p]; if (t[nw].tail > k) (dp[i + 1][nw][0] += v) %= mod; else if (k < l) (dp[i + 1][nw][k + 1] += v) %= mod; } } } } } long long ans = 0; for (long long i = 0, iend = cnt; i <= iend; ++i) { (ans += dp[n][i][0]) %= mod; } cout << ans; }
#include <bits/stdc++.h> using namespace std; template <typename S, typename T> inline bool smin(S &l, T r) { return r < l ? l = r, 1 : 0; } template <typename S, typename T> inline bool smax(S &l, T r) { return l < r ? l = r, 1 : 0; } constexpr int MOD = 1e9 + 7; template <typename S> inline S mod(S &l) { return l = (l % MOD + MOD) % MOD; } template <typename S, typename T> inline S add(S &l, T r) { return mod(l += r); } void fileIO(string s) { freopen((s + .in ).c_str(), r , stdin); freopen((s + .out ).c_str(), w , stdout); } constexpr int N = 1e5 + 10; constexpr int SQ = 780; int n, k; vector<int> vec[N]; int bias, dp[2][SQ], pd[2]; inline void input() { cin >> n >> k; for (int i = 0; i < k; i++) { int x, y; cin >> x >> y; vec[y].push_back(n - x + 1); } } inline void faze_0() { for (int i = 1; i < n + 1; i++) { sort(vec[i].begin(), vec[i].end()); while (vec[i].size() && vec[i].back() >= SQ - 2) { bias++; vec[i].pop_back(); } } } inline void faze_1() { memset(dp[0], 63, sizeof(dp)); dp[0][0] = 0; for (int i = 1; i < n + 1; i++) { int me = i & 1, num = 0; pd[me] = 1e9; for (int j = min(n - i + 1, SQ - 2); ~j; j--) { dp[me][j] = num * 3 + min(dp[me ^ 1][j + 1], pd[me ^ 1] + (j ? 2 : 0) + j * (j + 1) / 2); smin(pd[me], dp[me][j]); if (vec[i].size() && vec[i].back() == j) { num++; vec[i].pop_back(); } } } } int main() { input(); faze_0(); faze_1(); cout << 3 * bias + pd[n & 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { long long a, b, k; cin >> a >> b >> k; if (k % 2 == 0) cout << a * (k / 2) - b * (k / 2) << n ; else cout << a * (k / 2 + 1) - b * (k / 2) << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; long long int cnt[7]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, i, a, x; bool test = true; vector<pair<long long int, pair<long long int, long long int>>> v; cin >> n; for (i = 0; i < n; i++) { cin >> a; cnt[a]++; } x = n / 3; if (cnt[1] + cnt[2] + cnt[3] + cnt[4] + cnt[6] != n) test = false; if (cnt[1] == x && test) { for (i = 1; i <= x; i++) { if (cnt[4] > 0) { if (cnt[2] > 0) { v.push_back({1, {2, 4}}); cnt[2]--; cnt[4]--; } else { test = false; break; } } else if (cnt[3] > 0) { if (cnt[6] > 0) { v.push_back({1, {3, 6}}); cnt[3]--; cnt[6]--; } else { test = false; break; } } else if (cnt[2] > 0) { if (cnt[6] > 0) { v.push_back({1, {2, 6}}); cnt[2]--; cnt[6]--; } else { test = false; break; } } } } else test = false; if (test && cnt[2] == 0 && cnt[6] == 0 && cnt[3] == 0 && cnt[4] == 0) { for (i = 0; i < v.size(); i++) cout << v[i].first << << v[i].second.first << << v[i].second.second << endl; } else cout << -1 << endl; return 0; }
/*#pragma GCC target ( avx2 ) #pragma GCC optimization ( O3 ) #pragma GCC optimization ( unroll-loops ) */ #include<bits/stdc++.h> using namespace std; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << << x << ;} void __print(const char *x) {cerr << << x << ;} void __print(const string &x) {cerr << << x << ;} void __print(bool x) {cerr << (x ? true : false );} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << { ; __print(x.first); cerr << , ; __print(x.second); cerr << } ;} template<typename T> void __print(const T &x) {int f = 0; cerr << { ; for (auto &i: x) cerr << (f++ ? , : ), __print(i); cerr << } ;} void _print() {cerr << ] n ;} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << , ; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << [ << #x << ] = [ ; _print(x) #else #define debug(x...) #endif #define ll long long #define f first #define s second #define Fast ios_base::sync_with_stdio(false);cin.tie(NULL); typedef pair<ll , pair<ll, ll> > pi; int pow(int x,int y){ int res=1; while(y){ if(y&1) res*=x; y>>=1; x*=x; } return res; } struct Compare { constexpr bool operator()(pi const & a, pi const & b) const noexcept { return a.first < b.first || (a.first == b.first && a.second.first > b.second.first); } }; void prefix_function( string s,ll arr[] ) { long long border=0; arr[0]=0; for(long long i=1;i<s.size();i++) { while(border>0 && s[i]!=s[border]) border=arr[border-1]; if(s[i]==s[border]) border++; else border=0; arr[i]=border; } }//send mod-2 for a^-1 if mod is a prime number ll mod=998244353; ll add( ll a , ll b) { return (((a%mod)+(b%mod))%mod); } ll mul(ll a,ll b) { return (((a%mod)*(b%mod))%mod); } ll binpow(ll a, ll b) { ll res = 1; while (b) { if (b & 1) res = mul(res, a); a = mul(a, a); b >>= 1; } return res; } ll subs(ll a,ll b) { return (((a%mod)-(b%mod)+mod)%mod); } ll dv(ll a,ll b) { ll inv=binpow(b,mod-2); return mul(a,inv); } ll dsu_arr[100000]; ll dsu_sz[100000]; void dsu(ll n) { for(ll i=0;i<=n;i++) { dsu_arr[i]=i; dsu_sz[i]=1; } } ll find(ll x) { ll root=x; while (root!=dsu_arr[root]) { root=dsu_arr[root]; } while(x!=dsu_arr[x]) { dsu_arr[x]=root; x=dsu_arr[x]; } return root; } ll merge(ll x,ll y) { ll root1=find(x); ll root2=find(y); if(root1==root2) return 0ll; if(dsu_sz[x]>dsu_sz[y]){ dsu_arr[root2]=root1; dsu_sz[root1]+=dsu_sz[root2]; } else { dsu_sz[root2]+=dsu_sz[root1]; dsu_arr[root1]=root2; } return 1ll; } /* vector<ll>adj[100005]; bool vis[100005]; ll dist[100005]; void bfs(ll c) { vis[c]=true; dist[c]=0; queue<ll>q; q.push(c); while(!q.empty()) { ll x=q.front(); q.pop(); for(ll i=0;i<adj[x].size();i++) { ll y=adj[x][i]; if(!vis[y]) { vis[y]=true; dist[y]=dist[x]+1; q.push(y); } } } } */ int main() { Fast ll test; cin>>test; while(test--) { ll n; cin>>n; vector<ll>sura,mura,dp; for(ll i=0;i<n;i++) { ll k; cin>>k; if(i%2) sura.push_back(k); else{ mura.push_back(k); dp.push_back(0ll); } } ll ans=1000000000000000000,ans2=0; ll mn=100000000000; ll sum=0; for(ll i=0;i<mura.size();i++) { sum+=mura[i]; mn=min(mn,mura[i]); dp[i]=(sum-mn)+(mn*(n-i)); // cout<<dp[i]<< ; } dp.push_back(100000000000000000); // cout<<endl; mn=100000000000; sum=0; for(ll i=0;i<sura.size();i++) { sum+=sura[i]; mn=min(mn,sura[i]); ll x=(sum-mn)+(mn*(n-i)); ans=min(ans,min(dp[i],dp[i+1])+x); } cout<<ans<< n ; } }
#include <bits/stdc++.h> using namespace std; long long int n, m; long long int power(long long int a, long long int b) { if (b == 0) return 1; long long int sr = power(a, b >> 1); long long int r = sr * sr; if (b & 1) r = a * r; return r; } void solve() { long long int z = power(2, 0); cin >> n; vector<long long int> v(n); long long int ans = 0; long long int p = 1; for (long long int i = 0; i < n; i++) cin >> v[i]; sort(v.begin(), v.end()); long long int count = 0; for (long long int i = 0; i < n; i++) { if (v[i] >= z) { ans += (count * (count - 1) / 2); while (v[i] >= z) { z = power(2, p); p++; } count = 0; } count++; } ans += (count * (count - 1) / 2); cout << ans << n ; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int x; cin >> x; for (long long int i = 1; i <= x; i++) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 5e5 + 5; long long n, m; vector<vector<long long> > g(N, vector<long long>()); vector<long long> d(N, 0), last(N, -1); vector<long long> path; long long k = 0; vector<bool> in_path(N, 0); vector<pair<long long, long long> > pairs; long long p = 0; void dfs(long long v, long long l) { d[v] = l; if (last[l] != -1) { pairs.push_back({last[l] + 1, v + 1}); last[l] = -1; p++; } else { last[l] = v; } for (auto u : g[v]) { if (d[u] == 0) { dfs(u, l + 1); if (in_path[u]) { path.push_back(v + 1); k++; in_path[v] = 1; } } } if (d[v] >= (n + 1) / 2 && !in_path[v] && k == 0) { path.push_back(v + 1); k++; in_path[v] = 1; } } void solve() { cin >> n >> m; for (long long i = 0; i < m; ++i) { long long u, v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); } dfs(0, 1); if (k >= (n + 1) / 2) { cout << PATH n ; cout << k << n ; for (long long i = 0; i < k; ++i) { cout << path[i] << ; } cout << n ; } else { cout << PAIRING n ; cout << p << n ; for (long long i = 0; i < p; ++i) { cout << pairs[i].first << << pairs[i].second << n ; } } for (long long i = 0; i <= n; ++i) { g[i].clear(); d[i] = 0; last[i] = -1; in_path[i] = 0; } path.clear(); pairs.clear(); k = 0; p = 0; } signed main() { ios_base::sync_with_stdio(false); long long t; cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int read() { char ch = getchar(); int f = 0, x = 1; while (ch < 0 || ch > 9 ) { if (ch == - ) x = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) { f = (f << 1) + (f << 3) + ch - 0 ; ch = getchar(); } return f * x; } int ch[2000005][2], fa[2000005], w[2000005], mark[2000005]; int f[2000005], top, s[2000005], n, k, m; bool rev[2000005]; bool isroot(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; } void pushdown(int x) { if (rev[x]) { rev[ch[x][0]] ^= 1; rev[ch[x][1]] ^= 1; swap(ch[x][0], ch[x][1]); rev[x] = 0; } if (mark[x]) { mark[ch[x][0]] = min(mark[ch[x][0]], mark[x]); mark[ch[x][1]] = min(mark[ch[x][1]], mark[x]); w[ch[x][0]] = min(w[ch[x][0]], mark[x]); w[ch[x][1]] = min(w[ch[x][1]], mark[x]); mark[x] = 2000000000; } } void rotate(int x) { int y = fa[x], z = fa[y], l, r; if (ch[y][0] == x) l = 0; else l = 1; r = l ^ 1; if (!isroot(y)) { if (ch[z][0] == y) ch[z][0] = x; else ch[z][1] = x; } fa[y] = x; fa[x] = z; fa[ch[x][r]] = y; ch[y][l] = ch[x][r]; ch[x][r] = y; } void splay(int x) { top = 0; s[++top] = x; for (int i = x; !isroot(i); i = fa[i]) s[++top] = fa[i]; while (top) pushdown(s[top--]); while (!isroot(x)) { int y = fa[x], z = fa[y]; if (!isroot(y)) { if (ch[z][0] == y ^ ch[y][0] == x) rotate(x); else rotate(y); } rotate(x); } } void access(int x) { for (int t = 0; x; t = x, x = fa[x]) { splay(x); ch[x][1] = t; } } void makeroot(int x) { access(x); splay(x); rev[x] ^= 1; } void link(int x, int y) { makeroot(x); fa[x] = y; } int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); } void dfs(int x) { if (ch[x][0] && !isroot(ch[x][0])) { mark[ch[x][0]] = min(mark[ch[x][0]], mark[x]); w[ch[x][0]] = min(w[ch[x][0]], mark[x]); dfs(ch[x][0]); } if (ch[x][1] && !isroot(ch[x][1])) { mark[ch[x][1]] = min(mark[ch[x][1]], mark[x]); w[ch[x][1]] = min(w[ch[x][1]], mark[x]); dfs(ch[x][1]); } } int main() { memset(w, 0x7f7f7f7f, sizeof(w)); memset(mark, 0x7f7f7f7f, sizeof(mark)); n = read(); k = read(); m = read(); for (int i = 1; i <= n; i++) { f[i] = i; } for (int i = 1; i <= k; i++) { int u = read(), v = read(); link(u, n + i); link(n + i, v); f[find(u)] = find(v); } for (int i = 1; i <= m; i++) { int u = read(), v = read(), w1 = read(); int fu = find(u), fv = find(v); if (fu == fv) { makeroot(u); access(v); splay(v); mark[v] = min(mark[v], w1); w[v] = min(w[v], w1); } else { link(u, i + k + n); link(i + k + n, v); f[fu] = fv; } } long long ans = 0; for (int i = 1; i <= n + k + m; i++) { if (isroot(i)) { dfs(i); } } for (int i = 1; i <= k; i++) { if (w[i + n] >= 2000000000) { puts( -1 ); return 0; } ans += w[i + n]; } cout << ans; }
#include <bits/stdc++.h> using namespace std; bool check(string cards, int a, int b) { bool isValid = false; for (int j = 0; j < b && !isValid; j++) { int shift = 0; isValid = true; for (int i = 0; i < a; i++, shift += b) { if (cards[shift + j] != X ) { isValid = false; break; } } } return isValid; } int main() { int t; vector<string> ways; cin >> t; for (int i = 0; i < t; i++) { string cards; cin >> cards; if (check(cards, 1, 12)) ways.push_back( 1x12 ); if (check(cards, 2, 6)) ways.push_back( 2x6 ); if (check(cards, 3, 4)) ways.push_back( 3x4 ); if (check(cards, 4, 3)) ways.push_back( 4x3 ); if (check(cards, 6, 2)) ways.push_back( 6x2 ); if (check(cards, 12, 1)) ways.push_back( 12x1 ); cout << ways.size() << ; vector<string>::iterator itr = ways.begin(); for (; itr != ways.end(); itr++) cout << *itr << ; cout << endl; ways.clear(); } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int oo = 1e9 + 9; const long long inf = 1e18 + 18; const int max6 = 1e6 + 6; const int modx = 1e9 + 123; const int mody = 997; const int base = 137; int t[max6]; int n, k; bool ok(int l, int r) { int cnt = 0; for (int i = l; i <= r; ++i) cnt += t[i] == 1; if (cnt) return false; int cur = 0; for (int i = r + 1; i <= n; ++i) { if (t[i] == 2) cur++; else cur = 0; if (cur > k) return false; } for (int i = 1; i < l; ++i) { if (t[i] == 2) cur++; else cur = 0; if (cur > k) return false; } return true; } int main() { cin >> n >> k; string s; cin >> s; for (int i = 1; i <= n; ++i) if (s[i - 1] == Y ) t[i] = 1; else if (s[i - 1] == N ) t[i] = 2; else if (s[i - 1] == ? ) t[i] = 3; int res = 0; for (int i = 1; i <= n; ++i) if (i == 1 || t[i - 1] == 1 || t[i - 1] == 3) { int j = i + k - 1; if (j == n || t[j + 1] == 1 || t[j + 1] == 3) if (ok(i, j)) res = 1; } cout << (res ? YES : NO ); }
#include <bits/stdc++.h> using namespace std; template <class T> inline void read(T &x) { x = 0; char c = getchar(); int f = 1; while (!isdigit(c)) { if (c == - ) f = -1; c = getchar(); } while (isdigit(c)) { x = x * 10 + c - 0 ; c = getchar(); } x *= f; } template <class T> inline void umin(T &x, T y) { x = x < y ? x : y; } template <class T> inline void umax(T &x, T y) { x = x > y ? x : y; } inline unsigned int R() { static unsigned int seed = 416; return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13; } const int N = 266666; const double inf = 1e9, eps = 1e-6; int n, m, fa[N], top[N], dep[N], sz[N], son[N], dfn[N], idfn[N], num; vector<int> e[N]; double ans = inf, cur; struct dot { double x, y; dot(double X = 0, double Y = 0) { x = X; y = Y; } }; vector<pair<dot, dot> > mrk[N]; dot a[N], b[N]; int len; pair<double, int> ev[N]; bool cmp1(pair<double, int> a, pair<double, int> b) { return a.first != b.first ? a.first < b.first : a.second > b.second; } inline double gety(int i) { return a[i].x == b[i].x ? a[i].y : a[i].y + (cur - a[i].x) / (b[i].x - a[i].x) * (b[i].y - a[i].y); } struct node { int u; bool operator<(const node &a) const { return gety(u) != gety(a.u) ? gety(u) < gety(a.u) : u < a.u; } }; set<node> Set; dot operator-(dot a, dot b) { return dot(a.x - b.x, a.y - b.y); } double Cross(dot a, dot b) { return a.x * b.y - a.y * b.x; } bool isin(dot p, dot a, dot b) { return p.x >= min(a.x, b.x) - eps && p.x <= max(a.x, b.x) + eps && p.y >= min(a.y, b.y) - eps && p.y <= max(a.y, b.y) + eps; } double Abs(double x) { return x > 0 ? x : -x; } bool on_line(dot p, dot a, dot b) { return Abs(Cross(p - a, b - a)) <= eps; } double getlen(dot a) { return sqrt(a.x * a.x + a.y * a.y); } void print(int i) { printf( %lf %lf %lf %lf n , a[i].x, a[i].y, b[i].x, b[i].y); } double getans(int i, int j) { if (i == j) return inf; if (getlen(b[i] - a[i]) > getlen(b[j] - a[j])) swap(i, j); if (getlen(b[i] - a[i]) <= eps) { if (isin(a[i], a[j], b[j]) && on_line(a[i], a[j], b[j])) return a[i].x; return inf; } dot A = a[i], B = b[i], C = a[j], D = b[j], u = B - A, v = D - C; if (Abs(Cross(u, v)) <= eps) { if (Abs(gety(i) - gety(j)) <= eps && cur >= A.x - eps && cur <= B.x + eps && cur >= C.x - eps && cur <= D.x + eps) { return cur; } return inf; } double k = (Cross(D, v) - Cross(A, v)) / Cross(u, v); dot p(A.x + k * u.x, A.y + k * u.y); if (isin(p, A, B) && isin(p, C, D) && on_line(p, A, B) && on_line(p, C, D)) return A.x + k * u.x; return inf; } void upd(int i) { set<node>::iterator it = Set.lower_bound((node){i}); if (it != Set.end()) umin(ans, getans(i, (*it).u)); if (it != Set.begin()) it--, umin(ans, getans(i, (*it).u)); } void dfs1(int u, int Fa, int Dep) { fa[u] = Fa; dep[u] = Dep; sz[u] = 1; for (register int i = (0); i <= (((int)e[u].size()) - 1); i++) if (e[u][i] != Fa) { dfs1(e[u][i], u, Dep + 1), sz[u] += sz[e[u][i]]; if (sz[e[u][i]] > sz[son[u]]) son[u] = e[u][i]; } } void dfs2(int u, int Top) { top[u] = Top; dfn[u] = ++num; idfn[num] = u; if (son[u]) dfs2(son[u], Top); for (register int i = (0); i <= (((int)e[u].size()) - 1); i++) if (e[u][i] != fa[u] && e[u][i] != son[u]) dfs2(e[u][i], e[u][i]); } int getlca(int u, int v) { while (top[u] != top[v]) { if (dep[top[u]] < dep[top[v]]) swap(u, v); u = fa[top[u]]; } return dep[u] < dep[v] ? u : v; } int getpath(int u, int v, double t, int xs, double c) { while (top[u] != top[v]) { int x = fa[top[u]]; mrk[top[u]].push_back(make_pair( dot(t, dfn[u]), dot(t + xs * (dep[u] - dep[x]) / c, dfn[u] - (dep[u] - dep[x])))); t += xs * (dep[u] - dep[x]) / c; u = fa[top[u]]; } return u; } int main() { read(n); read(m); for (register int i = (1); i <= (n - 1); i++) { int u, v; read(u); read(v); e[u].push_back(v); e[v].push_back(u); } dfs1(1, 0, 0); dfs2(1, 1); for (register int i = (1); i <= (m); i++) { int T, C, u, v; read(T); read(C); read(u); read(v); double t = T, c = C; int lca = getlca(u, v); int ori = u; v = getpath(v, lca, t + (dep[u] + dep[v] - 2 * dep[lca]) * 1.0 / c, -1, c); u = getpath(u, lca, t, 1, c); t += (dep[ori] - dep[u]) * 1.0 / c; if (dep[u] >= dep[v]) mrk[top[u]].push_back(make_pair( dot(t, dfn[u]), dot(t + (dep[u] - dep[v]) * 1.0 / c, dfn[v]))); else mrk[top[u]].push_back(make_pair( dot(t, dfn[u]), dot(t + (dep[v] - dep[u]) * 1.0 / c, dfn[v]))); } for (register int t = (1); t <= (n); t++) if (((int)mrk[t].size())) { Set.clear(); len = 0; for (register int i = (0); i <= (((int)mrk[t].size()) - 1); i++) len++, a[len] = mrk[t][i].first, b[len] = mrk[t][i].second; for (register int i = (1); i <= (len); i++) if (a[i].x > b[i].x) swap(a[i], b[i]); for (register int i = (1); i <= (len); i++) ev[2 * i - 1] = make_pair(a[i].x, i), ev[2 * i] = make_pair(b[i].x, -i); sort(ev + 1, ev + len * 2 + 1, cmp1); for (register int i = (1); i <= (len * 2); i++) { if ((cur = ev[i].first) >= ans) break; if (ev[i].second > 0) { set<node>::iterator it = Set.insert((node){ev[i].second}).first, pre, nxt; pre = it; nxt = it; nxt++; if (it != Set.begin()) { pre--; umin(ans, getans((*pre).u, (*it).u)); } if (nxt != Set.end()) { umin(ans, getans((*nxt).u, (*it).u)); } } else { set<node>::iterator it = Set.find((node){-ev[i].second}), it1, it2; it1 = it, it2 = it; it2++; if (it != Set.begin() && it2 != Set.end()) { it1--; umin(ans, getans((*it1).u, (*it2).u)); } Set.erase(it); } } } if (ans < 1e8) printf( %.9lf n , ans); else puts( -1 ); return 0; }
#include <bits/stdc++.h> using namespace std; map<int, int> m; int main() { int a, t, n, i, j, k, l, mn = INT_MAX, mx = 0; cin >> n >> k; int cnt = 0; int ans = 0; for (int i = 0; i < n; i++) { cin >> a; mn = min(mn, a); m[a]++; } int p = mn; while (p < k) { ans++; mn = INT_MAX; for (int i = k - 1; i >= p; i--) { if (m[i] > 0) m[i]--, m[i + 1]++; if (m[i] > 0) mn = min(mn, i); if (m[i + 1] > 0) mn = min(mn, i + 1); } p = mn; } cout << ans << n ; }
#include <bits/stdc++.h> using namespace std; const long double PI = 3.141592653589793238; const double EPS = 1e-6; void solve() { long long r, g, b, w; cin >> r >> g >> b >> w; long long total = r + g + b + w; long long odd = 0; odd = r % 2 + g % 2 + b % 2 + w % 2; long long col = r && g && b; if (odd <= 1 || odd == 4 || (odd == 3 && col)) cout << YES << endl; else cout << NO << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long tc; cin >> tc; while (tc--) { solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { int t,n,k; string x= abc ; cin>>t; while(t--) { cin>>n>>k; string s,h; for(int i=0;i<n-k;i++) { s+=x[i%3]; } char c=x[(n-k)%3]; while(k--) { s+=c; } cout<<s<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, k; cin >> n >> k; string s = ; for (int i = 0;; i++) { bool flag = 1; for (int j = i * k; j < (i + 1) * k; j++) { s += ( a + j - i * k); if (j == n - 1) { flag = 0; break; } } if (flag == 0) break; } cout << s << endl; } }
#include <bits/stdc++.h> using namespace std; void start() {} void sober() { long long int n, x = 0, a = 0, b = 0, y = 0, sum = 0, k = 0, m = 0; cin >> n >> a >> b; string s; cin >> s; a--, b--; if (s[a] == s[b]) cout << 0 n ; else cout << 1 n ; } signed main() { start(); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t = 1; while (t--) { sober(); } }
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; int a[maxn]; unordered_map<int, int> b, c; unordered_map<int, int>::iterator it, it2; vector<int> ans; int main() { ios_base::sync_with_stdio(0); int n, m, p; cin >> n >> m >> p; int x; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < m; ++i) { cin >> x; it = b.find(x); if (it == b.end()) b.insert(make_pair(x, 1)); else ++it->second; } for (int q = 0; q < p; ++q) { int cur = 0; c.clear(); for (int i = q; i < n; i += p) { if (1ll * i >= 1ll * m * p) { it = b.find(a[i - m * p]); if (it != b.end()) { it2 = c.find(a[i - m * p]); if (it2->second <= it->second) --cur; --it2->second; } } it = b.find(a[i]); if (it == b.end()) continue; else { it2 = c.find(a[i]); if (it2 == c.end()) it2 = c.insert(make_pair(a[i], 0)).first; if (it2->second < it->second) ++cur; ++it2->second; } if (cur == m) ans.push_back(i - (m - 1) * p + 1); } } sort(ans.begin(), ans.end()); cout << ans.size() << n ; for (int i : ans) cout << i << ; cout << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int k; cin >> k; char s[200]; vector<int> __v; __v.assign(1000000, 127); cin >> s; int n; cin >> n; set<int> n_del[ z - a + 1]; set<int>::iterator pos_n_del[ z - a + 1]; for (int i = 0; i < n; i++) { char c; int nc; cin >> nc >> c; set<int>::iterator it = n_del[c - a ].begin(); if (n_del[c - a ].empty() || nc <= *it) { n_del[c - a ].insert(nc - 1); continue; } nc -= *it; int pos = 0; while (it != n_del[c - a ].end() && nc > 0) { set<int>::iterator it0 = it; it++; if (it != n_del[c - a ].end()) { nc -= *it - *it0 - 1; } } if (nc > 0) { it--; pos = *it + nc; } else pos = *it + nc - 1; n_del[c - a ].insert(pos); } for (int i = 0; i < z - a + 1; ++i) { pos_n_del[i] = n_del[i].begin(); } int tSums[ z - a + 1] = {0}; for (int i = 0, len = strlen(s), j = 0; i < len * k; ++i, ++j) { if (j == len) j = 0; if (pos_n_del[s[j] - a ] != n_del[s[j] - a ].end() && *pos_n_del[s[j] - a ] == tSums[s[j] - a ]) { pos_n_del[s[j] - a ]++; } else cout << s[j]; tSums[s[j] - a ]++; } cout << endl; cerr << __v[rand() % __v.size()]; }
#include <bits/stdc++.h> using namespace std; const int dx[4] = {0, 1, -1, 0}, dy[4] = {-1, 0, 0, 1}; int n, m; char g[60][60]; bool col[60][60][2][4]; bool tmp[60][60][2][4]; bool inrange(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; } void solve(int x, int y, int turn, int dir) { int nx = x + dx[dir], ny = y + dy[dir]; if (inrange(nx, ny) && g[nx][ny] == B && tmp[nx][ny][turn][dir] == false) { tmp[nx][ny][turn][dir] = tmp[nx][ny][1][dir] = true; solve(nx, ny, turn, dir); } if (turn == 1) return; for (int i = 0; i < 4; i++) { if (i == dir) continue; nx = x + dx[i], ny = y + dy[i]; if (inrange(nx, ny) && g[nx][ny] == B && tmp[nx][ny][1][i] == false) { tmp[nx][ny][1][i] = true; solve(nx, ny, 1, i); } } return; } bool check() { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (g[i][j] == B ) { bool ok = false; for (int x = 0; x < 2 && !ok; x++) for (int y = 0; y < 4 && !ok; y++) if (tmp[i][j][x][y]) ok = true; if (!ok) return false; } return true; } int main() { scanf( %d%d , &n, &m); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { cin >> g[i][j]; } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (g[i][j] == B ) { memset(tmp, false, sizeof(tmp)); for (int k = 0; k < 4; k++) tmp[i][j][0][k] = tmp[i][j][1][k] = true, solve(i, j, 0, k); if (!check()) { cout << NO << endl; return 0; } } cout << YES << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef string ss ; typedef pair<int, int> pi; typedef pair<ll,ll> pl; typedef vector<int> vi; typedef vector<ll> vl; #define mp make_pair #define pb push_back void solve(){ ll n,k=0,q=0; string s; cin >> n >> k ; ll x[n],y[n]; for(int i=0 ; i<n ; i++){ cin >> x[i] >> y[i]; } bool ok=false; for(int i=0 ; i<n ; i++){ for(int j=0 ; j<n ; j++){ if(abs(x[i]-x[j]) + abs(y[i]-y[j]) <= k){ q++; } if(q==n) ok=true; } q=0; } cout << (ok ? 1 n : -1 n ) ; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t=1; cin >> t; while(t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 17; int N, Q; int main() { ios_base::sync_with_stdio(false); cin >> Q; while (Q--) { cin >> N; long long mx; cin >> mx; long long diff = 0; while (--N) { long long x; cin >> x; mx = max(mx, x); diff = max(diff, mx - x); } long long ans = 0; while (diff) { ans++; diff >>= 1; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, p; string msg, s[100001], h = <3 ; int sub(int &i, string &w) { int k = 0; for (; k < w.length() && i < msg.length(); i++) { if (w[k] == msg[i]) { ++k; } } return k == w.length(); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> s[i]; } cin >> msg; int j = 0, o = 1; for (int i = 0; o && i < n; i++) { if (!sub(j, h)) { o = 0; } if (o && !sub(j, s[i])) { o = 0; } } if (!sub(j, h)) { o = 0; } if (o) { cout << yes << n ; } else { cout << no << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; const int OO = 1e9; const double EPS = 1e-9; const int MX = 1e6 + 5; int p = 1299709, n, m, u, v; vector<vector<int>> adj(MX); unsigned long long pPw[MX], M = 1000000009, Hash[MX]; int main() { ios::sync_with_stdio(false); cout.precision(10); cin.tie(0); pPw[0] = 1; for (int i = 1; i < MX; ++i) pPw[i] = p * pPw[i - 1]; cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> u >> v; for (int I = 0; I < 1; ++I) { Hash[u] += pPw[v]; Hash[v] += pPw[u]; } } map<unsigned long long, int> hashes; for (int i = 1; i <= n; ++i) { ++hashes[Hash[i]]; } unsigned long long ans = 0; for (auto& pr : hashes) { ans += pr.second * 1LL * (pr.second - 1) / 2; } hashes.clear(); for (int i = 1; i <= n; ++i) { ++hashes[Hash[i] + pPw[i]]; } for (auto& pr : hashes) { ans += pr.second * 1LL * (pr.second - 1) / 2; } cout << ans << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int a[200], ans[200]; int v[1000001]; int main() { int T, n; cin >> T; while (T--) { memset(v, 0, sizeof v); scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); sort(a + 1, a + n + 1); for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) v[a[j] - a[i]] = 1; } memset(ans, 0, sizeof ans); ans[1] = 1; int len = 2; for (int j = 1; j <= 1e6; j++) { int tag = 1; for (int k = 1; k < len; k++) { if (v[j - ans[k]]) { tag = 0; break; } } if (tag) ans[len++] = j; if (len > n) break; } int tag = 1; for (int i = 1; i <= n; i++) if (ans[i] == 0) tag = 0; if (tag) { puts( YES ); for (int i = 1; i <= n; i++) printf( %d%c , ans[i], i == n ? n : ); } else puts( NO ); } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> istream& operator>>(istream& is, vector<T>& v) { for (T& x : v) is >> x; return is; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { if (!v.empty()) { os << v.front(); for (int i = 1; i < v.size(); ++i) os << << v[i]; } return os; } const int N = 100010, L = 17; int n, ns, nt, cnt; int dist[N], ds[N], dt[N], ts[N], tt[N]; pair<int, int> cs[N], ct[N], c[N], de[N], des[N], det[N]; int ss[N], st[N]; bool vis[N]; vector<pair<int, int>> g[N]; int prs[N][L], prt[N][L]; bool tag; void dfs(int u) { vis[u] = true; for (const auto& pr : g[u]) { int v, w; tie(v, w) = pr; if (!vis[v]) { dist[v] = dist[u] + w; dfs(v); } } } pair<int, int> decomp(int u) { if (tag) for (int i = 1; i < L; ++i) prt[u][i] = prt[prt[u][i - 1]][i - 1]; else for (int i = 1; i < L; ++i) prs[u][i] = prs[prs[u][i - 1]][i - 1]; pair<int, int> ret(0, u); vis[u] = true; for (const auto& pr : g[u]) { int v, w; tie(v, w) = pr; if (!vis[v]) { if (tag) prt[v][0] = u; else prs[v][0] = u; dist[v] = dist[u] + w; auto cur = decomp(v); cur.first += w; if (cur > ret) { if (ret.first) c[++cnt] = ret; ret = cur; } else c[++cnt] = cur; } } return de[u] = ret; } int s, t; int query(int x, int y) { if (ds[x] > dt[x]) { if (y >= ns) return ss[ns]; if (ts[x] <= y) return ss[y]; int ret = ss[y] + ds[x] + des[x].first; for (int i = L - 1; i >= 0; --i) if (ts[prs[x][i]] > y) x = prs[x][i]; x = prs[x][0]; return ret - ds[x] - min(des[x].first, cs[y].first); } if (y >= nt) return st[nt]; if (tt[x] <= y) return st[y]; int ret = st[y] + dt[x] + det[x].first; for (int i = L - 1; i >= 0; --i) if (tt[prt[x][i]] > y) x = prt[x][i]; x = prs[x][0]; return ret - dt[x] - min(det[x].first, ct[y].first); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int q; cin >> n >> q; for (int rep = 1; rep < n; ++rep) { int u, v, w; cin >> u >> v >> w; g[u].emplace_back(v, w); g[v].emplace_back(u, w); } dfs(1); s = max_element(dist + 1, dist + n + 1) - dist; memset(vis, 0, sizeof(vis)); dist[s] = 0; auto diam = decomp(s); t = diam.second; c[++cnt] = diam; sort(c + 1, c + cnt + 1, greater<pair<int, int>>()); copy(c + 1, c + cnt + 1, cs + 1); copy(dist + 1, dist + n + 1, ds + 1); copy(de + 1, de + n + 1, des + 1); ns = cnt; cnt = 0; memset(vis, 0, sizeof(vis)); dist[t] = 0; tag = true; diam = decomp(t); c[++cnt] = diam; sort(c + 1, c + cnt + 1, greater<pair<int, int>>()); copy(c + 1, c + cnt + 1, ct + 1); copy(dist + 1, dist + n + 1, dt + 1); copy(de + 1, de + n + 1, det + 1); nt = cnt; for (int i = 1; i <= ns; ++i) ss[i] = ss[i - 1] + cs[i].first; for (int i = 1; i <= nt; ++i) st[i] = st[i - 1] + ct[i].first; fill(ts + 1, ts + n + 1, -1); ts[s] = 0; for (int i = 1; i <= ns; ++i) { int u = cs[i].second; while (ts[u] == -1) { ts[u] = i; u = prs[u][0]; } } fill(tt + 1, tt + n + 1, -1); tt[t] = 0; for (int i = 1; i <= nt; ++i) { int u = ct[i].second; while (tt[u] == -1) { tt[u] = i; u = prt[u][0]; } } int ans = 0; while (q--) { int x, y; cin >> x >> y; x = (x + ans - 1) % n + 1; y = (y + ans - 1) % n + 1; cout << (ans = query(x, y * 2 - 1)) << n ; } return 0; }
#include bits/stdc++.h using namespace std; string alpha[26] = { 1 0 0 1 0 , 1 1 0 2 0 , 2 0 0 1 1 , 2 1 0 1 2 , 1 1 0 1 1 , 2 1 0 2 1 , 2 2 0 2 2 , 1 2 0 2 1 , 1 1 0 1 1 , 1 2 0 1 2 , 1 0 1 2 0 , 1 1 1 3 0 , 2 0 1 2 1 , 2 1 1 2 2 , 1 1 1 2 1 , 2 1 1 3 1 , 2 2 1 3 2 , 1 2 1 3 1 , 1 1 1 2 1 , 1 2 1 2 2 , 1 0 2 2 1 , 1 1 2 3 1 , 1 2 1 1 3 , 2 0 2 2 2 , 2 1 2 2 3 , 1 1 2 2 2 }; int main() { int n; string s; cin >> n; cin.ignore(); unordered_map<string, char> m; for (int i = 25; i >= 0; i--) { m[alpha[i]] = a + i; } for (int i = 0; i < n; i++) { getline(cin, s); putchar(m[s]); } return 0; }
#include <bits/stdc++.h> using namespace std; char c[503][503]; vector<pair<int, int> > v[503][503]; int main() { int n, m, k; cin >> n >> m >> k; int s = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> c[i][j]; if (c[i][j] == . ) s++; } } pair<int, int> st; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (c[i][j] == . ) { st.first = i; st.second = j; if (c[i - 1][j] == c[i][j]) { v[i][j].push_back(make_pair(i - 1, j)); } if (c[i + 1][j] == c[i][j]) { v[i][j].push_back(make_pair(i + 1, j)); } if (c[i][j - 1] == c[i][j]) { v[i][j].push_back(make_pair(i, j - 1)); } if (c[i][j + 1] == c[i][j]) { v[i][j].push_back(make_pair(i, j + 1)); } } } } queue<pair<int, int> > q; q.push(st); int num = s - k; if (num) { c[st.first][st.second] = q ; num--; } while (!q.empty()) { pair<int, int> tp = q.front(); q.pop(); for (pair<int, int> kom : v[tp.first][tp.second]) { if (c[kom.first][kom.second] == . && num > 0) { c[kom.first][kom.second] = q ; num--; q.push(make_pair(kom.first, kom.second)); } } } for (int i = 1; i < n + 1; i++) { for (int j = 1; j <= m; j++) { if (c[i][j] == q ) cout << . ; else if (c[i][j] == . && k) cout << X ; else cout << c[i][j]; } cout << endl; } }
#include <bits/stdc++.h> using namespace std; const int N = 32, M = 2e5 + 10; int q[N], qz[M]; bool insert(int x) { for (int i = N - 1; i >= 0; --i) { if ((x >> i) & 1) { if (!q[i]) { q[i] = x; return true; } else x ^= q[i]; } } return false; } int main() { int n; scanf( %d , &n); for (int i = 1; i <= n; ++i) scanf( %d , &qz[i]); for (int i = 1; i <= n; ++i) qz[i] ^= qz[i - 1]; int ans = 0; if (!qz[n]) return printf( -1 n ), 0; for (int i = n; i >= 1; --i) { if (insert(qz[i])) ans++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100010; int n, a[N], b[N], c[N]; multiset<int> S; multiset<int>::iterator it; int main() { std::ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; i++) cin >> a[i], c[a[i]] = i; for (int i = 0; i < n; i++) cin >> b[i]; for (int i = 0; i < n; i++) { int t = i - c[b[i]]; S.insert(t); } vector<int> r; for (int i = 0; i < n; i++) { int t = 1000000; it = S.lower_bound(i); if (it != S.end()) t = min(t, *(it)-i); if (it != S.begin()) t = min(t, i - *(--it)); r.push_back(t); S.erase(S.find(i - c[b[i]])); S.insert(n - c[b[i]] + i); } for (int i = 0; i < r.size(); i++) cout << r[i] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); cin.tie(0); long long n, i, j; cin >> n; vector<long long> v(n / 2); for (i = 0; i < v.size(); i++) { cin >> v[i]; } sort(v.begin(), v.end()); long long mn = LLONG_MAX; j = 1; long long ans = 0; for (i = 0; i < v.size(); i++) { ans += abs(j - v[i]); j += 2; } mn = ans; reverse(v.begin(), v.end()); ans = 0; j = n; for (i = 0; i < v.size(); i++) { ans += abs(j - v[i]); j -= 2; } cout << min(mn, ans); return 0; }
#include <bits/stdc++.h> using namespace std; const unsigned long long hash1 = 201326611; const double eps = 1e-8; const long long INF = 0x3f3f3f3f3f3f3f3f; const int inf = 0x3f3f3f3f; const long long mod = 1e9 + 7; const int N = 255; const int M = 12; const int dif = 26; const double PI = acos(-1.0); inline void Mod(long long& x) { if (x >= mod) x -= mod; } void BinaryBitset(int n) { cout << bitset<sizeof(int) * 4>(n) << endl; } int n, m; long long c[N][N], m0[N], m1[N], dp[N][N]; void init(int n) { m0[0] = m1[0] = 1; for (int i = 1; i <= n; i++) { m0[i] = m0[i - 1] * m % mod; m1[i] = m1[i - 1] * (m - 1) % mod; } c[1][0] = 1; c[1][1] = 1; c[0][0] = 1; for (int i = 1; i <= n; i++) c[i][0] = 1, c[i][i] = 1; for (int i = 2; i <= n; i++) { for (int j = 1; j < n; j++) { c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod; } } } int main() { scanf( %d%d , &n, &m); init(n); for (int i = 1; i <= n; i++) dp[1][i] = c[n][i] * m1[n - i] % mod; for (int i = 2; i <= n; i++) { for (int j = 0; j <= n; j++) { for (int k = 0; k <= j; k++) { dp[i][j] = (dp[i][j] + c[n - k][j - k] * dp[i - 1][k] % mod * m1[n - j] % mod * m0[k] % mod) % mod; if (j == k) dp[i][j] = ((dp[i][j] - m1[n] * dp[i - 1][k] % mod + mod) % mod + mod) % mod; } } } printf( %lld n , dp[n][n]); }
#include <bits/stdc++.h> #pragma optimization_level 3 #pragma GCC optimize( Ofast ) #pragma GCC target( avx,avx2,fma ) using namespace std; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); int getChar() { static char buf[1 << 16]; static int pos = 0, size = 0; if (pos == size) { pos = 0; size = (int)fread(buf, sizeof(buf[0]), sizeof(buf) / sizeof(buf[0]), stdin); } return buf[pos++]; } inline int ni() { char c = getChar(); for (; c == n || c == ;) c = getChar(); int o = 0; for (; c != && c != n ; c = getChar()) o = o * 10 + c - 0 ; return o; } inline void putChar(int c) { static char buf[1 << 16]; static int pos = 0; if (c == EOF || pos == 1 << 16) { fwrite(buf, sizeof(buf[0]), pos, stdout); pos = 0; } if (c == EOF) return; buf[pos++] = c; } void printInt(int val) { char buf[7]; int pos = 0; if (val == 0) buf[pos++] = 48; for (; val; val /= 10) buf[pos++] = 48 + val % 10; for (pos--; pos >= 0;) putChar(buf[pos--]); putChar( n ); } const int G = 400002; int a, OPA = -1; vector<int> l[G]; int skok[G], o[G], zam[G], GR[G][2], mxd[G], mxp[G], dp[G][2], sz[G]; int CALC(int v, int D) { mxd[v] = 1; sz[v] = 1; int c1 = 0, c2 = 0; for (int h : l[v]) { int x = CALC(h, D + 1); mxd[v] = max(mxd[v], x + 1); sz[v] += sz[h]; if (x >= c1) c2 = c1, c1 = x; else if (x > c2) c2 = x; } mxp[v] = c1 + c2 + 1; return mxd[v]; } int eba = 1; void RIP_PAPA(int v, int p) { for (int q = 0; q < l[v].size(); q++) { if (l[v][q] == p) { l[v].erase(l[v].begin() + q); break; } } for (int h : l[v]) RIP_PAPA(h, v); } void rnm(int v, int p) { for (int h : l[v]) if (h != p) zam[h] = eba++; for (int h : l[v]) if (h != p) rnm(h, v); } int ST; int GA(int LEN) { if (o[LEN] != -1) return o[LEN]; int d1, d2, w, rg; for (; ST >= 0 && mxp[ST] < OPA;) ST--; for (int q = ST; q >= 0; q--) { dp[q][0] = 0; d1 = d2 = 0, w = GR[q][0], rg = GR[q][1]; for (; w <= rg; w++) { dp[q][0] += dp[w][0]; if (dp[w][1] >= d1) d2 = d1, d1 = dp[w][1]; else d2 = max(d2, dp[w][1]); } if (d1 + d2 + 1 < LEN) dp[q][1] = d1 + 1; else dp[q][0]++, dp[q][1] = 0; } return o[LEN] = dp[0][0]; } int main() { cin.tie(0); cout.tie(0); cin.sync_with_stdio(0); cout.sync_with_stdio(0); ; memset(o, -1, sizeof(o)); a = ni(); o[1] = a; ; ST = a - 1; for (int q = 1; q <= a; q++) skok[a / q]++; for (int &i : skok) i /= 2; vector<pair<int, int> > reb(a - 1); for (int q = 0; q < a - 1; q++) { int x = ni() - 1, y = ni() - 1; reb[q] = {x, y}; l[x].push_back(y); l[y].push_back(x); } zam[0] = 0; rnm(0, -1); for (int q = 0; q < a; q++) l[q].clear(); for (pair<int, int> p : reb) { l[zam[p.first]].push_back(zam[p.second]); l[zam[p.second]].push_back(zam[p.first]); } RIP_PAPA(0, -1); for (int q = 0; q < a; q++) { GR[q][0] = l[q].size() ? l[q][0] : a; GR[q][1] = l[q].size() ? l[q].back() : q; } CALC(0, 1); int diam = 0; for (int &i : mxp) diam = max(i, diam); o[diam] = 1; for (int q = diam + 1; q <= a; q++) o[q] = 0; int H = min(a - 1, (int)(sqrt(diam))), U = 1, I = 2; for (; I <= H; I++) { o[I] = GA(I); OPA = I / 2; } int tyt; for (; I <= a;) { OPA = I / 2; tyt = GA(I); int lf = I, rg = min(a + 1, lf + skok[tyt] + 1); if (tyt == 0) break; if (tyt == 1) lf = diam, rg = diam + 1; for (; lf + 1 < rg;) { int md = (lf + rg) / 2; if (GA(md) == tyt) lf = md; else rg = md; } for (; I < rg; I++) o[I] = tyt; } for (int q = 1; q <= a; q++) { printInt(max(0, o[q])); } putChar(EOF); }
#include <bits/stdc++.h> using namespace std; long long n, m, s, e, v, q; vector<long long> vs, ve; int32_t main() { ios::sync_with_stdio(0), cin.tie(0); cin >> n >> m >> s >> e >> v; vs.resize(s), ve.resize(e); for (auto& u : vs) { cin >> u; } for (auto& u : ve) { cin >> u; } cin >> q; while (q--) { long long x, y, a, b; cin >> x >> y >> a >> b; if (x == a) { cout << abs(y - b) << n ; } else { long long ans = 1e18, idx, es = (abs(x - a) + v - 1) / v; if (y > b) swap(y, b); idx = lower_bound(vs.begin(), vs.end(), y) - vs.begin(); if (idx < s) { ans = min(ans, abs(vs[idx] - y) + abs(vs[idx] - b) + abs(x - a)); } idx--; if (idx >= 0) { ans = min(ans, abs(vs[idx] - y) + abs(vs[idx] - b) + abs(x - a)); } idx = lower_bound(ve.begin(), ve.end(), y) - ve.begin(); if (idx < e) { ans = min(ans, abs(ve[idx] - y) + abs(ve[idx] - b) + es); } idx--; if (idx >= 0) { ans = min(ans, abs(ve[idx] - y) + abs(ve[idx] - b) + es); } cout << ans << n ; } } return 0; }
#include <bits/stdc++.h> using namespace std; long long w[1000000], w1[100000]; long long t[1000000]; pair<long long, long long> ans; long long a, b, c, n, m, q, k, x, c1, b1; long long poww(long long x, long long y) { if (x == 0) return 0; if (x == 1) return 1; if (y == 0) return 1; if (y == 1) return x; long long d = poww(x, y / 2); if (y % 2) return d * d * x; return d * d; } map<long long, long long> M; vector<long long> v; int main() { cin >> a >> b >> c; for (int i = 1; i <= 1000000; i++) { a *= 10; if (a / b == c) return cout << i, 0; a %= b; } cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s = ROYGBIV ; long long int n, i; cin >> n; for (i = 0; i < n - 3; i++) { cout << s[i % 4]; } cout << BIV ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, s; cin >> n >> s; int sum = 100; bool res = false; for (int i = 0; i < n; i++) { int temp1, temp2; cin >> temp1 >> temp2; if (temp1 < s) { res = true; if (temp2 != 0 && temp2 < sum) sum = temp2; } if (temp1 == s && temp2 == 0) res = true; } if (res) cout << 100 - sum; else cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; const int dx[8] = {0, -1, 0, 1, 1, -1, -1, 1}; const int dy[8] = {-1, 0, 1, 0, -1, -1, 1, 1}; const long long mod = 1000000007; const int base = 311; const int N = 6e5 + 5; map<long long, vector<pair<int, int>>> e; int n, m; long long a[N]; vector<int> dsk[N]; long long pw[N]; int dd[N]; int k; void dfs(int u, int &d) { dd[u] = 1; ++d; for (int v : dsk[u]) if (!dd[v]) dfs(v, d); } void gogo() { cin >> n >> m >> k; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= m; ++i) { int u, v; cin >> u >> v; e[a[u] ^ a[v]].push_back(make_pair(u, v)); } pw[0] = 1; for (int i = 1; i <= 6e5; ++i) pw[i] = pw[i - 1] * 2 % mod; long long ans = pw[k] * pw[n] % mod; for (auto it : e) { vector<pair<int, int>> tmp = it.second; for (pair<int, int> i : tmp) { dsk[i.first].push_back(i.second); dsk[i.second].push_back(i.first); } vector<int> cnt; for (pair<int, int> i : tmp) { int d = 0; if (!dd[i.first]) { dfs(i.first, d); cnt.push_back(d); } } int sum = 0; for (int i : cnt) sum += i; long long comp = n - sum + ((int)(cnt).size()); ans = (ans - pw[n] + pw[comp] + mod * mod) % mod; for (pair<int, int> i : tmp) { dd[i.first] = dd[i.second] = 0; dsk[i.first].clear(); dsk[i.second].clear(); } } cout << ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); if (fopen( sol .inp , r )) { freopen( sol .inp , r , stdin); freopen( sol .out , w , stdout); } gogo(); }
#include <bits/stdc++.h> using namespace std; int n; long long ans, X, Y, s[200010], w[30], t, A[131072], B[131072]; inline long long gcd(long long a, long long b) { for (long long c; b; a = b, b = c) c = a % b; return a; } int main() { scanf( %d%lld%lld , &n, &X, &Y); for (int i = 1; i <= n; ++i) scanf( %lld , s + i); if (Y % X) return 0 & puts( 0 ); long long D = Y; for (int i = 2; D > 1 && i <= 1000000; ++i) if (D % i == 0) for (w[++t] = i; D % i == 0; D /= i) ; if (D > 1) { long long Q = sqrt(D); if (Q * Q == D) w[++t] = Q; else for (int i = 1; i <= n; ++i) { if ((Q = gcd(D, s[i])) > 1 && Q < D) { w[++t] = Q; w[++t] = D / Q; break; } } w[++t] = D; } int a[30] = {0}, b[30] = {0}, T; for (int i = 1; i <= t; ++i) { for (D = X; D % w[i] == 0; D /= w[i]) ++a[i]; for (D = Y; D % w[i] == 0; D /= w[i]) ++b[i]; } for (int i = 1; i <= n; ++i) if (Y % s[i] == 0) { int c[30] = {0}, k = 0; D = s[i]; for (int j = 1; j <= t; ++j) if (D % w[j] == 0) for (; D % w[j] == 0; ++c[j]) D /= w[j]; for (int j = 1; j <= t; ++j) if (c[j] == b[j]) k ^= (1 << j - 1); ++B[k]; } int N = 1 << t; for (int i = 1; i <= N; i <<= 1) for (int j = i; j < N; ++j& i ? 0 : j |= i) B[j ^ i] += B[j]; for (int i = 1; i <= n; ++i) if (s[i] % X == 0) { int c[30] = {0}, k = (1 << t) - 1; D = s[i]; for (int j = 1; j <= t; ++j) if (D % w[j] == 0) for (; D % w[j] == 0; ++c[j]) D /= w[j]; for (int j = 1; j <= t; ++j) if (c[j] == a[j] || a[j] == b[j]) k ^= (1 << j - 1); ans += B[k]; } printf( %lld n , ans); }
#include <bits/stdc++.h> using namespace std; double x[1005], y[1005], z[1005], r[1005]; long long int n, m; int main() { double res = (1ll << (long long int)(60)), ax, ay, az, vx, vy, vz, R; cin >> ax >> ay >> az >> vx >> vy >> vz >> R >> n; for (long long int i = 0; i < ((long long int)(n)); i++) { cin >> x[0] >> y[0] >> z[0] >> r[0] >> m; for (long long int j = 0; j < ((long long int)(m)); j++) cin >> x[j + 1] >> y[j + 1] >> z[j + 1]; for (long long int j = 0; j < ((long long int)(m)); j++) x[j + 1] += x[0], y[j + 1] += y[0], z[j + 1] += z[0]; for (long long int j = 0; j < ((long long int)(m + 1)); j++) { double t = -(vx * (ax - x[j]) + vy * (ay - y[j]) + vz * (az - z[j])) / ((vx) * (vx) + (vy) * (vy) + (vz) * (vz)); if (t < 0) continue; if (((vx * t + ax - x[j]) * (vx * t + ax - x[j]) + (vy * t + ay - y[j]) * (vy * t + ay - y[j]) + (vz * t + az - z[j]) * (vz * t + az - z[j])) < (R + r[j]) * (R + r[j]) + 0.0000001) { double low = 0, high = t; for (long long int k = 0; k < ((long long int)(100)); k++) { double mid = (low + high) / 2.0; if (((vx * mid + ax - x[j]) * (vx * mid + ax - x[j]) + (vy * mid + ay - y[j]) * (vy * mid + ay - y[j]) + (vz * mid + az - z[j]) * (vz * mid + az - z[j])) < (R + r[j]) * (R + r[j])) high = mid; else low = mid; } res = min(res, low); } } } if (res == (1ll << (long long int)(60))) cout << -1 << endl; else printf( %0.20lf n , res); }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 20; vector<int> a[N]; vector<int> Q; int t, x, p, k; int solve(int n, int p) { int ret = 0; Q.clear(); Q.push_back(-1); vector<int>& f = a[p]; for (int i = 0; i < (int)f.size(); ++i) { int top = Q.size(); for (int j = 0; j < top; ++j) { Q.push_back(-1 * f[i] * Q[j]); } } for (int i = 1; i < (int)Q.size(); ++i) { ret += n / Q[i]; } return n - ret; } int main() { for (int i = 2; i <= 1000000; ++i) { if (a[i].size() > 0) continue; for (int j = i; j <= 1000000; j += i) { a[j].push_back(i); } } scanf( %d , &t); while (t--) { scanf( %d %d %d , &x, &p, &k); k += solve(x, p); int lb = 1, ub = 1e9, ans = x; while (lb <= ub) { int mid = (lb + ub) / 2; if (solve(mid, p) >= k) { ans = mid; ub = mid - 1; } else { lb = mid + 1; } } printf( %d n , ans); } }
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; } template <typename T> inline bool chkmax(T &a, T b) { return b > a ? a = b, 1 : 0; } inline int read() { int x(0), sgn(1); char ch(getchar()); for (; !isdigit(ch); ch = getchar()) if (ch == - ) sgn = -1; for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48); return x * sgn; } void File() {} int n, m; const int inf = 0x3f3f3f3f; template <int Maxn, int Maxm> struct Dinic { int Head[Maxn], Next[Maxm], cap[Maxm], to[Maxm], e; Dinic() { e = 1; } inline void add_edge(int u, int v, int flow) { to[++e] = v; Next[e] = Head[u]; Head[u] = e; cap[e] = flow; } inline void Add(int u, int v, int flow) { add_edge(u, v, flow); add_edge(v, u, 0); } int S, T, dis[Maxn]; bool Bfs() { queue<int> Q; Q.push(S); memset(dis, 0, sizeof(dis)); dis[S] = 1; while (!Q.empty()) { int u = Q.front(); Q.pop(); for (int i = Head[u], v = to[i]; i; v = to[i = Next[i]]) if (cap[i] && !dis[v]) dis[v] = dis[u] + 1, Q.push(v); } return dis[T]; } int cur[Maxn]; int Dfs(int u, int flow) { if (u == T || !flow) return flow; int res = 0, f; for (int &i = cur[u]; i; i = Next[i]) { int v = to[i]; if (dis[v] == dis[u] + 1 && (f = Dfs(v, min(flow, cap[i])))) { cap[i] -= f, cap[i ^ 1] += f, res += f; if (!(flow -= f)) break; } } if (!res) dis[u] = 0; return res; } int Run() { int sum_flow = 0; while (Bfs()) memcpy(cur, Head, sizeof(cur)), sum_flow += Dfs(S, inf); return sum_flow; } }; const int N = 5050; Dinic<(int)1e5, (int)4e5> D; int tot; int ans[N], Bef; template <int Maxn> struct Segment_Tree { int id[Maxn]; void Build(int o, int l, int r) { if (l == r) { id[o] = l; return; } id[o] = ++tot; int mid = (l + r) >> 1; Build(o << 1, l, mid); Build(o << 1 | 1, mid + 1, r); D.Add(id[o], id[o << 1], id[o << 1] <= m ? 1 : inf); D.Add(id[o], id[o << 1 | 1], id[o << 1 | 1] <= m ? 1 : inf); } inline void Connect(int o, int l, int r, int ql, int qr, int Id) { if (ql <= l && r <= qr) { D.Add(Id, id[o], 2); return; } int mid = (l + r) >> 1; if (ql <= mid) Connect(o << 1, l, mid, ql, qr, Id); if (qr > mid) Connect(o << 1 | 1, mid + 1, r, ql, qr, Id); } vector<int> find(int o, int l, int r) { if (l == r) return vector<int>(); vector<int> tmp; int mid = (l + r) >> 1; vector<int> ls = find(o << 1, l, mid), rs = find(o << 1 | 1, mid + 1, r); set_union((ls).begin(), (ls).end(), (rs).begin(), (rs).end(), inserter(tmp, tmp.end())); for (int i = D.Head[id[o]], v = D.to[i]; i; v = D.to[i = D.Next[i]]) if (v <= m && !D.cap[i]) tmp.push_back(v); for (int i = D.Head[id[o]], v = D.to[i]; i; v = D.to[i = D.Next[i]]) if (D.cap[i] && v > Bef) { int cur = tmp[tmp.size() - 1]; tmp.pop_back(); ans[cur] = v - Bef; } return tmp; } }; Segment_Tree<N << 2> T; vector<int> V1, V2, V3; int Ref[N], from[N], go[N]; void Get_Ans() { for (int u : V1) { for (int i = D.Head[Ref[u]], v = D.to[i]; i; v = D.to[i = D.Next[i]]) if (v <= m && D.cap[i ^ 1]) ans[v] = u; } T.find(1, 1, m); for (int u : V3) { int flow = D.cap[from[u]]; for (int i = D.Head[Ref[u]], v = D.to[i]; i; v = D.to[i = D.Next[i]]) if (v != D.S && !D.cap[i]) ans[v] = u; if (flow == 1) for (int i = D.Head[Ref[u]], v = D.to[i]; i; v = D.to[i = D.Next[i]]) if (v != D.S && D.cap[i]) { ans[v] = u; break; } } } int main() { File(); n = read(), m = read(); tot = m; T.Build(1, 1, m); Bef = tot; D.S = tot + n + 1; D.T = tot + n + 2; for (register int i = (1), iend = (int)(m); i <= iend; ++i) D.Add(i, D.T, 1), go[i] = D.e - 1; for (register int i = (1), iend = (int)(n); i <= iend; ++i) { int opt = read(); D.Add(D.S, Ref[i] = ++tot, 1 + (opt == 2)); from[i] = D.e - 1; if (opt == 0) { int k = read(); V1.push_back(i); while (k--) D.Add(tot, read(), 1); } else if (opt == 1) { int l = read(), r = read(); V1.push_back(i); T.Connect(1, 1, m, l, r, tot); } else { int a = read(), b = read(), c = read(); V3.push_back(i); D.Add(tot, a, 1); D.Add(tot, b, 1); D.Add(tot, c, 1); } } printf( %d n , D.Run()); Get_Ans(); for (register int i = (1), iend = (int)(m); i <= iend; ++i) if (ans[i]) printf( %d %d n , ans[i], i); return 0; }
#include <bits/stdc++.h> using namespace std; const double PI = 2 * acos(0.0); void solve(void) { string str; cin >> str; int one = 0, zero = 0; for (int i = 0; i < str.length(); i++) { if (str[i] == 1 ) { one++; while (i < str.length() && str[i] == 1 ) i++; i--; } else { zero++; while (i < str.length() && str[i] == 0 ) i++; i--; } } if (one && zero) { if (zero <= 1) cout << 1 << n ; else cout << 2 << n ; } else if (zero) cout << 1 << n ; else if (one) cout << 0 << n ; } int main(void) { ios::sync_with_stdio(0), cin.tie(0); int t; cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> inline string tostring(const T &x) { ostringstream os; os << x; return os.str(); } inline int toint(const string &s) { istringstream is(s); int x; is >> x; return x; } inline int todecimal(string s) { int a = 0; for (int i = 0; i < s.size(); i++) a = 2 * a + (s[i] - 0 ); return a; } inline string tobinary(int a) { string s; while (a != 0) { s = (char)(a % 2 + 0 ) + s; a >>= 1; } return s; } template <typename T> inline T sqr(T x) { return x * x; } template <typename T> T gcd(T a, T b) { return (b == 0) ? abs(a) : gcd(b, a % b); } inline int isvowel(char c) { if (c == a || c == e || c == i || c == o || c == u ) return 1; return 0; } inline int isprime(int a) { for (int i = 2; i * i <= a; i++) if (!(a % i)) return 0; return 1; } inline void inp(int &n) { n = 0; int ch = getchar(); int sign = 1; while (ch < 0 || ch > 9 ) { if (ch == - ) sign = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) n = (n << 3) + (n << 1) + ch - 0 , ch = getchar(); n = n * sign; } const int dx4[4] = {0, 0, 1, -1}; const int dy4[4] = {1, -1, 0, 0}; const int dx8[8] = {0, 0, 1, 1, 1, -1, -1, -1}; const int dy8[8] = {1, -1, 0, 1, -1, 1, 0, -1}; class node { public: int x, y, z; node(int a, int b, int c) { x = a; y = b; z = c; } }; bool operator<(const node &a, const node &b) { return a.x > b.x; } bool compare(const node &a, const node &b) { return a.x < b.x; } const int maxn = 66; int grid[maxn][maxn]; int n; void solve() { memset((grid), (0), sizeof(grid)); grid[0][0] = n; int moving = 1; while (moving) { moving = 0; for (int i = 0; i < maxn; i++) { for (int j = 0; j < maxn; j++) { int x = grid[i][j] / 4; grid[i + 1][j] += x; grid[i][j + 1] += x; if (i) grid[i - 1][j] += x * (i == 1 ? 2 : 1); if (j) grid[i][j - 1] += x * (j == 1 ? 2 : 1); grid[i][j] %= 4; if (x) moving = 1; } } } int q = 0; ; cin >> q; int x, y; for (int i = 0; i < q; i++) { cin >> x >> y; if (x < 0) x = -x; if (y < 0) y = -y; if (x >= maxn || y >= maxn) cout << 0 << endl; else cout << grid[x][y] << endl; } } void input() { cin >> n; } int main() { int testcase = 1; for (int i = 0; i < testcase; i++) { input(); solve(); } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( inline,Ofast , 3) #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize( Ofast ) #pragma GCC optimize( inline ) #pragma GCC optimize( -fgcse ) #pragma GCC optimize( -fgcse-lm ) #pragma GCC optimize( -fipa-sra ) #pragma GCC optimize( -ftree-pre ) #pragma GCC optimize( -ftree-vrp ) #pragma GCC optimize( -fpeephole2 ) #pragma GCC optimize( -ffast-math ) #pragma GCC optimize( -fsched-spec ) #pragma GCC optimize( unroll-loops ) #pragma GCC optimize( -falign-jumps ) #pragma GCC optimize( -falign-loops ) #pragma GCC optimize( -falign-labels ) #pragma GCC optimize( -fdevirtualize ) #pragma GCC optimize( -fcaller-saves ) #pragma GCC optimize( -fcrossjumping ) #pragma GCC optimize( -fthread-jumps ) #pragma GCC optimize( -funroll-loops ) #pragma GCC optimize( -fwhole-program ) #pragma GCC optimize( -freorder-blocks ) #pragma GCC optimize( -fschedule-insns ) #pragma GCC optimize( inline-functions ) #pragma GCC optimize( -fschedule-insns2 ) #pragma GCC optimize( -fstrict-aliasing ) #pragma GCC optimize( -fstrict-overflow ) #pragma GCC optimize( -falign-functions ) #pragma GCC optimize( -fcse-skip-blocks ) #pragma GCC optimize( -fcse-follow-jumps ) #pragma GCC optimize( -fsched-interblock ) #pragma GCC optimize( -fpartial-inlining ) #pragma GCC optimize( no-stack-protector ) #pragma GCC optimize( -freorder-functions ) #pragma GCC optimize( -findirect-inlining ) #pragma GCC optimize( inline-small-functions ) #pragma GCC optimize( -finline-small-functions ) #pragma GCC optimize( -ftree-switch-conversion ) #pragma GCC optimize( -foptimize-sibling-calls ) #pragma GCC optimize( -fexpensive-optimizations ) #pragma GCC optimize( -funsafe-loop-optimizations ) #pragma GCC optimize( inline-functions-called-once ) #pragma GCC optimize( -fdelete-null-pointer-checks ) using namespace std; long long n, m, q, fa[1000010], ans, l, r; struct lujing { long long u, v, c, id; } f[1000010]; inline long long read() { long long w = 0, e = 1; char ch = getchar(); while (!isdigit(ch)) { e = ch == - ? -1 : 1; ch = getchar(); } while (isdigit(ch)) { w = (w << 3) + (w << 1) + (ch ^ 48); ch = getchar(); } return w * e; } long long find(long long x) { return (fa[x] == x) ? x : (fa[x] = find(fa[x])); } void add(long long x, long long y) { long long xx = find(x), yy = find(y); if (xx != yy) fa[xx] = yy; } bool pd(long long x, long long y) { long long xx = find(x), yy = find(y); if (xx == yy) return true; return false; } bool cmp(lujing x, lujing y) { return x.c > y.c; } int main() { n = read(); m = read(); q = read(); for (register int i = 1; i <= m; ++i) f[i].u = read(), f[i].v = read(), f[i].c = read(), f[i].id = i; sort(f + 1, f + 1 + m, cmp); while (q--) { l = read(), r = read(); ans = -1; for (register int i = 1; i <= n * 2; ++i) fa[i] = i; for (register int i = 1; i <= m; ++i) { if (f[i].id >= l && f[i].id <= r) { if (pd(f[i].u, f[i].v)) { ans = f[i].c; break; } else { add(f[i].u + n, f[i].v); add(f[i].u, f[i].v + n); } } } cout << ans << endl; } return 0; }