QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#88197 | #5686. 种苹果 | Scintilla | TL | 0ms | 0kb | C++14 | 7.8kb | 2023-03-15 15:33:08 | 2023-03-15 15:33:10 |
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], cs[N];
int p[N], dfn[N], dn, sz[N], dep[N], ep[N], rk[N], typ[N];
int col[N], top[N], len[M], id[M][M], laz[M];
bool tag[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);
if (dep[u] == dep[v] && rk[u] < rk[v]) swap(u, v);
int t = ep[u];
// cout << "u, v, t = " << u << ' ' << v << ' ' << t << endl;
dep[cn] = dep[t], ep[cn] = t;
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);
if (col[t]) cs[col[t]].pb(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;
se[u].insert(cn), se[cn].insert(u);
p[cn] = typ[u] ? p[ep[u]] : u, dep[cn] = dep[u] + 1;
ep[cn] = cn, es[cn] = {cn};
}
void Work(int u, int v, int w, int op) {
// cout << "----- Work u, v, w, op = " << u << ' ' << v << ' ' << w << ' ' << op << " -----\n";
function <void(int)> f1, f2, g;
function <void(int, int)> h;
auto ask = [&](int u) {
return a[u] + laz[col[u]];
} ;
if (op == 3) {
f1 = [&](int u) {
a[u] += w;
} ;
f2 = [&](int u) {
if (typ[u]) a[u] += w;
else for (int i : es[u]) a[i] += w;
} ;
g = [&](int c) {
laz[c] += w;
for (int i : cs[c]) a[i] += w;
} ;
h = [&](int u, int v) {
int c = col[v];
for (int w = v; w != u; w = p[w]) {
f2(w), tag[w] = true;
}
if (col[u] == c) f1(u), tag[u] = 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();
}
} ;
}
else {
ans = 0;
f1 = [&](int u) {
// cout << "u, ask = " << u << ' ' << ask(u) << endl;
ans += ask(u) >= w;
} ;
f2 = [&](int u) {
if (typ[u]) ans += ask(u) >= w;
else for (int i : es[u]) ans += ask(i) >= 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;
for (int i : cs[c]) ans += ask(i) >= w;
} ;
h = [&](int u, int v) {
int c = col[v];
for (int w = v; w != u; w = p[w]) {
f2(w), tag[w] = true;
}
if (col[u] == c) f1(u), tag[u] = true;
} ;
}
// cout << "1 u, v = " << u << ' ' << v << endl;
if (ep[u] == ep[v]) {
if (rk[u] > rk[v]) swap(u, v);
rep(i, rk[u], rk[v] - 1) f1(es[ep[u]][i]);
if (typ[v]) { f1(v); return; }
else u = v;
}
if (dep[ep[u]] < dep[ep[v]]) swap(u, v);
// cout << "2 u, v = " << u << ' ' << v << endl;
// pv(ans);
auto eup = [&](int& x) {
// cout << "eup x = " << x << endl;
if (!typ[x]) return;
// cout << "x, rk[x] = " << x << ' ' << rk[x] << endl;
// pv(ep[x]);
rep(i, 0, rk[x]) f1(es[ep[x]][i]);
x = p[ep[x]];
} ;
auto edown = [&](int& x) {
while (typ[x]) {
f1(x), x = es[ep[x]][rk[x] + 1];
}
} ;
eup(u);
if (in(ep[v], ep[u])) edown(v);
else eup(v);
// cout << "3 u, v = " << u << ' ' << v << endl;
// pv(ans);
while (!col[u] || !col[v]) {
// cout << "3.5 u, v = " << u << ' ' << v << endl;
if (u == v) return f1(u), void();
if (col[u] || (!col[v] && dep[u] < dep[v])) swap(u, v);
f2(u), u = p[u];
}
// cout << "4 u, v = " << u << ' ' << v << endl;
// pv(ans);
while (col[u] != col[v]) {
if (dep[u] < dep[v]) swap(u, v);
// cout << "4.5 u, v = " << u << ' ' << v << endl;
int c = col[u];
// cout << "c, top[c] = " << c << ' ' << top[c] << endl;
// pa(col, 1, n);
if (dep[u] - dep[top[c]] == len[c]) g(c);
else h(top[c], u);
u = top[c];
}
// cout << "5 u, v = " << u << ' ' << v << endl;
// pv(ans);
if (dep[u] < dep[v]) swap(u, v);
h(v, u);
}
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;
cs[c].clear();
}
rep(i, 1, n) {
p[i] = dfn[i] = sz[i] = dep[i] = 0;
ep[i] = i, rk[i] = col[i] = typ[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);
}
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;
}
详细
Test #1:
score: 0
Time Limit Exceeded
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...