signedmain(){ int n = read(); for (int i = 1; i <= n; ++i) a[i] = read(), s[i] = s[i - 1] ^ a[i]; for (int i = 1; i <= n; ++i) { for (int j = 0; j < 33; ++j) f[0][i][j] = f[0][i - 1][j], f[1][i][j] = f[1][i - 1][j]; int x = 0; for (int j = 0; a[i] >> j; ++j) if ((a[i] >> j) & 1) x = j + 1; f[bit(s[i - 1], x - 1)][i][x - 1]++; } for (int i = 1; i <= n; ++i) { int l = i, r = n; while (l < r) { int mid = (l + r + 1) >> 1; if (check(i, mid)) l = mid; else r = mid - 1; } nxt[i] = l; } for (int i = 1; i <= n; ++i) rt[i] = add(rt[i - 1], 1, n, i, nxt[i]); int m = read(); for (int i = 1, lst = 0; i <= m; ++i) { int l = (read() + lst) % n + 1, r = (read() + lst) % n + 1; if (l > r) swap(l, r); printf("%lld\n", lst = ask(rt[l - 1], rt[r], 1, n, l, r)); } return0; }
3.5 / 模拟赛 2
T1 【长沙2020年1月集训day10】黎曼几何
题目描述
玩一种类似汉诺塔的游戏,只不过围成了一圈,你只能顺时针走。分别求将 n 个盘子移动到 2 柱和 3 柱的最小步数,多组询问。
// Code By CloudySky #include<bits/stdc++.h> #define int long long namespace IO { inlineintread(){ int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + (c ^ 48); c = getchar(); } return x * f; } voidprint_n(int x){ if (x > 9) print_n(x / 10); putchar(x % 10 + '0'); } inlinevoidprint(int x, char s = '\n'){ if (x < 0) putchar('-'), x = -x; print_n(x), putchar(s); } } // namespace IO usingnamespace IO; constint Maxn = 5e3 + 10; usingnamespace std;
int a[Maxn], t[Maxn];
boolcmp(int a, int b){return a > b;}
intgcd(int a, int b){return !b ? a : gcd(b, a % b);}
intcalc(int l, int r){ int n = r - l + 1, mid = (n + 1) / 2, ans = 0; for (int i = l, L = mid, R = mid + 1; i <= r; i += 2, --L, ++R) { t[L] = a[i]; if(R <= n) t[R] = a[i + 1]; } for (int i = 1; i < n; ++i) ans += t[i] * t[i + 1]; return ans + t[n] * t[1]; }
signedmain(){ int n = read(); for (int i = 1; i <= n; ++i) a[i] = read(); sort(a + 1, a + n + 1, cmp); int m = read(); for (int i = 1; i <= m; ++i) { int k = read(), ans = 0; if (k == 0) for (int i = 1; i <= n; ++i) ans += a[i] * a[i]; else { int cnt = gcd(n, k), len = n / cnt; for (int j = 1; j <= n; j += len) ans += calc(j, j + len - 1); } print(ans); } return0; }
// Code By CloudySky #include<bits/stdc++.h> // #define int long long namespace IO { inlineintread(){ int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();} while (c >= '0' && c <= '9') {x = x * 10 + (c ^ 48); c = getchar();} return x * f; } inlineintread_n(){ int x = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); if (c >= '0' && c <= '9') x = (c ^ 48); return x; } voidprint_n(int x){ if (x > 9) print_n(x / 10); putchar(x % 10 + '0'); } inlinevoidprint(int x, char s = '\n'){ if (x < 0) putchar('-'), x = -x; print_n(x), putchar(s); } } // namespace IO usingnamespace IO; constint Maxn = 1e3 + 10; usingnamespace std;
int a[Maxn][Maxn], b[Maxn][Maxn], line[Maxn], col[Maxn]; int _a[Maxn][Maxn], _b[Maxn][Maxn], cl, cc; bool visl[Maxn], visc[Maxn];
boolwork(){ memset(visl, 0, sizeof(visl)); memset(visc, 0, sizeof(visc)); memset(line, 0, sizeof(line)); memset(col, 0, sizeof(col)); cl = cc = 0; int n = read(), m = read(); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) a[i][j] = read_n(); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) { b[i][j] = read_n(); if (b[i][j]) line[i]++, col[j]++; } int flag = 1; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) if (a[i][j] != b[i][j]) flag = 0; } if (flag) returntrue; for (int i = 1; i <= n; ++i) { if (line[i] > 1) { visl[i] = 1; for (int j = 1; j <= m; ++j) { if (b[i][j]) visc[j] = 1; } } } for (int j = 1; j <= m; ++j) { if (col[j] > 1) { visc[j] = 1; for (int i = 1; i <= n; ++i) { if (b[i][j]) visl[i] = 1; } } } for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) if (visl[i] && visc[j] && a[i][j] != b[i][j]) returnfalse; for (int i = 1; i <= n; ++i) { if (visl[i]) continue; ++cl, cc = 0; for (int j = 1; j <= m; ++j) if (!visc[j]) _a[cl][++cc] = a[i][j], _b[cl][cc] = b[i][j]; } flag = 1; for (int i = 1; i <= cl; ++i) for (int j = 1; j <= cc; ++j) flag &= _a[i][j]; if (flag) returnfalse; flag = 0; for (int i = 1; i <= cl; ++i) for (int j = 1; j <= cc; ++j) flag |= _b[i][j]; if (!flag) returnfalse; returntrue; }
signedmain(){ int t = read(); while (t--) printf("%s\n", work() ? "Yes" : "No"); return0; }
T2 找朋友
题目描述
N 个鱼缸排成一列,按照 1,2,⋯,N 的顺序编号。第 i 个鱼缸里水的高度为 Hi,保证 Hi<W。
现在放入 M 条鱼,第 i 条鱼被放入第 Pi 个鱼缸,同时它最多能跳 Ji 单位。鱼 i 能从鱼缸 a 跳到鱼缸 b 当且仅当 ∣a−b∣≤1 且 Ji>W−Ha 。
// Code By CloudySky #include<bits/stdc++.h> // #define int long long namespace IO { inlineintread(){ int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();} while (c >= '0' && c <= '9') x = x * 10 + (c ^ 48), c = getchar(); return x * f; } voidprint_n(int x){ if (x > 9) print_n(x / 10); putchar(x % 10 + '0'); } inlinevoidprint(int x, char s = '\n'){ if (x < 0) putchar('-'), x = -x; print_n(x), putchar(s); } } // namespace IO usingnamespace IO; voidopenfile(); constint Maxn = 1e4 + 10; usingnamespace std;
int h[Maxn], L[210], R[210], x[420], tot; int vis[420], f[420][420];
signedmain(){ openfile(); int n = read(), m = read(), w = read(); for (int i = 1; i <= n; ++i) h[i] = w - read(); for (int i = 1; i <= m; ++i) { int st = read(), t = read(), l = st, r = st; for (; l > 1 && t > h[l]; --l); for (; r < n && t > h[r]; ++r); L[i] = l, R[i] = r, x[++tot] = l, x[++tot] = r; } sort(x + 1, x + tot + 1), n = unique(x + 1, x + tot + 1) - x - 1; for (int i = 1; i <= m; ++i) L[i] = lower_bound(x + 1, x + n + 1, L[i]) - x, R[i] = lower_bound(x + 1, x + n + 1, R[i]) - x; for (int len = 1; len <= n; ++len) { for (int l = 1, r = len; r <= n; ++l, ++r) { for (int i = 1; i <= m; ++i) if (L[i] >= l && R[i] <= r) vis[L[i]]++, vis[R[i] + 1]--; for (int i = l; i <= r; ++i) vis[i] += vis[i - 1]; for (int k = l; k <= r; ++k) f[l][r] = max(f[l][r], f[l][k - 1] + f[k + 1][r] + vis[k] * (vis[k] - 1) / 2); for (int i = l; i <= r + 1; ++i) vis[i] = 0; } } print(f[1][n]); return0; }
// Code By CloudySky #include<bits/stdc++.h> // #define int long long namespace IO { inlineintread(){ int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();} while (c >= '0' && c <= '9') x = x * 10 + (c ^ 48), c = getchar(); return x * f; } voidprint_n(int x){ if (x > 9) print_n(x / 10); putchar(x % 10 + '0'); } inlinevoidprint(int x, char s = '\n'){ if (x < 0) putchar('-'), x = -x; print_n(x), putchar(s); } } // namespace IO usingnamespace IO; voidopenfile(); constint Maxn = 2e6 + 10; usingnamespace std;
int ac[Maxn][26], siz[Maxn], fail[Maxn], tot; int que[Maxn];
voidinsert(char* s, int _id){ int n = strlen(s), p = 0; for (int i = n - 1; i >= 0; --i) { if (!ac[p][s[i] - 'A']) ac[p][s[i] - 'A'] = ++tot; p = ac[p][s[i] - 'A']; } que[_id] = p; }
voidbuild(){ queue <int> q; for (int i = 0; i < 26; ++i) if (ac[0][i]) q.push(ac[0][i]); while (!q.empty()) { int p = q.front(); q.pop(); for (int i = 0; i < 26; ++i) { int np = ac[fail[p]][i]; if (ac[p][i]) fail[ac[p][i]] = np, q.push(ac[p][i]); else ac[p][i] = np; } } } structedge {int v, nxt;} e[Maxn]; int hd[Maxn], cnt; voidadd(int u, int v){e[++cnt] = {v, hd[u]}, hd[u] = cnt;} voiddfs(int x){ for (int i = hd[x]; i; i = e[i].nxt) dfs(e[i].v), siz[x] += siz[e[i].v]; }
signedmain(){ openfile(); int n = read(), m = read(); staticchar s[Maxn]; for (int i = 1, id; i <= n; ++i) scanf("%s", s), id = read(), ac[id][s[0] - 'A'] = ++tot, siz[tot]++; for (int i = 1; i <= m; ++i) scanf("%s", s), insert(s, i); build(); for (int i = 1; i <= tot; ++i) add(fail[i], i); dfs(0); for (int i = 1; i <= m; ++i) print(siz[que[i]]); return0; }
// Code By CloudySky #include<bits/stdc++.h> #define int long long namespace IO { inlineintread(){ int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();} while (c >= '0' && c <= '9') x = x * 10 + (c ^ 48), c = getchar(); return x * f; } voidprint_n(int x){ if (x > 9) print_n(x / 10); putchar(x % 10 + '0'); } inlinevoidprint(int x, char s = '\n'){ if (x < 0) putchar('-'), x = -x; print_n(x), putchar(s); } } // namespace IO usingnamespace IO; voidopenfile(); usingnamespace std;
intgcd(int a, int b){return !b ? a : gcd(b, a % b);}
voidwork(){ int n = read(), ans = 0, k; if (n & 1) returnprint(0); n /= 2; vector <int> inv; for (int i = 1; i * i < n; ++i) if (n % i == 0) inv.push_back(i), inv.push_back(n / i); k = sqrt(n); if (k * k == n) inv.push_back(k); for (int p : inv) { for (int i = 1; i * i <= p / 2; ++i) { k = sqrt(p - i * i); if (k * k == p - i * i && gcd(i, k) == 1) ans++; } } print(ans); }
signedmain(){ openfile(); int t= read(); while (t--) work(); return0; }
// Code By CloudySky #include<bits/stdc++.h> // #define int long long namespace IO { inlineintread(){ int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();} while (c >= '0' && c <= '9') x = x * 10 + (c ^ 48), c = getchar(); return x * f; } voidprint_n(int x){ if (x > 9) print_n(x / 10); putchar(x % 10 + '0'); } inlinevoidprint(int x, char s = '\n'){ if (x < 0) putchar('-'), x = -x; print_n(x), putchar(s); } } // namespace IO usingnamespace IO; voidopenfile(); constint Maxn = 3e3 + 10, inf = 0x3f3f3f3f; usingnamespace std;
int p[2000010], d[2000010];
voidprime(int n, int cnt = 0){ for (int i = 2; i <= n; ++i) { if (!d[i]) d[i] = i, p[++cnt] = i; for (int j = 1; j <= cnt; ++j) { if (p[j] * i > n || p[j] > d[i]) break; d[p[j] * i] = p[j]; } } }
structedge {int v, nxt, t;} e[Maxn * Maxn * 2]; int hd[Maxn], hhd[Maxn], cnt = 1; voidadd(int u, int v, int t){e[++cnt] = {v, hd[u], t}, hd[u] = cnt;}
int s, t, ans, dep[Maxn];
intdfs(int x, int flow){ if (!flow || x == t) return flow; int res = flow; for (int &i = hhd[x]; i && res; i = e[i].nxt) { int y = e[i].v; if (!e[i].t || dep[y] != dep[x] + 1) continue; int k = dfs(y, min(e[i].t, res)); e[i].t -= k, e[i ^ 1].t += k, res -= k; } return flow - res; }
boolbfs(){ memset(dep, 0, sizeof(dep)); queue <int> q; q.push(s), dep[s] = 1; while (!q.empty()) { int x = q.front(); q.pop(); for (int i = hd[x]; i; i = e[i].nxt) { int y = e[i].v; if (e[i].t && !dep[y]) dep[y] = dep[x] + 1, q.push(y); } } return dep[t]; }
voiddinic(){while(bfs()) memcpy(hhd, hd, sizeof(hhd)), ans += dfs(s, inf);}
int a[Maxn], deg[Maxn]; vector <int> l, r;
voidwork(int n){ l.clear(), r.clear(), s = n + 1, t = n + 2; int m = read(), tot = 0, mn = 0, ans1, c1, S = n + 3; for (int i = 1; i <= n; ++i) { a[i] = read(), tot += (a[i] == 1); if (a[i] & 1) l.push_back(i); else r.push_back(i); } cnt = 1, ans = 0; memset(hd, 0, sizeof(hd)), memset(deg, 0, sizeof(deg)); add(s, S, m), add(S, s, 0); for (int u : l) add(S, u, 1), add(u, S, 0); for (int v : r) add(v, t, 1), add(t, v, 0); for (int u : l) for (int v : r) if (d[a[u] + a[v]] == a[u] + a[v] && a[u] != 1) add(u, v, 1), add(v, u, 0); dinic(), ans1 = ans; for (int u : l) for (int v : r) if (d[a[u] + a[v]] == a[u] + a[v] && a[u] == 1) add(u, v, 1), add(v, u, 0); dinic(), c1 = ans - ans1; if (m + ans + (tot - c1) / 2 > 2 * m) returnprint(2 * m); for (int u = 1; u <= n; ++u) for (int v = u + 1; v <= n; ++v) if (d[a[u] + a[v]] == a[u] + a[v]) deg[u]++, deg[v]++; for (int i = 1; i <= n; ++i) if (deg[i] > 0) mn++; print(min(mn, m + ans + (tot - c1) / 2)); }
signedmain(){ openfile(), prime(2e6); int n; while (scanf("%d", &n) != EOF) work(n); return0; }
// Code By CloudySky #include<bits/stdc++.h> // #define int long long namespace IO { inlineintread(){ int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();} while (c >= '0' && c <= '9') x = x * 10 + (c ^ 48), c = getchar(); return x * f; } voidprint_n(int x){ if (x > 9) print_n(x / 10); putchar(x % 10 + '0'); } inlinevoidprint(int x, char s = '\n'){ if (x < 0) putchar('-'), x = -x; print_n(x), putchar(s); } } // namespace IO usingnamespace IO; voidopenfile(); constint Maxn = 5e6 + 10; usingnamespace std;
structedge {int v, nxt;} e[Maxn << 1]; int hd[Maxn], cnt; voidadd(int u, int v){e[++cnt] = {v, hd[u]}, hd[u] = cnt;} vector <pair <int, int> > g[Maxn]; vector <int> id;
int rt, siz[Maxn], mx[Maxn], vis[Maxn], dep[Maxn]; int a[Maxn], c[Maxn], tot;
intdfs(int x, int f, int mx = 0){ id.push_back(x); for (int i = hd[x]; i; i = e[i].nxt) { int y = e[i].v; if (y == f || vis[y]) continue; dep[y] = dep[x] + 1; mx = max(dfs(y, x) + 1, mx); } return mx; }
voidcalc(int x){ id.clear(), dep[x] = 0; int mx = dfs(x, 0); for (int i = 0; i <= mx; ++i) { c[i] = ++tot; if (i) g[c[i]].push_back({c[i - 1], 0}); } for (int y : id) { g[c[dep[y]]].push_back({y, 0}); int res = a[y] - dep[y]; if (res < 0) continue; g[y].push_back({c[min(mx, res)], 1}); } }
voidgetrt(int x, int f, int s){ siz[x] = 1, mx[x] = 0; for (int i = hd[x]; i; i = e[i].nxt) { int y = e[i].v; if (y == f || vis[y]) continue; getrt(y, x, s), siz[x] += siz[y]; mx[x] = max(mx[x], siz[y]); } mx[x] = max(mx[x], s - siz[x]); if (!rt || mx[x] < mx[rt]) rt = x; }
voidsolve(int x){ vis[x] = 1, calc(x); for (int i = hd[x]; i; i = e[i].nxt) { int y = e[i].v, s = siz[y]; if (vis[y]) continue; rt = 0, getrt(y, x, s), getrt(y = rt, x, s); solve(y); } }
int dis[Maxn];
voidbfs(int s){ memset(dis, 0x3f, sizeof(dis)); deque <int> q; q.push_back(s), dis[s] = 0; while (!q.empty()) { int x = q.front(); q.pop_front(); for (auto tmp : g[x]) { int y = tmp.first, t = tmp.second; if (dis[y] > dis[x] + t) { dis[y] = dis[x] + t; if (!t) q.push_front(y); else q.push_back(y); } } } }
signedmain(){ openfile(); int n = read(); tot = n; for (int i = 1; i <= n; ++i) a[i] = min(n, read()); for (int i = 1, u, v; i < n; ++i) u = read(), v = read(), add(u, v), add(v, u); rt = 0, getrt(1, 0, n), getrt(rt, 0, n); solve(rt), bfs(1); for (int i = 2; i <= n; ++i) print(dis[i], ' '); return0; }
// Code By CloudySky #include<bits/stdc++.h> // #define int long long namespace IO { inlinelonglongread(){ longlong x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();} while (c >= '0' && c <= '9') x = x * 10 + (c ^ 48), c = getchar(); return x * f; } voidprint_n(int x){ if (x > 9) print_n(x / 10); putchar(x % 10 + '0'); } inlinevoidprint(int x, char s = '\n'){ if (x < 0) putchar('-'), x = -x; print_n(x), putchar(s); } } // namespace IO usingnamespace IO; voidopenfile(); constint Maxn = 1e6 + 10, P = 1e9 + 7; usingnamespace std;
int mu[Maxn], p[Maxn], vis[Maxn], cnt;
voidprime(longlong n){ mu[1] = 1;int up = sqrt(n); for (int i = 2; 1ll * i * i <= n; ++i) { if (!vis[i]) vis[i] = i, p[++cnt] = i, mu[i] = -1; for (int j = 1; j <= cnt; ++j) { if (p[j] > vis[i] || p[j] * i > up) break; vis[p[j] * i] = i; if (i % p[j]) mu[i * p[j]] = -mu[i]; } } }
longlongs(longlong n){ longlong res = 0; for (longlong l = 1, r = 0; l <= n; l = r + 1) { r = n / (n / l); res = (res + (n / l) * (r - l + 1) % P) % P; } return res; }
signedmain(){ // openfile(); longlong n = read(), ans = 0; prime(n); for (longlong i = 1; i * i <= n; ++i) ans = (ans + (mu[i] * s(n / (i * i)) % P) + P) % P; print(ans); return0; }
voidgetrt(int x, int f, int S){ siz[x] = 1, mx[x] = 0; for (int i = hd[x]; i; i = e[i].nxt) { int y = e[i].v; if (y == f || vis[y]) continue; getrt(y, x, S), siz[x] += siz[y]; mx[x] = max(mx[x], siz[y]); } mx[x] = max(mx[x], S - siz[x]); if (!rt || mx[x] < mx[rt]) rt = x; }
voiddfs(int x, int f, int r, int dep){ F[x].push_back({r, dep}); for (int i = hd[x]; i; i = e[i].nxt) { int y = e[i].v; if (y == f || vis[y]) continue; dfs(y, x, r, dep + 1); } }
voidbuild(int x){ vis[x] = 1, dfs(x, 0, x, 0); for (int i = hd[x]; i; i = e[i].nxt) { int y = e[i].v, S = siz[y]; if (vis[y]) continue; rt = 0, getrt(y, 0, S), getrt(y = rt, 0, S), build(y); } }
intadd(int p, int l, int r, int v, int k){ int np = ++tot; St[np] = St[p]; if (l == r) returnmn(np) = k, np; int mid = (l + r) >> 1; if (v <= mid) ls(np) = add(ls(p), l, mid, v, k); elsers(np) = add(rs(p), mid + 1, r, v, k); returnmn(np) = min(mn(ls(np)), mn(rs(np))), np; }
intask(int p, int l, int r, int k){ if (l == r) returnmn(p); int mid = (l + r) >> 1; if (k <= mid) returnask(ls(p), l, mid, k); elsereturnask(rs(p), mid + 1, r, k); }
int tot2, T1[Maxn << 4], T2[Maxn], T3[Maxn];
voidchange(int pre, int nw, int x, int n){ int t = ask(T3[pre], 1, n, x); T2[nw] = T2[pre], T3[nw] = add(T3[pre], 1, n, x, t == inf ? 0 : inf); for (int i = F[x].size() - 1; i >= 0; --i) { int f = F[x][i].first, dis = F[x][i].second; int pr = ask(T2[pre], 1, n, f); if (pr == inf) pr = 0; T1[++tot2] = add(T1[pr], 1, n, x, t == inf ? dis : inf); T2[nw] = add(T2[nw], 1, n, f, tot2); } }
intquery(int nw, int x, int n){ int ans = inf; for (int i = F[x].size() - 1; i >= 0; --i) { int f = F[x][i].first, dis = F[x][i].second; int pr = ask(T2[nw], 1, n, f), res = 0; if (pr == inf) res = inf; else res = mn(T1[pr]); ans = min(ans, dis + res); } return ans; }
signedmain(){ openfile(); int Type = read(), n = read(); for (int i = 1, u, v; i < n; ++i) u = read(), v = read(), add(u, v), add(v, u); mn(0) = inf; getrt(1, 0, n), getrt(rt, 0, n), build(rt); int m = read(), t = 0, c = 0; for (int i = 1, lst = 0; i <= m; ++i) { int op = read(), x = read() ^ (lst * Type); if (op == 1) change(c, ++t, x, n), c = t; if (op == 2) print(lst = query(c, x, n)), lst = lst == inf ? 0 : lst; if (op == 3) c = x; } return0; }
// Code By CloudySky #include<bits/stdc++.h> // #define int long long namespace IO { inlineintread(){ int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();} while (c >= '0' && c <= '9') x = x * 10 + (c ^ 48), c = getchar(); return x * f; } voidprint_n(longlong x){ if (x > 9) print_n(x / 10); putchar(x % 10 + '0'); } inlinevoidprint(longlong x, char s = '\n'){ if (x < 0) putchar('-'), x = -x; print_n(x), putchar(s); } } // namespace IO usingnamespace IO; voidopenfile(); constint Maxn = 2e6 + 10, inf = 0x3f3f3f3f; usingnamespace std;
longlong v1[Maxn], v2[Maxn];
voidadd(int x, longlong v){ for (int i = x; i < Maxn; i += (i & -i)) v1[i] += v, v2[i] += x * v; }
longlongask(int x, longlong res = 0){ for (int i = x; i; i -= (i & -i)) res += (x + 1) * v1[i] - v2[i]; return res; }
longlong a[Maxn]; set <int> seg;
voidins(int x){add(x, 1), ++a[x]; if (a[x] == 1) seg.insert(x);}
voiddel(int x){add(x, -1), --a[x]; if (a[x] == 0) seg.erase(x);}
signedmain(){ openfile(); int n = read(); longlong ans = 0; for (int i = 1; i <= n; ++i) a[i] = read(); sort(a + 1, a + n + 1); a[0] = -inf; for (int i = n; i; --i) { a[i] -= a[i - 1]; if (a[i]) seg.insert(i); add(i, i == 1 ? a[i] + a[0] : a[i]); } seg.insert(n + 1); int m = read(); for (int i = 1; i <= m; ++i) { int k = read(); ans += ask(n) - ask(n - k + 1 - 1); if (a[n - k + 1]) del(n - k + 1); else { auto pos = seg.lower_bound(n - k + 1); int r = *pos, l = *(--pos), t = k - (n - r + 1); del(l), ins(l + t), del(r); } } print(ans); return0; }
// Code By CloudySky #include<bits/stdc++.h> // #define int long long namespace IO { inlineintread(){ int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();} while (c >= '0' && c <= '9') x = x * 10 + (c ^ 48), c = getchar(); return x * f; } voidprint_n(int x){ if (x > 9) print_n(x / 10); putchar(x % 10 + '0'); } inlinevoidprint(int x, char s = '\n'){ if (x < 0) putchar('-'), x = -x; print_n(x), putchar(s); } } // namespace IO usingnamespace IO; voidopenfile(); constint Maxn = 2e6 + 10; usingnamespace std;
structedge {int v, nxt;} e[Maxn << 1]; int hd[Maxn], cnt; voidadd(int u, int v){e[++cnt] = {v, hd[u]}, hd[u] = cnt;}
int rt, siz[Maxn], mx[Maxn];
voidgetrt(int x, int f, int S){ siz[x] = 1, mx[x] = 0; for (int i = hd[x]; i; i = e[i].nxt) { int y = e[i].v; if (y == f) continue; getrt(y, x, S), siz[x] += siz[y]; mx[x] = max(mx[x], siz[y]); } mx[x] = max(mx[x], S - siz[x]); if (!rt || mx[x] <= mx[rt]) rt = x; }
int col[Maxn], pos[Maxn], pre[Maxn];
voiddfs(int x, int f, int _col){ col[x] = _col; for (int i = hd[x]; i; i = e[i].nxt) { int y = e[i].v; if (y == f) continue; dfs(y, x, _col); } }
structson { int siz, id; booloperator < (const son y) {return siz == y.siz ? id < y.id : siz > y.siz;} };
vector <son> a;
signedmain(){ // openfile(); int n = read(); for (int i = 1, u, v; i < n; ++i) u = read(), v = read(), add(u, v), add(v, u); getrt(1, 0, n), getrt(rt, 0, n); col[rt] = -1; for (int i = hd[rt]; i; i = e[i].nxt) { int y = e[i].v; a.push_back({siz[y], y}), dfs(y, rt, y); } sort(a.begin(), a.end()); int t = 0; for (int i = 0; i < a.size(); ++i) { pos[a[i].id] = i, pre[i] = pre[i - 1] + a[i].siz; t += pre[i] < n / 2; } for (int i = 1; i <= n; ++i) { if (col[i] == -1) {print(0); continue;} if (pos[col[i]] <= t) { if (pre[t] - a[pos[col[i]]].siz + siz[i] >= n / 2) print(t); elseprint(t + 1); } else { if (pre[t - 1] + siz[i] >= n / 2) print(t); elseprint(t + 1); } } return0; }
// Code By CloudySky #include<bits/stdc++.h> // #define int long long namespace IO { inlineintread(){ int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();} while (c >= '0' && c <= '9') x = x * 10 + (c ^ 48), c = getchar(); return x * f; } voidprint_n(int x){ if (x > 9) print_n(x / 10); putchar(x % 10 + '0'); } inlinevoidprint(int x, char s = '\n'){ if (x < 0) putchar('-'), x = -x; print_n(x), putchar(s); } } // namespace IO usingnamespace IO; voidopenfile(); constint Maxn = 2e5 + 10; usingnamespace std;
namespace BIT { int v[Maxn]; voidadd(int x, int val){for (; x; x -= (x & -x)) v[x] = max(v[x], val);} intask(int x, int ans = 0) {for (; x < Maxn; x += (x & -x)) ans = max(ans, v[x]); return ans;} }
int sam[Maxn][2], fail[Maxn], len[Maxn], lst, tot, id[Maxn];
voidinsert(char c, int _id){ int p = lst, np = ++tot, ch = c - '0'; id[_id] = lst = np, len[np] = len[p] + 1; for (; p && !sam[p][ch]; p = fail[p]) sam[p][ch] = np; if (p == 0) return fail[np] = 1, void(); int q = sam[p][ch]; if (len[q] == len[p] + 1) return fail[np] = q, void(); int nq = ++tot; fail[nq] = fail[q], fail[q] = fail[np] = nq; memcpy(sam[nq], sam[q], sizeof(sam[nq])); len[nq] = len[p] + 1; for (; p && sam[p][ch] == q; p = fail[p]) sam[p][ch] = nq; }
voidSAM(int n){ len[0] = 0, tot = lst = 1; staticchar s[Maxn]; scanf("%s", s + 1); for (int i = 1; i <= n; ++i) insert(s[i], i); }
voidrotate(int x){ int y = fail[x], z = fail[y], c = rs(y) == x, w = ch(x, !c); if (!isroot(y)) ch(z, rs(z) == y) = x; ch(x, !c) = y, ch(y, c) = w; if (w) fail[w] = y; fail[y] = x, fail[x] = z; }
voidsplay(int x){ spread(x); while (!isroot(x)) { int y = fail[x], z = fail[y]; if (!isroot(y)) rotate((rs(y) == x) ^ (rs(z) == y) ? x : y); rotate(x); } }
voidaccess(int x, int k, int y = 0){ for (; x; x = fail[y = x]) splay(x), BIT::add(v(x), len[x]), rs(x) = y; v(y) = k; }
int ans[Maxn]; vector <pair<int, int> > q[Maxn];
signedmain(){ openfile(); int n = read(), m = read(); SAM(n); for (int i = 1, l, r; i <= m; ++i) l = read(), r = read(), q[r].push_back({l, i}); for (int i = 1; i <= n; ++i) { access(id[i], i); for (auto x : q[i]) ans[x.second] = BIT::ask(x.first); } for (int i = 1; i <= m; ++i) print(ans[i]); return0; }
// Code By CloudySky #include<bits/stdc++.h> // #define int long long namespace IO { inlineintread(){ int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + (c ^ 48); c = getchar(); } return x * f; } voidprint_n(int x){ if (x > 9) print_n(x / 10); putchar(x % 10 + '0'); } inlinevoidprint(int x, char s = '\n'){ if (x < 0) putchar('-'), x = -x; print_n(x), putchar(s); } } // namespace IO usingnamespace IO; constint Maxn = 110; usingnamespace std;
vector <int> s[Maxn]; int tot, a[Maxn], n, vis[Maxn], ans[Maxn];
voidlink(int x){ s[++tot].clear(); for (int i = x; !vis[i]; i = a[i]) s[tot].push_back(i), vis[i] = 1; if (s[tot].size() == 2) ans[s[tot][0]] = 1, ans[s[tot][1]] = -1, --tot; }
voiddfs(int x){ if (x == tot + 1) { for (int i = 1, del = 0; i <= n; ++i) { del += ans[i]; if (del < 0) return; } for (int i = 1; i <= n; ++i) putchar(ans[i] == 1 ? '(' : ')'); exit(0); } for (unsigned i = 0; i < s[x].size(); i += 2) ans[s[x][i]] = 1, ans[s[x][i + 1]] = -1; dfs(x + 1); for (unsigned i = 0; i < s[x].size(); i += 2) ans[s[x][i]] = -1, ans[s[x][i + 1]] = 1; dfs(x + 1); }
signedmain(){ n = read(); for (int i = 1; i <= n; ++i) a[i] = read(); for (int i = 1; i <= n; ++i) if (!vis[i]) link(i); dfs(1); return0; }