QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#873380#9676. Ancestorsxcs3210 485ms192728kbC++145.8kb2025-01-26 12:49:162025-01-26 12:49:27

Judging History

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

  • [2025-01-26 12:49:27]
  • 评测
  • 测评结果:0
  • 用时:485ms
  • 内存:192728kb
  • [2025-01-26 12:49:16]
  • 提交

answer

#include<bits/stdc++.h>
#define N 2000005
#define M 10000006
#define il inline
#pragma GCC optimize(3,"Ofast","inline")
using namespace std;

#define getchar() (p1 == p2 && (p2 = (p1 = buf1) + fread(buf1, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf1[1 << 23], *p1 = buf1, *p2 = buf1, ubuf[1 << 23], *u = ubuf;
namespace IO
{
	template<typename T>
	void read(T &_x){_x=0;int _f=1;char ch=getchar();while(!isdigit(ch)) _f=(ch=='-'?-1:_f),ch=getchar();while(isdigit(ch)) _x=_x*10+(ch^48),ch=getchar();_x*=_f;}
	template<typename T,typename... Args>
	void read(T &_x,Args&...others){Read(_x);Read(others...);}
	const int BUF=20000000;char buf[BUF],to,stk[32];int plen;
	#define pc(x) buf[plen++]=x
	#define flush(); fwrite(buf,1,plen,stdout),plen=0;
	template<typename T>inline void print(T x){if(!x){pc(48);return;}if(x<0) x=-x,pc('-');for(;x;x/=10) stk[++to]=48+x%10;while(to) pc(stk[to--]);}
}
using namespace IO;

int n, m, B1, B2, root;
vector<int>p[N];
int dep[N], ans[N], dfn[N], k[N], len, tot, n1, n2;
struct RMQ_LCA { //O(n log n) - O(1) LCA
    int z[N], r[N], lg[N], tot;
    int f[N][22];
    void dfs(int x, int fa) {
        dep[x] = dep[fa] + 1;
        r[++tot] = x;
        dfn[x] = ++len;
        z[x] = tot;
        for(auto y : p[x]) {
            dfs(y, x);
            r[++tot] = x;
        }
    }
    int Min(int x, int y) {
        return dep[x] < dep[y] ? x : y;
    }
    il int LCA(int x, int y) {
        x = z[x], y = z[y];
        if(x > y) swap(x, y);
        int h = lg[y - x + 1];
        return Min(f[x][h], f[y - (1 << h) + 1][h]);
    }
    void init() {
        dfs(root, 0);
        for(int i = 2; i <= tot; i++) lg[i] = lg[i / 2] + 1;
        for(int i = 1; i <= tot; i++) f[i][0] = r[i];
        for(int j = 1; j <= 20; j++) 
        for(int i = 1; i + (1 << j) - 1 <= tot; i++) f[i][j] = Min(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
    }
}ljm;

int tim1, tim2;
struct DS { //分块:O(1)修改, O(sqrt(n))查询
    int v1[N], v2[N];
    int X[M], Y[M];
    il void update(int x, int y) {
        X[++tim1] = x; Y[tim1] = y;
        v1[k[x]] += y;
        v2[x] += y;
    }
    il int query(int L, int R) {
    	if(L > R) return 0;
    	int res = 0;
    	if(k[L] == k[R]) {
    		for(int i = L; i <= R; i++) res += k[i];
    		return res;
		}
        int rr = B2 * k[L];
        for(int i = L; i <= rr; i++) res += v2[i];
        for(int i = k[L] + 1; i <= k[R]; i++) res += v1[i];
        return res;
    }
    il void Go(int x) { //撤销回到时间轴为x时
        while(tim1 > x) {
            v1[k[X[tim1]]] -= Y[tim1];
            v2[X[tim1]] -= Y[tim1];
            tim1--;
        }
    }
}ds;

struct List {
    int Pre, Nxt;
}t[N];

struct node {
    int l, r, x, id;
}z[N], h[N];
bool cmp1(node A, node B) {
    return A.l < B.l;
}
bool cmp2(node A, node B) {
    return A.r > B.r;
}

int v[N];
bool cmp(int x, int y) {
    return dfn[x] < dfn[y];
}
vector<int>r[N];
void Prepare() {
    for(int i = 1; i <= n; i++) v[i] = i;
    sort(v + 1, v + n + 1, cmp);
    for(int i = 1; i <= n; i++) r[dep[v[i]]].push_back(v[i]);
    for(int d = 1; d <= n; d++) {
        for(int i = 0; i < r[d].size(); i++) {
            if(i == 0) t[r[d][i]].Pre = -1;
            else t[r[d][i]].Pre = r[d][i - 1];
            if(i == r[d].size() - 1) t[r[d][i]].Nxt = -1;
            else {
                t[r[d][i]].Nxt = r[d][i + 1];
                ds.update(d - dep[ljm.LCA(r[d][i], r[d][i + 1])], 1);
            }
        }
        if(r[d].size()) ds.update(d, 1); //d深度只要有一个,贡献就要加1
    }
    n1 = tim1, n2 = tim2;
}


int X[M], Y[M], opt[M];
il void Go(int x) {
    while(tim2 > x) {
        if(opt[tim2] == 1) t[X[tim2]].Pre = Y[tim2];
        else t[X[tim2]].Nxt = Y[tim2];
        tim2--;
    }
}
il void Del(int x) {
    if(t[x].Pre != -1) {
        ds.update(dep[x] - dep[ljm.LCA(x, t[x].Pre)], -1);
        tim2++; X[tim2] = t[x].Pre, Y[tim2] = t[t[x].Pre].Nxt, opt[tim2] = 2;
        t[t[x].Pre].Nxt = t[x].Nxt; 
    }
    if(t[x].Nxt != -1) {
        ds.update(dep[x] - dep[ljm.LCA(x, t[x].Nxt)], -1);
        tim2++; X[tim2] = t[x].Nxt, Y[tim2] = t[t[x].Nxt].Pre, opt[tim2] = 1;
        t[t[x].Nxt].Pre = t[x].Pre; 
    }
    if(t[x].Pre != -1 && t[x].Nxt != -1) 
        ds.update(dep[x] - dep[ljm.LCA(t[x].Pre, t[x].Nxt)], 1);
    if(t[x].Pre == -1 && t[x].Nxt == -1)  //这个深度删掉没有点了
        ds.update(dep[x], -1);
}

il void Sol(int L, int R) { //处理左端点在[L,R]的询问
    sort(h + 1, h + tot + 1, cmp2);
    int Num = 0;
    for(int i = 1; i < L; i++) Del(i), Num++;
    int curL = L, curR = n;
    for(int i = 1; i <= tot; i++) {
        while(curR > h[i].r) Del(curR--), Num++;
        int lst1 = tim1, lst2 = tim2;
        while(curL < h[i].l) Del(curL++), Num++;
        ans[h[i].id] = ds.query(h[i].x + 1, n);
        ds.Go(lst1); Go(lst2);
        curL = L;
    }
    //cerr << Num << endl;
    ds.Go(n1); Go(n2);
}

signed main() {
	//freopen("x.in", "r", stdin);
	//freopen("x.out", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    read(n),read(m);
    int vv = sqrt(m);
	B1 = max(1, n / vv); B2 = sqrt(n);
	for(int i = 1;i <= n;i++) k[i] = (i - 1) / B2 + 1;
    for(int i = 1, fa; i <= n; i++) {
        read(fa);
        if(fa) p[fa].push_back(i);
        else root = i;
    }
    ljm.init(); Prepare();
    for(int i = 1; i <= m; i++) read(z[i].l),read(z[i].r),read(z[i].x),z[i].id = i;
    sort(z + 1, z + m + 1, cmp1);
    int j = 0;
    for(int i = 1; i <= n; i += B1) {
        tot = 0;
        while(j + 1 <= m && z[j + 1].l <= i + B1 - 1) j++, h[++tot] = z[j];
        Sol(i, min(n, i + B1 - 1)); 
    }
    for(int i = 1; i <= m; i++) print(ans[i]),pc('\n');
    flush();
    return 0;
}

//start : 2025/1/24 19:03

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 11
Accepted
time: 12ms
memory: 132592kb

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: 12ms
memory: 130876kb

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:

452
67
611
126
329
486
354
25
559
585
184
265
576
116
489
289
147
287
13
282
151
192
146
141
148
131
43
72
69
29
5
15
57
10
9
16
87
162
19
217
232
24
178
334
103
139
293
400
299
351
529
632
592
296
640
678
715
708
52
465
322
731
2
69
110
286
172
0
40
31
16
105
75
45
8
94
540
115
357
264
11
431
581
3...

result:

wrong answer 80th numbers differ - expected: '7', found: '264'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #30:

score: 0
Wrong Answer
time: 200ms
memory: 149892kb

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:

12045
1321
12292
2444
32443
36534
38575
30505
16464
16648
47153
41144
23401
1762
18567
6831
26486
29716
8905
16295
38569
29990
22061
7638
3820
34754
17582
6466
12632
23384
11657
16493
24434
11503
3945
2205
37806
13036
29052
24114
39158
34702
37326
20815
11766
41253
44355
17807
23452
26717
43107
4133...

result:

wrong answer 296th numbers differ - expected: '6', found: '3600'

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Wrong Answer

Test #67:

score: 0
Wrong Answer
time: 485ms
memory: 192728kb

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:

52956
18767
1319
13405
11021
455
50595
81481
6813
3640
58937
10991
70
4713
36
9517
39731
1166
67346
74637
2667
45182
4914
6774
1625
4198
52270
30435
60137
48654
29768
2815
6846
73091
21944
49693
9923
46795
29787
6866
2435
20773
2959
34666
4377
2428
4582
7527
38292
7253
3586
63817
28075
43828
20215
1...

result:

wrong answer 514th numbers differ - expected: '65', found: '25994'

Subtask #6:

score: 0
Skipped

Dependency #1:

0%