QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#789281#7448. rfplcaMr_AzTL 587ms8468kbC++142.5kb2024-11-27 19:44:492024-11-27 19:44:53

Judging History

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

  • [2024-11-27 19:44:53]
  • 评测
  • 测评结果:TL
  • 用时:587ms
  • 内存:8468kb
  • [2024-11-27 19:44:49]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

template<typename T>
void read(T &x){
    x=0;int f=1;char c=getchar();
    while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}
    while(isdigit(c)){x=x*10+c-'0';c=getchar();}
    x*=f;
}
template<typename T,typename ... Args>
void read(T &x, Args &... y){read(x);read(y...);}

const int N = 4e5 + 15;
int n, q, B, a[N], pre[N];
int pos[N];

int m, l[N], r[N], flag[N], cnt[N];

inline int del(int x, int d) { return max(x - d, 1); }
inline void init() {
    for (int i = 1; i * B <= n; i++) ++m, l[m] = r[m - 1] + 1, r[m] = i * B;
    if (m * B < n) ++m, l[m] = r[m - 1] + 1, r[m] = n;
    
    for (int i = 1; i <= m; i++)
        for (int j = l[i]; j <= r[i]; j++) pos[j] = i;
}
void build(int p){
    for (int i=l[p];i<=r[p];i++) a[i]=max(1,a[i]-flag[p]);
    flag[p]=0;
    for (int i=l[p];i<=r[p];i++)
        if (a[i]<l[p]) pre[i]=a[i];
        else pre[i]=pre[a[i]];
}
inline void pushdown(int u) {
    if (flag[u])
        for (int i = l[u]; i <= r[u]; i++) a[i] = del(a[i], flag[u]);
    flag[u] = 0; build(u);
}

inline void change(int x, int y, int d) {
    if (pos[x] == pos[y]) {
        for (int i = x; i <= y; i++) a[i] = del(a[i], d);
        build(pos[x]);
        return;
    }
    for (int i = x; i <= r[pos[x]]; i++) a[i] = del(a[i], d);
    for (int i = l[pos[y]]; i <= y; i++) a[i] = del(a[i], d);
    build(pos[x]), build(pos[y]);

    for (int u = pos[x] + 1; u <= pos[y] - 1; u++) {
        flag[u] = min(flag[u] + d, n);
        if (cnt[u] <= B) pushdown(u);  //??????,????
        cnt[u] = min(cnt[u] + d, n);
    }
}

inline int lca(int x, int y) {
    while (pos[x] != pos[y]) {
        if (pos[x] < pos[y]) swap(x, y);
        if (cnt[pos[x]] <= B) x = del(pre[x], flag[pos[x]]);
        else x = del(a[x], flag[pos[x]]);
    }
    while (x != y) {
        if (x < y) swap(x, y);
        x = del(a[x], flag[pos[x]]);
    }
    return x;
}

int main() {
    read(n), read(q), B = 500;
    init();
    for (int i = 2; i <= n; i++) read(a[i]);
    for (int i = 1; i <= m; i++) build(i);
    int lst = 0;
    while (q--) {
        int opt; read(opt);
        if (opt == 1) {
            int l, r, x; read(l,r,x);
            l ^= lst, r ^= lst, x ^= lst;
            change(l, r, x);
        } else {
            int u, v; read(u), read(v);
            u ^= lst, v ^= lst;
            lst = lca(u, v);
            printf("%d\n", lst);
        }
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 587ms
memory: 8468kb

input:

400000 400000
1 2 1 4 1 1 5 4 6 5 9 1 1 13 3 8 12 16 3 11 8 8 2 18 21 15 7 23 11 6 4 26 17 5 12 6 5 28 32 26 21 5 19 1 25 25 11 26 47 11 31 25 18 7 25 36 40 23 54 31 14 62 61 33 57 40 11 16 24 51 69 25 55 51 55 65 34 19 18 21 20 62 64 83 22 48 67 47 21 27 30 63 10 14 70 63 18 17 74 98 40 89 10 7 30 ...

output:

1
1
1
1
1
1
4
1
1
1
4
1
1
18
1
4
1
18
1
1
4
1
1
1
1
5
1
4
4
4
11
5
1
4
1
4
1
1
4
1
1
1
4
1
1
1
1
4
4
1
1
1
4
4
1
1
1
1
4
1
1
1
5
1
1
1
1
5
1
1
1
1
4
1
1
12
12
1
1
4
1
1
11
1
1
1
1
4
1
1
1
4
1
1
1
14
1
18
7
5
25
1
62
1
1
1
1
1
1
1
4
4
4
102
1
1
1
1
1
2
1
1
1
1
1
1
1
1
18
4
1
1
4
5
1
1
1
1
1
1
1
1
1
1...

result:

ok 200476 lines

Test #2:

score: -100
Time Limit Exceeded

input:

400000 400000
1 1 1 1 1 1 1 1 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 9...

output:

1
1
289629
1
1
37362
1
1
1
1
1
2107
1
1
1
1
1
200528
1
1
1
1
1
1
1
1
1
11646
1
1
1
1
7608
1
1
1
1
1
12297
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
206102
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
234105
1
213107
1
1
1
1
47884
1
1
1
1
1
1
1
1
1
1
1
4725
1
1
1
2170
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
206097
1
...

result: