QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#87982 | #5686. 种苹果 | Scintilla | RE | 0ms | 0kb | C++14 | 7.3kb | 2023-03-14 22:26:46 | 2023-03-14 22:26:47 |
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], es[N];
int p[N], dfn[N], dn, sz[N], dep[N], ep[N], typ[N], rk[N];
int col[N], top[N], len[M], id[M][M], laz[M], el[N];
bool in(int u, int v) {
return dfn[u] <= dfn[v] && dfn[v] < dfn[u] + sz[u];
}
void Link(int u, int v, int w) {
// cout << "----- Link u, v, w = " << u << ' ' << v << ' ' << w << endl;
a[++ cn] = w, typ[cn] = 1;
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 (dep[u] < dep[v]) swap(u, v);
int t = ep[u];
dep[cn] = dep[t], ep[cn] = t, a[cn] -= el[ep[cn]] + laz[col[ep[cn]]];
for (int i = 0; i < es[t].size(); ++ i) {
if (es[t][i] == u) {
// cout << "t, i = " << t << ' ' << i << endl;
es[t].insert(es[t].begin() + i, cn);
// cout << "es[t] : ";
// for (auto it : es[t]) cout << it << ' ';
// cout << endl;
break;
}
}
for (int i = 0; i < es[t].size(); ++ i) rk[es[t][i]] = i;
}
void Add(int u, int w) {
// cout << "----- Add u, w = " << u << ' ' << w << endl;
a[++ cn] = w, typ[cn] = 2;
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, int)> h;
auto ask = [&](int u) {
if (!typ[u]) return a[u] + laz[col[u]];
else if (typ[u] == 1) return a[u] + el[ep[u]] + laz[col[ep[u]]];
else return a[u];
} ;
if (op == 3) {
f = [&](int u) {
a[u] += w;
if (!typ[u]) el[u] += w;
} ;
g = [&](int c) {
laz[c] += w;
} ;
h = [&](int c, int l, int r) {
auto Merge = [&](int x, int y) {
static int o[N];
rep(i, 1, y) o[i] = id[c][i];
for (int i = 1, j = x + 1, k = 1; i <= x || j <= y; ) {
if (j > y || (i <= x && a[o[i]] <= a[o[j]])) {
id[c][k] = o[i], ++ i;
}
else {
id[c][k] = o[j], ++ j;
}
}
} ;
rep(i, l, r) f(id[c][i]);
Merge(l - 1, r), Merge(r, len[c]);
} ;
}
else {
ans = 0;
f = [&](int u) {
ans += ask(u) >= w;
} ;
g = [&](int c) {
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 c, int l, int r) {
rep(i, l, r) f(id[c][i]);
} ;
}
while (typ[u] == 2 || typ[v] == 2) {
if (typ[u] != 2 || (typ[v] == 2 && dep[u] < dep[v])) swap(u, v);
f(u);
if (u == v) return;
u = p[u];
}
// cout << "122 u, v = " << u << ' ' << v << endl;
if (ep[u] == ep[v]) {
if (rk[u] > rk[v]) swap(u, v);
rep(i, rk[u], rk[v]) f(es[ep[u]][i]);
return;
}
if (dep[ep[u]] < dep[ep[v]]) swap(u, v);
auto eup = [&](int& x) {
// cout << "x, rk[x] = " << x << ' ' << rk[x] << endl;
rep(i, 0, rk[x]) {
// pv(es[ep[x]][i]);
f(es[ep[x]][i]);
}
x = p[ep[x]];
} ;
auto edown = [&](int& x) {
// cout << "x, rk[x] = " << x << ' ' << rk[x] << endl;
// pv(ep[x]);
// pv(es[ep[x]][rk[x]]);
for (int i = rk[x]; es[ep[x]][i] != ep[x]; ++ i) {
// pv(es[ep[x]][i]);
f(es[ep[x]][i]);
}
x = ep[x];
} ;
eup(u);
if (in(ep[v], ep[u])) edown(v);
else eup(v);
// cout << "128 u, v = " << u << ' ' << v << endl;
// pv(ans);
while (!col[u] || !col[v]) {
if (col[u] || (!col[v] && dep[u] < dep[v])) swap(u, v);
f(u);
if (u == v) return;
u = p[u];
}
// cout << "136 u, v = " << u << ' ' << v << endl;
// pv(ans);
while (col[u] != col[v]) {
if (dep[u] < dep[v]) swap(u, v);
// cout << "116 u, v = " << u << ' ' << v << endl;
int c = col[u], k = dep[u] - dep[top[c]];
// cout << "c, top[c] = " << c << ' ' << top[c] << endl;
// pa(col, 1, n);
if (k == len[u]) g(c);
else h(c, 1, k);
u = top[c];
}
// cout << "148 u, v = " << u << ' ' << v << endl;
// pv(ans);
if (dep[u] < dep[v]) swap(u, v);
int c = col[u], l = dep[v] - dep[top[c]], r = dep[u] - dep[top[c]];
// cout << "c, l, r = " << c << ' ' << l << ' ' << r << endl;
h(c, l, r);
}
void dfs0(int u, int fa) {
// cout << "u, fa = " << u << ' ' << fa << endl;
p[u] = fa, dep[u] = dep[fa] + 1;
dfn[u] = ++ dn, sz[u] = 1;
int son = 0;
for (int v : e[u]) if (v != fa) {
dfs0(v, u), sz[u] += sz[v];
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];
}
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] = 0;
}
rep(i, 1, n) {
for (int u : es[i]) {
if (u != i) a[u] += el[i];
}
p[i] = dfn[i] = sz[i] = dep[i] = 0;
ep[i] = i, typ[i] = rk[i] = 0;
col[i] = el[i] = 0;
e[i] = vi(se[i].begin(), se[i].end());
es[i] = {i};
}
// 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;
rep(i, 2, B) {
int u;
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(a, 1, n);
rep(i, 1, n) if (col[i]) {
int k = dep[i] - dep[top[col[i]]];
id[col[i]][k] = i, len[col[i]] = max(len[col[i]], k);
}
}
int main() {
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;
}
詳細信息
Test #1:
score: 0
Runtime Error
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...