QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#866340#9676. Ancestorslittle_one0 781ms209292kbC++144.0kb2025-01-22 14:36:252025-01-22 14:36:36

Judging History

This is the latest submission verdict.

  • [2025-01-22 14:36:36]
  • Judged
  • Verdict: 0
  • Time: 781ms
  • Memory: 209292kb
  • [2025-01-22 14:36:25]
  • Submitted

answer

#include <cstdio>
#include <vector>
#include <set>
#include <array>
#include <algorithm>
using namespace std;
int n,m,fa,rt,o1,o2,o3,siz[100005],dep[100005],mxdep[100005],hson[100005],sum[100005],ans[1000005];
vector <int> g[100005];
vector < array <int,6> > op,opl,opr;
set <int> s[100005];
int lowbit(int x){
    return x & -x;
}
void add(int pos,int x){
    if(!pos){
        sum[0] += x;
        return;
    }
    for(;pos <= n;pos += lowbit(pos)){
        sum[pos] += x;
    }
}
int qry(int pos){
    int ret = sum[0];
    for(;pos;pos ^= lowbit(pos)){
        ret += sum[pos];
    }
    return ret;
}
void dfs1(int pos){
    siz[pos] = 1;
    mxdep[pos] = dep[pos];
    for(int i = 0;i < g[pos].size();i++){
        dep[g[pos][i]] = dep[pos] + 1;
        dfs1(g[pos][i]);
        siz[pos] += siz[g[pos][i]];
        mxdep[pos] = max(mxdep[pos],mxdep[g[pos][i]]);
        if(siz[g[pos][i]] > siz[hson[pos]]){
            hson[pos] = g[pos][i];
        }
    }
}
void dfs2(int pos,int x){
    set <int>::iterator it = s[dep[pos]].lower_bound(pos);
    int it2 = 0;
    if(it != s[dep[pos]].begin()){
        it2 = *(--it);
        it++;
    }
    if(it != s[dep[pos]].end()){
        op.push_back((array <int,6>){*it,1,dep[pos] - x,it2,-1,0});
        op.push_back((array <int,6>){*it,1,dep[pos] - x,pos,1,0});
    }
    op.push_back((array <int,6>){pos,1,dep[pos] - x,it2,1,0});
    s[dep[pos]].insert(pos);
    for(int i = 0;i < g[pos].size();i++){
        dfs2(g[pos][i],x);
    }
}
void dfs3(int pos){
    for(int i = 0;i < g[pos].size();i++){
        if(g[pos][i] != hson[pos]){
            dfs3(g[pos][i]);
            for(int j = dep[g[pos][i]];j <= mxdep[g[pos][i]];j++){
                int lst = 0;
                for(set <int>::iterator k = s[j].begin();k != s[j].end();k++){
                    op.push_back((array <int,6>){*k,1,j - dep[pos],lst,-1,0});
                    lst = *k;
                }
                s[j].clear();
            }
        }
    }
    if(hson[pos]){
        dfs3(hson[pos]);
    }
    op.push_back((array <int,6>){pos,1,0,0,1,0});
    s[dep[pos]].insert(pos);
    for(int i = 0;i < g[pos].size();i++){
        if(g[pos][i] != hson[pos]){
            dfs2(g[pos][i],dep[pos]);
        }
    }
}
void solve(int l,int r,int pl,int pr){
    if(l == r){
        return;
    }
    int mid = (l + r) >> 1;
    for(int i = pl;i < pr;i++){
        if(op[i][2] <= mid){
            if(op[i][1] == 1){
                add(op[i][3],op[i][4]);
            }
            opl.push_back(op[i]);
        }
        else{
            if(op[i][1] == 2){
                ans[op[i][5]] += op[i][4] * qry(op[i][3]);
            }
            opr.push_back(op[i]);
        }
    }
    op.clear();
    for(int i = 0;i < opl.size();i++){
        if(opl[i][1] == 1){
            add(opl[i][3],-opl[i][4]);
        }
        op.push_back(opl[i]);
    }
    for(int i = 0;i < opr.size();i++){
        op.push_back(opr[i]);
    }
    int tmp1 = opl.size(),tmp2 = opr.size();
    opl.clear();
    opr.clear();
    solve(l,mid,pl,pl + tmp1);
    solve(mid + 1,r,pl + tmp1,pl + tmp1 + tmp2);
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i++){
        scanf("%d",&fa);
        if(fa){
            g[fa].push_back(i);
        }
        else{
            rt = i;
        }
    }
    dfs1(rt);
    dfs3(rt);
    for(int i = dep[rt];i <= mxdep[rt];i++){
        int lst = 0;
        for(set <int>::iterator j = s[i].begin();j != s[i].end();j++){
            op.push_back((array <int,6>){*j,1,i - dep[rt] + 1,lst,-1,0});
            lst = *j;
        }
    }
    for(int i = 1;i <= m;i++){
        scanf("%d%d%d",&o1,&o2,&o3);
        op.push_back((array <int,6>){o1 - 1,2,o3 + 1,o1 - 1,-1,i});
        op.push_back((array <int,6>){o2,2,o3 + 1,o1 - 1,1,i});
    }
    sort(op.begin(),op.end());
    solve(0,n + 1,0,op.size());
    for(int i = 1;i <= m;i++){
        printf("%d\n",ans[i]);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 11
Accepted
time: 0ms
memory: 14708kb

input:

7 5
3 1 0 5 3 5 1
1 3 1
5 7 2
1 5 1
4 7 1
4 7 2

output:

2
1
3
3
1

result:

ok 5 number(s): "2 1 3 3 1"

Test #2:

score: 0
Wrong Answer
time: 3ms
memory: 14120kb

input:

1000 1000
686 337 192 336 405 0 108 485 350 762 258 780 179 939 25 657 571 662 119 786 604 224 935 494 685 575 369 178 249 740 954 204 598 592 68 771 498 86 55 38 298 704 239 292 993 286 16 813 719 187 14 476 792 49 944 52 227 720 310 470 900 243 663 950 627 300 728 189 45 610 673 548 873 95 48 841 ...

output:

484
78
690
237
423
510
384
65
582
594
405
312
576
320
490
385
227
326
-6
298
160
223
228
141
154
133
67
83
69
31
6
16
56
-7
12
8
99
164
-47
178
232
-19
189
319
-39
131
235
401
360
356
560
629
618
378
674
700
727
758
-107
494
480
742
2
84
110
292
228
1
87
31
-31
166
84
51
122
113
589
142
415
-68
11
4...

result:

wrong answer 1st numbers differ - expected: '452', found: '484'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #30:

score: 0
Wrong Answer
time: 188ms
memory: 65812kb

input:

50000 200000
42574 43129 47328 17982 40521 6668 12729 32377 201 11940 8599 11734 18349 41045 26854 22540 9897 33419 7463 1243 47272 27135 49050 49111 22435 42539 39924 20272 5843 9308 45963 3283 31185 13692 38952 20583 15885 24802 4773 953 49907 28689 36942 23550 19449 8970 33340 31665 5407 46023 18...

output:

12529
117
12558
509
37221
36998
40037
30813
24787
24795
47251
42689
24297
823
24859
12554
25674
30356
12695
24700
38956
36209
23753
12670
3325
35336
24566
12934
24691
23580
13173
24559
23601
13124
3777
1148
40582
24649
35321
23531
39728
34409
40094
22614
24849
42344
44451
23151
23400
29455
43099
414...

result:

wrong answer 1st numbers differ - expected: '12045', found: '12529'

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Wrong Answer

Test #67:

score: 0
Wrong Answer
time: 781ms
memory: 209292kb

input:

100000 1000000
6457 23693 90928 23592 90440 75018 16865 3342 83718 16731 95103 31510 38719 27886 29093 41955 6596 46409 51839 10527 91993 61074 14405 34833 53674 42363 11490 43757 46191 6058 59164 96938 57858 40178 97523 84164 21582 72243 11267 47368 97058 6637 95208 60092 53943 16441 28363 64965 52...

output:

58294
29849
1386
-3217
-2584
-13728
47174
81764
7478
1793
60093
13140
-2733
5157
-4200
-17640
40092
-1805
67919
80033
4980
49318
-24135
-32018
1666
-26871
55245
30625
69330
49976
33914
-6466
12167
69556
23384
53212
11717
36587
31327
-15417
-10026
39539
-13665
34844
7995
-31102
-28589
8663
38806
7193...

result:

wrong answer 1st numbers differ - expected: '52956', found: '58294'

Subtask #6:

score: 0
Skipped

Dependency #1:

0%