QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#100958#5830. 树Scintilla0 3344ms605100kbC++143.1kb2023-04-28 20:42:142023-04-28 20:42:17

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-28 20:42:17]
  • 评测
  • 测评结果:0
  • 用时:3344ms
  • 内存:605100kb
  • [2023-04-28 20:42:14]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define rep(i, s, e) for (int i = s; i <= e; ++i)
#define drep(i, s, e) for (int i = s; i >= e; --i)
#define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout)
#define pv(a) cout << #a << " = " << a << endl
#define pa(a, l, r) cout << #a " : "; rep(_, l, r) cout << a[_] << ' '; cout << endl

const int N = 1e6 + 10;
const int M = 20;

int read() {
  int x = 0, f = 1; char c = getchar();
  for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = -1;
  for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48;
  return x * f;
}

int n, a[N], p[N], mson[N], dep[N], bfn[N], bn, rev[N], pre[N][M], cnt[N], down[N], lst[N], cur[N], son[N][M], f[N][M];
vector <int> e[N];

signed main() {
  n = read();
  rep(i, 1, n) a[i] = read();
  rep(i, 2, n) {
    p[i] = read();
    // cout << p[i] << ' ' << i << endl;
    e[p[i]].emplace_back(i);
    mson[p[i]] = i;
  }
  auto bfs = [&](int s = 1) {
    queue <int> q;
    q.push(s);
    while (q.size()) {
      int u = q.front(); q.pop();
      bfn[u] = ++ bn, rev[bfn[u]] = u;
      f[u][0] = f[cur[dep[u]]][0] + a[u];
      cnt[u] = cnt[cur[dep[u]]] + 1;
      rep(i, 0, M - 1) {
        pre[u][i] = pre[cur[dep[u]]][i] + (a[u] >> i & 1);
      }
      lst[u] = cur[dep[u]], cur[dep[u]] = u;
      for (int v : e[u]) {
        dep[v] = dep[u] + 1;
        q.push(v);
      }
    }
  } ;
  bfs();
  rep(u, 1, n) {
    son[u][0] = u;
    cnt[u] += cnt[p[u]];
    rep(i, 0, M - 1) pre[u][i] += pre[p[u]][i];
  }
  drep(u, n, 1) {
    down[u] = down[mson[u]] ? down[mson[u]] : u;
  }
  // pa(p, 1, n);
  // pa(mson, 1, n);
  // pa(lst, 1, n);
  // rep(u, 1, n) cout << f[u][0] << ' ';
  // cout << endl;
  // rep(u, 1, n) cout << pre[u][0] << ' ';
  // cout << endl;
  rep(i, 1, 19) drep(u, n, 1) {
    son[u][i] = son[mson[son[u][i - 1]]][i - 1];
    if (!son[u][i]) continue;
    f[u][i] = f[u][i - 1] + f[mson[son[u][i - 1]]][i - 1];
    int c = pre[son[u][i]][i - 1] - pre[son[u][i - 1]][i - 1];
    f[u][i] += (cnt[son[u][i]] - cnt[son[u][i - 1]] - 2 * c) << i - 1;
    // if (u == 1) cout << "i, u, c, f = " << i << ' ' << u << ' ' << c << ' ' << f[u][i] << endl;
  }
  for (int q = read(); q; -- q) {
    auto ask = [&](int u, int d) {
      if (!u) return 0ll;
      // cout << "----- ask u, d = " << u << ' ' << d << endl;
      d = min(d, dep[down[u]] - dep[u]);
      ++ d;
      int res = 0;
      drep(i, 19, 0) if (d >> i) {
        // cout << "u, i, f[u][i] = " << u << ' ' << i << ' ' << f[u][i] << endl;
        res += f[u][i], u = mson[son[u][i]], d ^= 1 << i;
        if (!u) break;
        int c = pre[down[u]][i] - pre[p[u]][i];
        res += (cnt[down[u]] - cnt[p[u]] - 2 * c) << i;
        // cout << "u, i, c, res = " << u << ' ' << i << ' ' << c << ' ' << res << endl;
      }
      return res;
    } ;
    int u = read(), d = read();
    int res = ask(u, d) - ask(lst[u], d);
    // pv(ask(u, d));
    // pv(ask(lst[u], d));
    printf("%lld\n", res);
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 9ms
memory: 31008kb

input:

2000
946347867 341162491 202762650 295215762 254064439 66267204 693162580 357612557 492940637 939526638 59775103 374919339 144042807 861369313 651043728 999024805 439554916 167782038 597475252 56704409 69846137 22185655 79439847 769194737 145071391 226046618 915359433 392527325 84982946 54584098 827...

output:

52316811279
-1804133329
-1032311970
10054691833
2759332204
-3111524244
3183900900
922104954
5892434731
7622694161
1832814618
1719999173
1183105284
2418952875
5872480867
742352347
21115425148
1458062161
-259385088
708763390
974312741
8298093315
-2686337046
13651677816
630835013
644887011
-1251706069
...

result:

wrong answer 1st numbers differ - expected: '31727996563', found: '52316811279'

Test #2:

score: 0
Wrong Answer
time: 91ms
memory: 84536kb

input:

99999
792064428 473106195 799314986 65440734 948726345 442414256 280245405 873012700 466192412 899092866 283555341 657824017 963883713 793944180 767444438 105576842 542107696 580140098 65321660 381184238 584604194 397414881 861590124 309323011 217641157 120832524 303744205 961590116 110259426 380351...

output:

1529721251
5789164435
1890036515
125427731
1953894159
15998798166
51167154769
586671581
3349448125
798898621
468901916
1620113250
25990778
1594379152
5312988480
3404424244
1721547881
4111735623
2485379044
3404424244
263989598
1350176621
3406531139
-1077201627
4142794387
-944976912
5853245640
-724770...

result:

wrong answer 1st numbers differ - expected: '2509771019', found: '1529721251'

Test #3:

score: 0
Wrong Answer
time: 3344ms
memory: 605020kb

input:

1000000
947727010 179844140 923847675 171881267 5552129 974443359 989307850 869400987 126992154 527448411 141137718 136124474 917813105 392020809 79012342 473860575 969007624 833563354 90169336 878445705 84352622 403307122 733751738 670851448 942399068 731541999 101293644 545785337 964751520 9168003...

output:

322288206991949
64144946607284
131622021461037
185914374900434
4835863616362
52498384514864
91107317148541
133549305860957
287851061406679
123171127467128
128592990938379
8701280183883
14021808260941
65044029326959
310510965687315
15605868051249
75072334425099
73630325382068
66821080529052
217424723...

result:

wrong answer 1st numbers differ - expected: '322288180595345', found: '322288206991949'

Test #4:

score: 0
Wrong Answer
time: 3205ms
memory: 605100kb

input:

1000000
264862971 751386013 921867736 711577153 262726588 565608444 975324815 440219681 107888226 928241413 729126923 283912914 86248857 896446999 12839598 651796991 139813366 105131395 341646170 839485925 939265720 844548518 102280410 457829889 8602879 737140565 17206920 974175632 535833885 8373832...

output:

1437301279725
97809355735725
338628474889116
233680202642926
7398782854524
271306310491595
20877499166572
90775318291878
2535173933607
66604551364413
211534435306253
84757466685500
17886217839734
132051691526218
1860377260424
46253769112453
83950948773188
257274546430432
240943174832345
118446636358...

result:

wrong answer 1st numbers differ - expected: '1437301063221', found: '1437301279725'

Test #5:

score: 0
Wrong Answer
time: 1435ms
memory: 589256kb

input:

1000000
978606419 773027389 111265179 979476504 280718851 476857501 751854950 579263969 848569548 781051974 31782627 533831186 812822170 111553645 297770650 331144396 676977983 2236128 258203325 75591120 676466973 60056446 494411414 286185093 92474576 173276071 535648669 87210101 355790411 880267291...

output:

2258826661
-532596236
1803221825
19115125215
10099675920
3667786823
13439907464
4901567648
1859550937
-4103518912
986063591
1579646947
8670706325
-3077422221
781099091
-1552864780
2392930135
1525496185
1620138021
-2929324097
5698332772
9617012778
2053841980
7648109611
49589751
499629803
11482363508
...

result:

wrong answer 2nd numbers differ - expected: '938786622', found: '-532596236'

Test #6:

score: 0
Wrong Answer
time: 1440ms
memory: 589412kb

input:

1000000
952470566 585754087 120174600 401525004 458588768 5487567 31210348 446333263 231409083 521960132 457721893 866842852 925207283 16805978 4706826 99640835 619272676 136536623 459247161 308807462 633687300 717271369 23906473 865522890 173799280 424309108 719410673 118906385 110627845 730629403 ...

output:

-8484529884
904057442
3574486883
30920858921
2362384018
976251948
-1272627612
43760580
2742454462
1892746322
5584847963
716198149
-9585198721
8468000792
1331415448
-1059003504
-1377580861
3104092
-13599858176
11895002813
-6113870131
-2174816941
425554649
3910401634
2133237262
426018723
-14866774216
...

result:

wrong answer 1st numbers differ - expected: '994051214', found: '-8484529884'

Test #7:

score: 0
Wrong Answer
time: 1359ms
memory: 589832kb

input:

1000000
732367509 105027907 958920212 886798715 102486738 813075884 301085392 242303497 979657287 944859684 307768 438158233 561755409 740706505 791145209 283862713 828081846 771569552 59044985 600549571 191330226 438693570 36976319 810654215 220068818 771875421 740642902 839964155 206129566 2065543...

output:

3718354583
401770479
7048649232
30777836909
6534909740
18158881919
1146711843
1211879407
2910590132
3921952225
1078061119
2140524944
-9756508198
11496763305
27050719749
-8850306304
5500013057
1085743066
7955775583
268315310
-8532624309
990736484
7141737641
1755569740
3147994153
25178730284
829776518...

result:

wrong answer 1st numbers differ - expected: '999908753', found: '3718354583'

Test #8:

score: 0
Wrong Answer
time: 1287ms
memory: 589648kb

input:

1000000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

output:

244
55
308
-50
2
51
0
132
173
-81
-12
-25
1
1
-3
0
3
3
-3
83
-24
0
0
6
0
42
-9
-55
-6
-52
-3
117
47
-15
45
3
10
11
37
-37
-31
103
24
-6
5
-5
82
3
4
13
27
9
12
-2
68
287
15
2
-86
-48
-404
1
13
6
-21
1
0
2
18
1
6
3
32
-5
-24
1
1
6
-13
11
18
180
-22
79
-22
0
0
74
-54
-72
7
14
0
-12
106
1
3
73
7
-5
-11
...

result:

wrong answer 1st numbers differ - expected: '3826404725', found: '244'

Test #9:

score: 0
Wrong Answer
time: 1324ms
memory: 589772kb

input:

1000000
465660691 982007525 816592310 377030959 572981469 679249520 86377999 709561525 940473306 35102782 886143915 792819787 903287397 264564177 857982095 91486434 217197704 123118964 383387342 820268798 497623987 255010796 607884194 848568529 38169627 197987657 421323589 664004905 485409127 696844...

output:

9716240361
7229021080
1638327460
7032230495
-1502030504
1384234655
-1794075098
4385262880
8825096713
3824336919
7509509980
-1829253452
1456723401
1800621907
8971111084
23475037
24196614025
245789356
-288598825
4503372779
2023996205
-620125207
3058253082
1556076353
5765719027
3001958902
492162979
-45...

result:

wrong answer 1st numbers differ - expected: '96094116015', found: '9716240361'

Test #10:

score: 0
Wrong Answer
time: 1365ms
memory: 590068kb

input:

1000000
665830082 788228483 245541444 289601309 641764988 150723484 925214020 557415731 310210969 379707835 517820381 883917428 134445288 775557009 444476671 89856268 655841087 888410254 37788122 694551869 563331754 488108584 839551943 415095075 445425438 35452604 562044723 640544531 146258096 66852...

output:

431856055
-423908495
12101684293
2994711969
3917474960
2204756674
-923488002
12334301620
1184478780
594507509
11366404895
34279945
-488937244
-1624691699
3273984800
-437490030
13085512887
4162775188
8001277188
-744952650
7212396434
2609908492
10584384696
8791645039
-451512904
1978036157
-8787573
679...

result:

wrong answer 1st numbers differ - expected: '431856043', found: '431856055'