QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#88212 | #5686. 种苹果 | Scintilla | RE | 132ms | 26736kb | C++14 | 6.1kb | 2023-03-15 16:24:42 | 2023-03-15 16:24:43 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define pb emplace_back
#define rep(i, s, e) for (int i = s; i <= e; ++i)
#define drep(i, s, e) for (int i = s; i >= e; --i)
#define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout)
#define pv(a) cout << #a << " = " << a << endl
#define pa(a, l, r) cout << #a " : "; rep(_, l, r) cout << a[_] << ' '; cout << endl
using ll = long long;
using vi = vector <int>;
const int N = 2e5 + 10;
const int M = 2000;
int read() {
int x = 0, f = 1; char c = getchar();
for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = -1;
for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48;
return x * f;
}
mt19937_64 rng(0);
int Rand(int l, int r) {
return l + rng() % (r - l + 1);
}
int n, cn, m, B, T, lstans, ans;
ll a[N];
set <int> se[N];
vi e[N];
int p[N], dfn[N], dn;
int col[N], top[M], len[M], id[M][M], laz[M], down[M];
double dep[N];
void Link(int u, int v, int w) {
// cout << "----- Link u, v, w = " << u << ' ' << v << ' ' << w << endl;
a[++ cn] = w;
se[u].erase(v), se[v].erase(u);
se[u].insert(cn), se[cn].insert(u);
se[v].insert(cn), se[cn].insert(v);
if (p[u] == v) swap(u, v);
p[v] = cn, p[cn] = u, dep[cn] = (dep[u] + dep[v]) / 2;
int c = col[v];
if (c) {
// pv(c);
col[cn] = c, a[cn] -= laz[c], id[c][len[c] + 1] = cn;
rep(i, 1, len[c]) if (a[cn] <= a[id[c][i]]) {
drep(j, len[c], i) id[c][j + 1] = id[c][j];
id[c][i] = cn;
break;
}
++ len[c];
}
}
void Add(int u, int w) {
// cout << "----- Add u, w = " << u << ' ' << w << endl;
a[++ cn] = w;
se[u].insert(cn), se[cn].insert(u);
p[cn] = u, dep[cn] = dep[u] + 1;
}
void Work(int u, int v, int w, int op) {
// cout << "----- Work u, v, w, op = " << u << ' ' << v << ' ' << w << ' ' << op << " -----\n";
function <void(int)> f, g;
function <void(int, int)> h;
auto ask = [&](int u) {
return a[u] + laz[col[u]];
} ;
if (op == 3) {
f = [&](int u) {
a[u] += w;
} ;
g = [&](int c) {
laz[c] += w;
} ;
h = [&](int u, int v) {
static bool tag[N];
int c = col[v];
for (int w = v; w != u; w = p[w]) f(w), tag[w] = true;
vector <int> sl, sr;
drep(i, len[c], 1) {
if (tag[id[c][i]]) sl.pb(id[c][i]);
else sr.pb(id[c][i]);
}
rep(i, 1, len[c]) {
if (!sr.size() || (sl.size() && a[sl.back()] <= a[sr.back()])) {
id[c][i] = sl.back(), sl.pop_back();
}
else id[c][i] = sr.back(), sr.pop_back();
}
for (int w = v; w != u; w = p[w]) tag[w] = false;
} ;
}
else {
ans = 0;
f = [&](int u) {
// cout << "u, ask = " << u << ' ' << ask(u) << endl;
ans += ask(u) >= w;
} ;
g = [&](int c) {
// cout << "g c = " << c << endl;
// rep(i, 1, len[c]) cout << ask(id[c][i]) << ' ';
// cout << endl;
int t = partition_point(id[c] + 1, id[c] + len[c] + 1, [&](int i) {
return ask(i) < w;
}) - id[c];
ans += len[c] - t + 1;
} ;
h = [&](int u, int v) {
for (int w = v; w != u; w = p[w]) f(w);
} ;
}
while (!col[u] || !col[v]) {
// cout << "3.5 u, v = " << u << ' ' << v << endl;
if (u == v) return f(u), void();
if (col[u] || (!col[v] && dep[u] < dep[v])) swap(u, v);
f(u), u = p[u];
}
// cout << "1 u, v = " << u << ' ' << v << endl;
// pv(ans);
while (col[u] != col[v]) {
if (dep[u] < dep[v]) swap(u, v);
// cout << "1.5 u, v = " << u << ' ' << v << endl;
int c = col[u];
// cout << "c, top[c] = " << c << ' ' << top[c] << endl;
// pa(col, 1, n);
if (u == down[c]) g(c);
else h(top[c], u);
u = top[c];
}
// cout << "2 u, v = " << u << ' ' << v << endl;
// pv(ans);
if (dep[u] < dep[v]) swap(u, v);
h(p[v], u);
}
void dfs0(int u, int fa) {
// cout << "u, fa = " << u << ' ' << fa << endl;
p[u] = fa, dfn[u] = ++ dn;
int son = 0;
for (int v : e[u]) if (v != fa) {
dep[v] = dep[u] + 1, dfs0(v, u);
if (col[v]) {
// cout << "u, v, son = " << u << ' ' << v << ' ' << son << endl;
if (col[u] || son) {
if (!col[u]) col[u] = ++ B;
top[col[son]] = top[col[v]] = u;
}
else son = v;
}
}
if (!col[u]) col[u] = col[son];
else down[col[u]] = u;
}
void rebuild() {
// cout << "----- rebuild -----\n";
n = cn, dn = 0;
rep(c, 1, B) {
rep(i, 1, len[c]) a[id[c][i]] += laz[c];
len[c] = laz[c] = top[c] = down[c] = 0;
}
rep(i, 1, n) {
p[i] = dfn[i] = dep[i] = col[i] = 0;
e[i] = vi(se[i].begin(), se[i].end());
}
// cout << "edge : \n";
// rep(i, 1, n) for (int j : e[i]) {
// if (i < j) cout << i << ' ' << j << endl;
// }
B = sqrt(n * log2(n) + 1);
col[1] = 1;
for (int i = 2, u; i <= B; ++ i) {
do { u = Rand(1, n); } while (col[u]);
col[u] = i;
}
dfs0(1, 0);
// pa(p, 1, n);
// pa(col, 1, n);
// pa(dep, 1, n);
// pa(top, 1, B);
// pa(down, 1, B);
// pa(a, 1, n);
rep(i, 1, n) if (col[i]) {
id[col[i]][++ len[col[i]]] = i;
}
rep(c, 1, B) {
sort(id[c] + 1, id[c] + len[c] + 1, [&](int i, int j) {
return a[i] < a[j];
});
}
}
int main() {
// file(data);
n = read(), m = read(), cn = n;
T = sqrt(n / log2(n + 1));
// T = m;
// cout << "B, T = " << (int) sqrt(n * log2(n) + 1) << ' ' << T << endl;
rep(i, 1, n) a[i] = read();
rep(i, 1, n - 1) {
int u = read(), v = read();
se[u].insert(v), se[v].insert(u);
}
rep(i, 0, m - 1) {
if (i % T == 0) rebuild();
int op = read();
if (op == 1) {
int u = read() ^ lstans, v = read() ^ lstans, w = read() ^ lstans;
Link(u, v, w);
}
else if (op == 2) {
int u = read() ^ lstans, w = read() ^ lstans;
Add(u, w);
}
else {
int u = read() ^ lstans, v = read() ^ lstans, w = read() ^ lstans;
Work(u, v, w, op);
if (op == 4) printf("%d\n", lstans = ans);
// lstans = 0;
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 132ms
memory: 26496kb
input:
5000 5000 -1201801 4507224 -765313 5795451 -126649 -5052948 470601 -5571705 7680665 -2662502 -689392 -3195719 3416253 -1023134 -3112032 3810606 -6975732 712075 257599 -180764 5190759 177433 -6055975 1555412 7768627 3402404 -872471 -1311920 -4231370 117735 3172664 -1502849 -3929398 -90435 6955032 382...
output:
645 0 0 0 57 0 1665 1087 1455 73 1172 1094 0 1739 1202 1290 0 0 0 569 0 1532 0 358 337 437 0 567 1086 12 73 922 1024 183 25 0 0 0 979 4 3 7 162 305 1285 0 1185 0 1263 402 576 1284 0 0 832 908 0 422 1425 1268 12 0 1692 439 0 0 28 0 384 0 0 1079 1257 1149 541 642 133 966 406 0 0 848 1335 431 0 0 363 8...
result:
ok 1020 lines
Test #2:
score: 0
Accepted
time: 119ms
memory: 26004kb
input:
5000 5000 -994733 862196 -2643618 4810652 2000445 -712322 -3955371 2924820 -675771 -6848147 825176 -5612153 4757907 1475460 1043402 1647007 -1015110 2001651 -6330733 2969460 -815149 6241724 136943 2360333 -3204656 5569099 809569 -4081554 -178998 -1363975 -4486956 -1858705 -59824 845830 4381332 17942...
output:
1469 2442 2 0 35 1869 0 353 993 1572 1095 0 12 1458 0 1659 545 0 0 1243 0 0 1410 911 1701 1660 0 0 84 728 0 5 0 1031 2089 1108 524 225 1237 2233 3 43 1324 327 1085 1049 615 61 1840 1892 0 491 989 242 1058 965 625 0 2592 340 36 0 211 0 146 2168 46 0 227 0 0 0 8 2103 1334 0 23 128 0 0 1307 0 0 0 2280 ...
result:
ok 962 lines
Test #3:
score: 0
Accepted
time: 123ms
memory: 26736kb
input:
5000 5000 1655881 -6171013 -6563069 6407036 -349214 1212406 2942912 -6594577 2008815 -1128329 -2115530 3807690 2938269 3705147 -5102093 -5658055 -2326373 4349357 -299635 825659 877457 3662152 358404 -892346 4685730 5257128 -6518733 -852709 4390588 -3492594 4680660 -386634 1743811 4770445 1735641 -39...
output:
1263 2159 0 107 4 2257 15 1329 172 0 929 0 1465 260 2106 496 685 80 347 1492 0 2638 409 0 177 0 0 0 5 0 2324 0 179 0 0 0 2122 1322 102 412 300 3 1272 0 6 0 1823 0 13 1739 1459 13 1207 278 0 1261 0 684 0 284 639 69 1700 88 243 1529 1970 0 98 2178 0 978 0 1229 1637 1620 660 835 0 0 214 953 203 674 159...
result:
ok 1055 lines
Test #4:
score: 0
Accepted
time: 130ms
memory: 24860kb
input:
5000 5000 1916330 -1277666 -2692038 -1686680 445447 522240 -9102533 4958411 5497747 -6470449 -1304451 -327692 -3951014 -5481922 -71181 -324109 -2416764 -6133434 -2713206 -6507719 -4562989 3531489 2232499 -1303696 3799685 1596321 4508356 -698966 6244189 -3451377 1146222 -1190263 -1224078 -1867687 256...
output:
0 0 0 1099 0 463 0 1182 25 0 784 0 663 1483 2171 0 1057 0 0 1377 0 0 0 218 0 0 70 906 1524 141 89 992 0 1667 0 308 0 77 6 0 148 0 1364 121 513 162 0 1440 1 1588 290 0 380 0 1517 8 1125 0 1362 592 74 0 83 443 857 0 0 0 10 307 1182 6 2352 15 953 31 966 2 536 2104 872 347 0 1846 185 146 1096 0 368 295 ...
result:
ok 1023 lines
Test #5:
score: -100
Runtime Error
input:
200000 200000 3962736 -7195878 -1853269 5312599 2123365 7028251 2312158 123258 -4443494 -5269322 877331 -616200 927434 -1927540 2943960 -7002528 -5869789 2362131 -2612652 -7859698 2076672 3520022 5147597 17824 -54033 5931665 -8193552 4202786 2726128 1137312 5233018 -580693 2414120 4925088 1642992 35...