結果
問題 | No.2258 The Jikka Tree |
ユーザー | Jaehyun Koo |
提出日時 | 2023-06-04 21:41:26 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 3,688 ms / 4,000 ms |
コード長 | 3,972 bytes |
コンパイル時間 | 2,730 ms |
コンパイル使用メモリ | 207,908 KB |
実行使用メモリ | 107,008 KB |
最終ジャッジ日時 | 2024-06-09 04:10:09 |
合計ジャッジ時間 | 89,475 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 5 ms
7,040 KB |
testcase_01 | AC | 6 ms
6,912 KB |
testcase_02 | AC | 7 ms
7,040 KB |
testcase_03 | AC | 26 ms
7,168 KB |
testcase_04 | AC | 21 ms
7,296 KB |
testcase_05 | AC | 30 ms
7,040 KB |
testcase_06 | AC | 31 ms
7,168 KB |
testcase_07 | AC | 31 ms
7,424 KB |
testcase_08 | AC | 21 ms
7,040 KB |
testcase_09 | AC | 52 ms
7,424 KB |
testcase_10 | AC | 42 ms
7,424 KB |
testcase_11 | AC | 41 ms
7,168 KB |
testcase_12 | AC | 15 ms
7,040 KB |
testcase_13 | AC | 17 ms
7,296 KB |
testcase_14 | AC | 23 ms
7,168 KB |
testcase_15 | AC | 26 ms
7,040 KB |
testcase_16 | AC | 17 ms
7,040 KB |
testcase_17 | AC | 27 ms
7,296 KB |
testcase_18 | AC | 21 ms
7,040 KB |
testcase_19 | AC | 22 ms
7,040 KB |
testcase_20 | AC | 147 ms
12,160 KB |
testcase_21 | AC | 271 ms
13,952 KB |
testcase_22 | AC | 297 ms
8,064 KB |
testcase_23 | AC | 259 ms
7,680 KB |
testcase_24 | AC | 196 ms
10,496 KB |
testcase_25 | AC | 592 ms
11,904 KB |
testcase_26 | AC | 748 ms
12,160 KB |
testcase_27 | AC | 613 ms
14,208 KB |
testcase_28 | AC | 833 ms
14,208 KB |
testcase_29 | AC | 865 ms
12,800 KB |
testcase_30 | AC | 832 ms
12,160 KB |
testcase_31 | AC | 401 ms
12,288 KB |
testcase_32 | AC | 308 ms
8,320 KB |
testcase_33 | AC | 425 ms
13,056 KB |
testcase_34 | AC | 2,947 ms
104,076 KB |
testcase_35 | AC | 829 ms
98,348 KB |
testcase_36 | AC | 1,400 ms
97,792 KB |
testcase_37 | AC | 1,435 ms
97,792 KB |
testcase_38 | AC | 1,358 ms
100,320 KB |
testcase_39 | AC | 2,424 ms
102,772 KB |
testcase_40 | AC | 602 ms
103,168 KB |
testcase_41 | AC | 3,442 ms
102,036 KB |
testcase_42 | AC | 3,607 ms
100,864 KB |
testcase_43 | AC | 2,875 ms
104,448 KB |
testcase_44 | AC | 579 ms
106,252 KB |
testcase_45 | AC | 1,317 ms
98,048 KB |
testcase_46 | AC | 1,510 ms
98,020 KB |
testcase_47 | AC | 1,412 ms
97,944 KB |
testcase_48 | AC | 659 ms
106,856 KB |
testcase_49 | AC | 766 ms
98,476 KB |
testcase_50 | AC | 1,331 ms
97,792 KB |
testcase_51 | AC | 1,493 ms
97,920 KB |
testcase_52 | AC | 1,602 ms
100,696 KB |
testcase_53 | AC | 2,343 ms
101,296 KB |
testcase_54 | AC | 2,522 ms
103,296 KB |
testcase_55 | AC | 3,688 ms
101,248 KB |
testcase_56 | AC | 3,451 ms
100,608 KB |
testcase_57 | AC | 2,060 ms
102,784 KB |
testcase_58 | AC | 655 ms
107,008 KB |
testcase_59 | AC | 2,032 ms
102,784 KB |
testcase_60 | AC | 867 ms
98,476 KB |
testcase_61 | AC | 1,505 ms
97,920 KB |
testcase_62 | AC | 1,513 ms
97,920 KB |
testcase_63 | AC | 1,145 ms
101,096 KB |
testcase_64 | AC | 1,179 ms
102,064 KB |
testcase_65 | AC | 2,816 ms
104,576 KB |
testcase_66 | AC | 3,229 ms
101,632 KB |
testcase_67 | AC | 3,220 ms
99,584 KB |
testcase_68 | AC | 927 ms
104,576 KB |
testcase_69 | AC | 860 ms
104,832 KB |
testcase_70 | AC | 1,047 ms
69,888 KB |
testcase_71 | AC | 298 ms
8,320 KB |
testcase_72 | AC | 145 ms
10,240 KB |
testcase_73 | AC | 146 ms
12,288 KB |
testcase_74 | AC | 180 ms
15,360 KB |
testcase_75 | AC | 373 ms
105,088 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using lint = long long; using pi = array<lint, 2>; #define sz(v) ((int)(v).size()) #define all(v) (v).begin(), (v).end() const int MAXN = 150005; const int MAXT = 4000000; struct node { int l, r; lint sum; int cnt; lint eval(lint k) { return sum + k * cnt; } } tree[MAXT]; int p; int newnode() { return ++p; } void pull(int p) { tree[p].sum = tree[tree[p].l].sum + tree[tree[p].r].sum; tree[p].cnt = tree[tree[p].l].cnt + tree[tree[p].r].cnt; } void init(int s, int e, int p) { if (s == e) { return; } int m = (s + e) / 2; tree[p].l = newnode(); tree[p].r = newnode(); init(s, m, tree[p].l); init(m + 1, e, tree[p].r); } void update(int pos, int s, int e, int r1, int r2, int v) { if (s == e) { tree[r2] = tree[r1]; tree[r2].sum += v; tree[r2].cnt += 1; return; } int m = (s + e) / 2; if (pos <= m) { tree[r2].l = newnode(); tree[r2].r = tree[r1].r; update(pos, s, m, tree[r1].l, tree[r2].l, v); } else { tree[r2].l = tree[r1].l; tree[r2].r = newnode(); update(pos, m + 1, e, tree[r1].r, tree[r2].r, v); } pull(r2); } vector<int> gph[MAXN]; int din[MAXN], dout[MAXN], par[18][MAXN], dep[MAXN], rev[MAXN], piv; void dfs(int x, int p) { din[x] = piv++; rev[din[x]] = x; for (auto &y : gph[x]) { if (y != p) { par[0][y] = x; dep[y] = dep[x] + 1; dfs(y, x); } } dout[x] = piv; } bool lessHalf(pi sum, lint tot) { if (sum[0] * 2 < tot) return true; if (sum[0] * 2 == tot && sum[1] == 0) return true; return false; } int get_med(int s, int e, int p1, int p2, int k, int t, pi curSum, lint tot) { if (s == e) return s; int m = (s + e) / 2; pi cmpSum = curSum; cmpSum[0] += tree[tree[p2].l].eval(k) - tree[tree[p1].l].eval(k); cmpSum[1] += (s <= t && t <= m ? 1 : 0); if (lessHalf(cmpSum, tot)) { return get_med(m + 1, e, tree[p1].r, tree[p2].r, k, t, cmpSum, tot); } return get_med(s, m, tree[p1].l, tree[p2].l, k, t, curSum, tot); } pi operator+(const pi &a, const pi &b) { return pi{a[0] + b[0], a[1] + b[1]}; } pi get_sum(int s, int e, int ps, int pe, int p1, int p2, int k, int t) { if (e < ps || pe < s) return pi{0, 0}; if (s <= ps && pe <= e) { lint sum = tree[p2].eval(k) - tree[p1].eval(k); return pi{sum, ps <= t && t <= pe}; } int pm = (ps + pe) / 2; return get_sum(s, e, ps, pm, tree[p1].l, tree[p2].l, k, t) + get_sum(s, e, pm + 1, pe, tree[p1].r, tree[p2].r, k, t); } int n; lint asum[MAXN]; vector<int> root; int query(int l, int r, int k, int d) { // cout << l << " " << r + 1 << " " << k << " " << d << "\n"; lint tot = asum[r + 1] - asum[l] + 1ll * (r - l + 1) * k; int v = get_med(0, n - 1, root[l], root[r + 1], k, din[d], pi{0, 0}, tot); v = rev[v]; auto sum = get_sum(din[v], dout[v] - 1, 0, n - 1, root[l], root[r + 1], k, din[d]); if (!lessHalf(sum, tot)) return v; for (int i = 17; i >= 0; i--) { if (dep[v] >= (1 >> i)) { int anc = par[i][v]; auto sum = get_sum(din[anc], dout[anc] - 1, 0, n - 1, root[l], root[r + 1], k, din[d]); if (lessHalf(sum, tot)) v = par[i][v]; } } return par[0][v]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; gph[u].push_back(v); gph[v].push_back(u); } dfs(0, -1); for (int i = 1; i < 18; i++) { for (int j = 0; j < n; j++) { par[i][j] = par[i - 1][par[i - 1][j]]; } } vector<int> a(n); for (auto &x : a) cin >> x; root.resize(n + 1); root[0] = newnode(); init(0, n - 1, root[0]); for (int i = 1; i <= n; i++) { root[i] = newnode(); update(din[i - 1], 0, n - 1, root[i - 1], root[i], a[i - 1]); asum[i] = asum[i - 1] + a[i - 1]; } int q; cin >> q; lint S = 0; while (q--) { lint a, b, z, d; cin >> a >> b >> z >> d; a += S; b += S * 2; z += (__int128)S * S % 150001; a %= n; b %= n; z %= 150001; if (a > b) swap(a, b); int ans = query(a, b, z, d); cout << ans << "\n"; S += ans; } }