QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#859339#9676. Ancestorsrainygame0 1035ms119140kbC++144.5kb2025-01-17 17:37:332025-01-17 17:37:46

Judging History

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

  • [2025-01-17 17:37:46]
  • 评测
  • 测评结果:0
  • 用时:1035ms
  • 内存:119140kb
  • [2025-01-17 17:37:33]
  • 提交

answer

#pragma GCC optimize(2, 3, "Ofast")
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define MAXM 1000005

bool st;

int urd(){char c; int x(0); while ((c=getchar())<48); do x=(x<<1)+(x<<3)+(c^48); while ((c=getchar())>47); return x;}

int n, q, m, rt, ct;
int fa[MAXN], dep[MAXN], siz[MAXN], top[MAXN], son[MAXN], dfn[MAXN], rt2[MAXN];
int ans[MAXM], col[MAXN*3], sz[MAXN*3]; set<int> ps[MAXN];
vector<int> e[MAXN], d[MAXN], g[MAXN*3]; unordered_map<int, int> ump[MAXN];
struct Node{int v, id;}; vector<Node> yyx; struct Que{int l, r, x, id;}que[MAXM];
struct Node2{int v, x, y, id;}; vector<Node2> qry;

void dfs1(int x){dep[x]=dep[fa[x]]+(siz[x]=1); for (auto i: e[x]) dfs1(i), siz[x]+=siz[i], son[x]=siz[i]>siz[son[x]]?i:son[x];}
void dfs2(int x, int tp){top[x]=tp; dfn[x]=++ct; if (son[x]) dfs2(son[x], tp); for (auto i: e[x]) if (i^son[x]) dfs2(i, i);}
int LCA(int x, int y){for (; top[x]^top[y]; x=fa[top[x]]) if (dep[top[x]]<dep[top[y]]) swap(x, y); return dep[x]<dep[y]?x:y;}
void sol0(int p){
    sort(d[p].begin(), d[p].end(), [=](int x, int y){return dfn[x]<dfn[y];});
    vector<int> vec(d[p]); for (int i(0); i+1<d[p].size(); ++i) vec.push_back(LCA(d[p][i], d[p][i+1]));
    sort(vec.begin(), vec.end(), [=](int x, int y){return dfn[x]<dfn[y];}); auto it(unique(vec.begin(), vec.end()));
    for (auto i(vec.begin()); i!=it; ++i){ump[p][*i]=++ct; if (dep[*i] == p) col[ct]=*i, sz[ct]=1;}
    for (auto i(vec.begin()); next(i)!=it; ++i) g[ump[p][LCA(*i, *next(i))]].push_back(ump[p][*next(i)]);
    for (auto i(vec.begin()); i!=it; ++i) if (dep[*i]^p) yyx.push_back({p-dep[*i], ump[p][*i]}); rt2[p] = ump[p][vec[0]];
}
void modify(int p, int q){
    int las(0); for (auto i: ps[p]) qry.push_back({-1, las, i, 0}), las = i;
    // if (p == 1) cerr << ps[p].size() << '\n';
    for (auto i: ps[p]){
        auto it(ps[q].insert(i).first); qry.push_back({1, it==ps[q].begin()?0:*prev(it), i, 0});
        // if (col[p] == 1) cerr << qry.size() << '\n';
        if (next(it) != ps[q].end()){
            qry.push_back({-1, it==ps[q].begin()?0:*prev(it), *next(it), 0}); qry.push_back({1, i, *next(it), 0});
        }
    }
    ps[p].clear();
}

namespace BIT{
#define lbw(x) ((x) & -(x))
    int c[MAXN];
    void add(int x, int k){for (; x<=n; x+=lbw(x)) c[x] += k;}
    int qry(int x){int res(0); for (; x; x-=lbw(x)) res += c[x]; return res;}
    int qry(int l, int r){return qry(r)-qry(l-1);}
}
void cdq(int l, int r){
    if (l == r) return; int mid((l+r)>>1); cdq(l, mid); cdq(mid+1, r);
    sort(qry.begin()+l, qry.begin()+mid+1, [](Node2 a, Node2 b){
        if (a.v && !b.v) return true; if (!a.v && b.v) return false; return a.x < b.x;
    });
    sort(qry.begin()+mid+1, qry.begin()+r+1, [](Node2 a, Node2 b){
        if (a.v && !b.v) return false; if (!a.v && b.v) return true; return a.x < b.x;
    }); int p(l);
    for (int i(mid+1); i<=r && !qry[i].v; ++i){
        while (p <= mid && qry[p].v && qry[p].x<qry[i].x) BIT::add(qry[p].y, qry[p].v), ++p;
        ans[qry[i].id] += BIT::qry(qry[i].x, qry[i].y);
    }
    for (--p; p>=l; --p) BIT::add(qry[p].y, -qry[p].v);
}

bool ed;

signed main(){
    // cerr << (&st-&ed)/1024.0/1024.0 << '\n';
    n = urd(); q = urd(); for (int i(1); i<=n; ++i){e[fa[i]=urd()].push_back(i); if (!fa[i]) rt=i;} dfs1(rt); dfs2(rt, rt);
    for (int i(1); i<=n; ++i) d[dep[i]].push_back(i); for (int i(1); i<=n && d[i].size(); ++i) sol0(i);
    for (int i(1); i<=q; ++i) cin >> que[i].l >> que[i].r >> que[i].x, que[i].id = i;
    sort(que+1, que+q+1, [](Que a, Que b){return a.x < b.x;});
    sort(yyx.begin(), yyx.end(), [](Node a, Node b){return a.v < b.v;});
    for (int i(1); i<=n; ++i) ps[i].insert(i), qry.push_back({1, 0, i, 0});
    for (int i(1), p(0); i<=q; ++i){
        for (; p<yyx.size() && que[i].x>=yyx[p].v; ++p){
            int id(yyx[p].id); sort(g[id].begin(), g[id].end(), [=](int x, int y){return sz[x] > sz[y];}); col[id]=col[g[id][0]];
            // cerr << id << '\n';
            for (int i(1); i<g[id].size(); ++i) modify(col[g[id][i]], col[id]);
        }
        if (que[i].x ^ que[i-1].x){
            int las(0); for (auto j: ps[col[rt2[que[i].x]]]) qry.push_back({-1, las, j, 0}), las = j;
        }
        qry.push_back({0, que[i].l, que[i].r, que[i].id});
    }
    // for (auto i: qry){
    //     cerr << i.v << ' ' << i.x << ' ' << i.y << ' ' << i.id << '\n';
    // }
    cdq(0, qry.size()-1); for (int i(1); i<=q; ++i) cout << ans[i] << '\n';

    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: 2ms
memory: 32336kb

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: 34776kb

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:

628
375
715
383
496
595
493
278
611
619
340
383
577
269
500
356
250
330
136
302
204
224
184
167
161
141
70
77
69
30
6
22
60
53
62
81
129
185
110
248
266
150
249
367
226
263
369
454
401
447
579
651
640
453
694
729
764
771
364
629
547
824
2
88
110
382
187
1
82
42
358
180
108
72
44
149
631
168
418
156
...

result:

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

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #30:

score: 0
Wrong Answer
time: 315ms
memory: 64172kb

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:

12732
2209
12968
3317
32746
36755
38755
30845
17042
17223
47186
41279
23855
2627
19103
7602
26881
30050
9627
16851
38720
30307
22514
8370
4613
34966
18105
7211
13235
23795
12282
17025
24828
12128
4721
3013
37939
13613
29351
24502
39268
34883
37458
21250
12358
41316
44365
18288
23834
27041
43133
4138...

result:

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

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Wrong Answer

Test #67:

score: 0
Wrong Answer
time: 1035ms
memory: 119140kb

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:

52957
18769
1319
13409
11024
456
50595
81481
6814
3641
58937
10993
70
4714
36
9521
39732
1166
67346
74637
2668
45183
4918
6776
1625
4199
52272
30436
60138
48654
29769
2816
6847
73091
21944
49693
9924
46795
29787
6870
2436
20776
2960
34667
4378
2429
4584
7528
38294
7253
3587
63817
28076
43830
20216
1...

result:

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

Subtask #6:

score: 0
Skipped

Dependency #1:

0%