#include<bits/stdc++.h> #define FIO cin.tie(0); ios::sync_with_stdio(false) #define all(x) (x).begin(), (x).end() #define fi first #define se second #define TEST #define TESTS int t = 1; cin >> t; while (t--)
#include<bits/stdc++.h> #define FIO cin.tie(0); ios::sync_with_stdio(false) #define all(x) (x).begin(), (x).end() #define fi first #define se second #define TEST #define TESTS int t = 1; cin >> t; while (t--)
#include<bits/stdc++.h> #define FIO cin.tie(0); ios::sync_with_stdio(false) #define all(x) (x).begin(), (x).end() #define fi first #define se second #define TEST #define TESTS int t = 1; cin >> t; while (t--)
#include<bits/stdc++.h> #define FIO cin.tie(0); ios::sync_with_stdio(false) #define all(x) (x).begin(), (x).end() #define fi first #define se second #define TEST #define TESTS int t = 1; cin >> t; while (t--)
voidsolve(){ cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i], t[i] = a[i]; sort(t + 1, t + n + 1); s = unique(t + 1, t + n + 1) - t - 1; for (int i = 1; i <= n; ++i) a[i] = lower_bound(t + 1, t + s + 1, a[i]) - t; for (int i = 1; i <= m; ++i) { auto& [l, r, id] = q[i]; cin >> l >> r; id = i; } int blen = sqrt(n); int bnum = (n + blen - 1) / blen; for (int i = 1; i <= n; ++i) bi[i] = (i - 1) / blen + 1; for (int i = 1; i <= bnum; ++i) br[i] = min(n, i * blen); sort(q + 1, q + m + 1, [&](auto x, auto y) { if (bi[x.l] != bi[y.l]) return bi[x.l] < bi[y.l]; return x.r < y.r; }); int l = 1, r = 0, last = 0; int ll, res, tmp; for (int i = 1; i <= m; ++i) { auto [ql, qr, id] = q[i]; if (bi[ql] == bi[qr]) { for (int i = ql; i <= qr; ++i) cnt2[a[i]]++; for (int i = ql; i <= qr; ++i) chkmax(ans[id], t[a[i]] * cnt2[a[i]]); for (int i = ql; i <= qr; ++i) cnt2[a[i]] = 0; continue; } if (bi[ql] != last) { while (r > br[bi[ql]]) del(a[r--]); while (l < br[bi[ql]] + 1) del(a[l++]); res = 0; last = bi[ql]; } while (r < qr) add(a[++r], res); ll = l, tmp = res; while (ll > ql) add(a[--ll], tmp); ans[id] = tmp; while (ll < l) del(a[ll++]); } for (int i = 1; i <= m; ++i) cout << ans[i] << "\n"; }
#include<bits/stdc++.h> #define FIO cin.tie(0); ios::sync_with_stdio(false) #define all(x) (x).begin(), (x).end() #define fi first #define se second #define TEST #define TESTS int t = 1; cin >> t; while (t--)
voidsolve(){ cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= m; ++i) { auto& [l, r, id] = q[i]; cin >> l >> r; id = i; } int blen = sqrt(n); int bnum = (n + blen - 1) / blen; for (int i = 1; i <= n; ++i) bi[i] = (i - 1) / blen + 1; for (int i = 1; i <= bnum; ++i) bl[i] = (i - 1) * blen + 1; sort(q + 1, q + 1 + m, [&](auto x, auto y) { if (bi[x.l] != bi[y.l]) return bi[x.l] < bi[y.l]; return x.r > y.r; }); int l = 1, r = n, last = 0; int ll, res{}, mex{}, tmp; for (int i = 1; i <= n; ++i) cnt[a[i]]++; while (cnt[mex]) mex++; for (int i = 1; i <= m; ++i) { auto [ql, qr, id] = q[i]; if (bi[ql] == bi[qr]) { for (int i = ql; i <= qr; ++i) cnt2[a[i]]++; while (cnt2[ans[id]]) ans[id]++; for (int i = ql; i <= qr; ++i) cnt2[a[i]] = 0; continue; } if (bi[ql] != last) { while (r < n) add(a[++r]); while (l < bl[bi[ql]]) del(a[l++], mex); res = mex; last = bi[ql]; } while (r > qr) del(a[r--], res); ll = l, tmp = res; while (ll < ql) del(a[ll++], tmp); ans[id] = tmp; while (ll > l) add(a[--ll]); } for (int i = 1; i <= m; ++i) cout << ans[i] << "\n"; }
#include<bits/stdc++.h> #define FIO cin.tie(0); ios::sync_with_stdio(false) #define all(x) (x).begin(), (x).end() #define fi first #define se second #define TEST #define TESTS int t = 1; cin >> t; while (t--)
usingnamespace std; using i64 = longlong; using u32 = unsigned; using u64 = unsignedlonglong; using pii = std::pair<int, int>;
inlinecharnc(){ staticchar buf[1 << 20], *p1, *p2; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++; } #ifndef ONLINE_JUDGE #define nc getchar #endif voidread(){} template<typename T, typename... T2> inlinevoidread(T &x, T2 &... oth){ x = 0; char c = nc(), up = c; while(!isdigit(c)) up = c, c = nc(); while(isdigit(c)) x = x * 10 + c - '0', c = nc(); up == '-' ? x = -x : 0; read(oth...); }
constexprint N = 2e5 + 10; constexprint LOG = 17; constexprint MOD = 998244353;
struct { int l, r, t, id; } q[N]; int v[N], w[N], c[N]; array<int, 2> up[N]; vector<int> e[N]; int id[N], ans[N]; int f[N], g[N], idx; int fa[N][LOG], d[N]; int n, m, tim, Q, cntq; int bi[N]; int vis[N], cnt[N]; int res;
voiddfs(int u){ id[f[u] = ++idx] = u; for (int v : e[u]) { if (v == fa[u][0]) continue; fa[v][0] = u; d[v] = d[u] + 1; dfs(v); } id[g[u] = ++idx] = u; }
intlca(int u, int v){ if (d[u] < d[v]) swap(u, v); for (int i = LOG - 1; i >= 0; --i) { if (d[fa[u][i]] >= d[v]) u = fa[u][i]; } if (u == v) return u; for (int i = LOG - 1; i >= 0; --i) if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i]; return fa[u][0]; }
voidadd(int x){ if (vis[x]) res -= v[c[x]] * w[cnt[c[x]]--]; else res += v[c[x]] * w[++cnt[c[x]]]; vis[x] ^= 1; }
voidsolve(){ read(n, m, Q); for (int i = 1; i <= m; ++i) read(v[i]); for (int i = 1; i <= n; ++i) read(w[i]); for (int i = 0; i < n - 1; ++i) { int u, v; read(u, v); e[u].push_back(v); e[v].push_back(u); } for (int i = 1; i <= n; ++i) read(c[i]); d[1] = 1; dfs(1); for (int i = 1; i < LOG; ++i) { for (int u = 1; u <= n; ++u) { fa[u][i] = fa[fa[u][i - 1]][i - 1]; } } while (Q--) { int op, x, y; read(op, x, y); if (op == 0) { up[++tim] = {x, y}; } else { if (f[x] > f[y]) swap(x, y); q[++cntq] = {lca(x, y) == x ? f[x] : g[x], f[y], tim, cntq}; } } int blen = max<int>(1, pow(2 * n, 2.0 / 3)); for (int i = 1; i <= 2 * n; ++i) bi[i] = (i - 1) / blen + 1; sort(q + 1, q + cntq + 1, [&](auto x, auto y) { if (bi[x.l] != bi[y.l]) return bi[x.l] < bi[y.l]; if (bi[x.r] != bi[y.r]) return bi[x.r] < bi[y.r]; return x.t < y.t; }); int l = 1, r = 0, t = 0; for (int i = 1; i <= cntq; ++i) { auto [ql, qr, qt, _id] = q[i]; while (l > ql) add(id[--l]); while (r < qr) add(id[++r]); while (l < ql) add(id[l++]); while (r > qr) add(id[r--]); while (t < qt) update(++t); while (t > qt) update(t--); int u = id[ql], v = id[qr]; int Lca = lca(u, v); if (u != Lca && v != Lca) { add(Lca); ans[_id] = res; add(Lca); } else { ans[_id] = res; } } for (int i = 1; i <= cntq; ++i) cout << ans[i] << "\n"; }
#include<bits/stdc++.h> #define FIO cin.tie(0); ios::sync_with_stdio(false) #define all(x) (x).begin(), (x).end() #define fi first #define se second #define TEST #define TESTS int t = 1; cin >> t; while (t--)
#include<bits/stdc++.h> #define FIO cin.tie(0); ios::sync_with_stdio(false) #define all(x) (x).begin(), (x).end() #define fi first #define se second #define TEST #define TESTS int t = 1; cin >> t; while (t--)
usingnamespace std; using i64 = longlong; using u32 = unsigned; using u64 = unsignedlonglong; using pii = std::pair<int, int>;
constexprint N = 1e5 + 10; constexprint MOD = 998244353;
int a[N], bi[N], bl[N], br[N], t[N], s; i64 pre[N], suf[N]; int cnt1[N], cnt2[N]; int tree[N]; int n, m; i64 ans[N]; vector<tuple<int, int, int, int>> e1[N], e2[N]; struct { int l, r, id; } q[N];
voidadd(int x, int v){ while (x <= s) { tree[x] += v; x += x & -x; } }
intquery(int x){ int res = 0; while (x) { res += tree[x]; x -= x & -x; } return res; }
voidmodify1(int v){ for (int i = 1; i < bi[v]; ++i) cnt1[i]++; for (int i = bl[bi[v]]; i < v; ++i) cnt2[i]++; }
voidmodify2(int v){ for (int i = bi[v] + 1; i <= bi[s]; ++i) cnt1[i]++; for (int i = br[bi[v]]; i > v; --i) cnt2[i]++; }
voidsolve(){ cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i], t[i] = a[i]; sort(t + 1, t + 1 + n); s = unique(t + 1, t + 1 + n) - t - 1; for (int i = 1; i <= n; ++i) a[i] = lower_bound(t + 1, t + s + 1, a[i]) - t; int blen = sqrt(n); int bnum = (n + blen - 1) / blen; for (int i = 1; i <= n; ++i) bi[i] = (i - 1) / blen + 1; for (int i = 1; i <= bnum; ++i) { bl[i] = (i - 1) * blen + 1; br[i] = min(i * blen, n); } for (int i = 1; i <= n; ++i) { pre[i] = pre[i - 1] + query(s) - query(a[i]); add(a[i], 1); } memset(tree, 0, sizeof tree); for (int i = n; i >= 1; --i) { suf[i] = suf[i + 1] + query(a[i] - 1); add(a[i], 1); } for (int i = 1; i <= m; ++i) { auto& [l, r, id] = q[i]; cin >> l >> r; id = i; } sort(q + 1, q + 1 + m, [&](auto x, auto y) { if (bi[x.l] != bi[y.l]) return bi[x.l] < bi[y.l]; return x.r < y.r; }); int l = 1, r = 0; for (int i = 1; i <= m; ++i) { auto [ql, qr, id] = q[i]; if (qr > r) { ans[id] += pre[qr] - pre[r]; e1[l - 1].emplace_back(r + 1, qr, id, -1); } elseif (qr < r) { ans[id] -= pre[r] - pre[qr]; e1[l - 1].emplace_back(qr + 1, r, id, 1); } r = qr; if (ql < l) { ans[id] += suf[ql] - suf[l]; e2[r + 1].emplace_back(ql, l - 1, id, -1); } elseif (ql > l) { ans[id] -= suf[l] - suf[ql]; e2[r + 1].emplace_back(l, ql - 1, id, 1); } l = ql; } for (int i = 1; i <= n; ++i) { modify1(a[i]); for (auto [l, r, id, op] : e1[i]) { for (int j = l; j <= r; ++j) { ans[id] += op * (cnt1[bi[a[j]]] + cnt2[a[j]]); } } } memset(cnt1, 0, sizeof cnt1); memset(cnt2, 0, sizeof cnt2); for (int i = n; i >= 1; --i) { modify2(a[i]); for (auto [l, r, id, op] : e2[i]) { for (int j = l; j <= r; ++j) { ans[id] += op * (cnt1[bi[a[j]]] + cnt2[a[j]]); } } } for (int i = 2; i <= m; ++i) ans[q[i].id] += ans[q[i - 1].id]; for (int i = 1; i <= m; ++i) cout << ans[i] << "\n"; }
#include<bits/stdc++.h> #define FIO cin.tie(0); ios::sync_with_stdio(false) #define all(x) (x).begin(), (x).end() #define fi first #define se second #define TEST #define TESTS int t = 1; cin >> t; while (t--)
#include<bits/stdc++.h> #define FIO cin.tie(0); ios::sync_with_stdio(false) #define all(x) (x).begin(), (x).end() #define fi first #define se second #define TEST #define TESTS int t = 1; cin >> t; while (t--)