QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#789216 | #7448. rfplca | Mr_Az | TL | 420ms | 11572kb | C++14 | 2.8kb | 2024-11-27 19:34:28 | 2024-11-27 19:34:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
char buf[1 << 20], *p1 = buf, *p2 = buf, ouf[1 << 23], *p3 = ouf;
inline char input() {
if (p1 == p2) p2 = buf + fread(p1 = buf, 1, 1 << 20, stdin);
return *(p1++);
}
inline void read(int &x) {
static char ch = input();
x = 0;
while (!isdigit(ch)) ch = input();
while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = input();
}
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;
}
inline void pushdown(int u) {
if (flag[u])
for (int i = l[u]; i <= r[u]; i++) a[i] = max(a[i]- flag[u],1);
flag[u] = 0;
for (int i = l[u]; i <= r[u]; i++) pre[i] = (a[i] < l[u]) ? a[i] : pre[a[i]];
}
inline void change(int x, int y, int d) {
if (pos[x] == pos[y]) {
for (int i = x; i <= y; i++) a[i] = max(a[i]-d,1);
int u=pos[x];for (int i = l[u]; i <= r[u]; i++) pre[i] = (a[i] < l[u]) ? a[i] : pre[a[i]];
return;
}
for (int i = x; i <= r[pos[x]]; i++) a[i] = max(a[i]-d,1);
for (int i = l[pos[y]]; i <= y; i++) a[i] = max(a[i]-d,1);
int u=pos[x];
for (int i = l[u]; i <= r[u]; i++) pre[i] = (a[i] < l[u]) ? a[i] : pre[a[i]];
u=pos[y];
for (int i = l[u]; i <= r[u]; i++) pre[i] = (a[i] < l[u]) ? a[i] : pre[a[i]];
for (int u = pos[x] + 1; u <= pos[y] - 1; u++) {
flag[u] = min(flag[u] + d, n), cnt[u] = min(cnt[u] + d, n);
if (cnt[u] <= B) pushdown(u); //更新次数较少,暴力重构
}
}
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 = max(pre[x]-flag[pos[x]],1);
else x = max(a[x]-flag[pos[x]],1);
}
while (x != y) {
if (x < y) swap(x, y);
x = max(a[x]- flag[pos[x]],1);
}
return x;
}
int main() {
read(n), read(q), B = 500;
init();
for (int i = 2; i <= n; i++) read(a[i]);
for (int u = 1; u <= m; u++) for (int i = l[u]; i <= r[u]; i++) pre[i] = (a[i] < l[u]) ? a[i] : pre[a[i]];
int lst = 0;
while (q--) {
int opt; read(opt);
if (opt == 1) {
int l, r, x; read(l), read(r), read(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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 420ms
memory: 11572kb
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 ...