QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#587811 | #9373. Query on Tree | MiniLong | WA | 27ms | 22052kb | C++17 | 8.8kb | 2024-09-24 21:46:29 | 2024-09-24 21:46:30 |
Judging History
answer
#include <bits/stdc++.h>
#define _rep(i, x, y) for(int i = x; i <= y; ++i)
#define _req(i, x, y) for(int i = x; i >= y; --i)
#define _rev(i, u) for(int i = head[u]; i; i = e[i].nxt)
#define pb push_back
#define fi first
#define se second
#define mst(f, i) memset(f, i, sizeof f)
using namespace std;
#ifdef ONLINE_JUDGE
#define debug(...) 0
#else
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#endif
typedef long long ll;
typedef pair<int, int> PII;
namespace fastio{
#ifdef ONLINE_JUDGE
char ibuf[1 << 20],*p1 = ibuf, *p2 = ibuf;
#define get() p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++
#else
#define get() getchar()
#endif
template<typename T> inline void read(T &t){
T x = 0, f = 1;
char c = getchar();
while(!isdigit(c)){
if(c == '-') f = -f;
c = getchar();
}
while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
t = x * f;
}
template<typename T, typename ... Args> inline void read(T &t, Args&... args){
read(t);
read(args...);
}
template<typename T> void write(T t){
if(t < 0) putchar('-'), t = -t;
if(t >= 10) write(t / 10);
putchar(t % 10 + '0');
}
template<typename T, typename ... Args> void write(T t, Args... args){
write(t), putchar(' '), write(args...);
}
template<typename T> void writeln(T t){
write(t);
puts("");
}
template<typename T> void writes(T t){
write(t), putchar(' ');
}
#undef get
};
using namespace fastio;
#define multitest() int T; read(T); _rep(tCase, 1, T)
namespace Calculation{
const ll mod = 998244353;
ll ksm(ll p, ll h){ll base = p % mod, res = 1; while(h){if(h & 1ll) res = res * base % mod; base = base * base % mod, h >>= 1ll;} return res;}
void dec(ll &x, ll y){x = ((x - y) % mod + mod) % mod;}
void add(ll &x, ll y){x = (x + y) % mod;}
void mul(ll &x, ll y){x = x * y % mod;}
ll sub(ll x, ll y){return ((x - y) % mod + mod) % mod;}
ll pls(ll x, ll y){return ((x + y) % mod + mod) % mod;}
ll mult(ll x, ll y){return x * y % mod;}
}
using namespace Calculation;
const int N = 2e5 + 5, B = 10;
const ll inf = 0x3f3f3f3f3f3f3f3f;
ll n, q, a[N];
vector<int> G[N];
struct sgt{
ll tr[N << 2], tag[N << 2];
#define ls x << 1
#define rs x << 1 | 1
void update(int x){tr[x] = max(tr[ls], tr[rs]);}
void pushdown(int x){
if(!tag[x]) return;
tag[ls] += tag[x], tag[rs] += tag[x];
tr[ls] += tag[x], tr[rs] += tag[x];
tag[x] = 0;
}
void modify(int x, int l, int r, int L, int R, ll val){
if(l >= L && r <= R) return tr[x] += val, tag[x] += val, void();
int mid = l + r >> 1;
pushdown(x);
if(L <= mid) modify(ls, l, mid, L, R, val);
if(R > mid) modify(rs, mid + 1, r, L, R, val);
update(x);
}
ll query(int x, int l, int r, int L, int R){
if(l >= L && r <= R) return tr[x];
int mid = l + r >> 1; ll res = -inf;
pushdown(x);
if(L <= mid) res = max(res, query(ls, l, mid, L, R));
if(R > mid) res = max(res, query(rs, mid + 1, r, L, R));
return res;
}
void clr(int x, int l, int r){
tr[x] = tag[x] = 0;
if(l == r) return;
int mid = l + r >> 1;
clr(ls, l, mid), clr(rs, mid + 1, r);
}
}f, ans, tag;
int id, dfn[N], cnt[N][15], sum[N][15], dep[N], rev[N], pre[N], maxn[N];
int id2, in[N], out[N];
void clr(){
f.clr(1, 1, n), ans.clr(1, 1, n), tag.clr(1, 1, n);
_rep(i, 1, n) mst(cnt[i], 0), mst(sum[i], 0), dfn[i] = dep[i] = rev[i] = pre[i] = maxn[i] = in[i] = out[i] = 0, G[i].clear(), a[i] = 0;
id = id2 = 0;
}
void dfs(int u, int fa){
cnt[u][0]++, dep[u] = dep[fa] + 1, pre[u] = fa;
for(auto &v : G[u]) if(v != fa){
dfs(v, u), maxn[u] = max(maxn[u], maxn[v] + 1);
_rep(i, 0, B - 1) cnt[u][i + 1] += cnt[v][i];
}
_rep(i, 0, B) sum[u][i] = (!i ? 0 : sum[u][i - 1]) + cnt[u][i];
}
void dfs2(int u, int fa){
if(maxn[u] < B) return;
in[u] = ++id2;
for(auto &v : G[u]) if(v != fa) dfs2(v, u);
out[u] = id2;
}
void bfs(){
queue<int> q; q.push(1);
while(q.size()){
int u = q.front(); q.pop();
rev[dfn[u] = ++id] = u;
for(auto &v : G[u]) if(dep[v] > dep[u]) q.push(v);
}
}
void pushdown(int u){
_rep(i, 0, 2 * B){
if(!u) break;
if(maxn[u] >= B){
ll x = tag.query(1, 1, n, in[u], in[u]); tag.modify(1, 1, n, in[u], in[u], -x);
int L = dfn[u] + sum[u][B - 1], R = L + cnt[u][B] - 1;
f.modify(1, 1, n, L, R, x);
}
u = pre[u];
}
}
void update(int u){
_rep(i, 0, 2 * B){
if(!u) break;
if(maxn[u] >= B){
ll x = ans.query(1, 1, n, in[u], in[u]); ans.modify(1, 1, n, in[u], in[u], -x);
int L = dfn[u] + sum[u][B - 1], R = L + cnt[u][B] - 1;
ans.modify(1, 1, n, in[u], in[u], f.query(1, 1, n, L, R));
}
u = pre[u];
}
}
void modify(int u, int v, int k, ll val){
if(!cnt[u][k]) return;
int L = dfn[u] + (!k ? 0 : sum[u][k - 1]), R = L + cnt[u][k] - 1;
if(k > 0 && v && cnt[v][k - 1]){
int l = dfn[v] + (!(k - 1) ? 0 : sum[v][k - 2]), r = l + cnt[v][k - 1] - 1;
if(L < l) f.modify(1, 1, n, L, l - 1, val);
if(r < R) f.modify(1, 1, n, r + 1, R, val);
}else f.modify(1, 1, n, L, R, val);
}
ll query(int u, int v, int k){
if(!cnt[u][k]) return -inf;
int L = dfn[u] + (!k ? 0 : sum[u][k - 1]), R = L + cnt[u][k] - 1;
if(k > 0 && v && cnt[v][k - 1]){
int l = dfn[v] + (!(k - 1) ? 0 : sum[v][k - 2]), r = l + cnt[v][k - 1] - 1;
ll res = -inf;
if(L < l) res = max(res, f.query(1, 1, n, L, l - 1));
if(r < R) res = max(res, f.query(1, 1, n, r + 1, R));
return res;
}
return f.query(1, 1, n, L, R);
}
void modify2(int u, int v, int k, ll val){
if(!cnt[u][k]) return;
int L = dfn[u], R = L + sum[u][k] - 1;
if(k > 0 && v){
int l = dfn[v], r = l + sum[v][k - 1] - 1;
if(L < l) f.modify(1, 1, n, L, l - 1, val);
if(r < R) f.modify(1, 1, n, r + 1, R, val);
}else f.modify(1, 1, n, L, R, val);
}
ll query2(int u, int v, int k){
if(!cnt[u][k]) return -inf;
int L = dfn[u], R = L + sum[u][k] - 1;
if(k > 0 && v){
int l = dfn[v], r = l + sum[v][k - 1] - 1;
ll res = -inf;
if(L < l) res = max(res, f.query(1, 1, n, L, l - 1));
if(r < R) res = max(res, f.query(1, 1, n, r + 1, R));
return res;
}
return f.query(1, 1, n, L, R);
}
int main(){
multitest(){
read(n, q);
_rep(i, 1, n) read(a[i]);
_rep(i, 2, n){
int u, v; read(u, v);
G[u].pb(v), G[v].pb(u);
}
dfs(1, 0), dfs2(1, 0), bfs();
_rep(i, 1, n) f.modify(1, 1, n, dfn[i], dfn[i], a[i]);
_rep(i, 1, n) if(maxn[i] >= B){
int L = dfn[i] + sum[i][B - 1], R = L + cnt[i][B] - 1;
ans.modify(1, 1, n, in[i], in[i], f.query(1, 1, n, L, R));
}
while(q--){
ll opt, x, k, val; read(opt, x);
if(opt == 1){
read(k, val); int cur = k, v = 0, u = x;
ll res = -inf;
pushdown(x);
while(u && cur >= 0){
modify(u, v, cur, val);
res = max(res, query(u, v, cur));
v = u, u = pre[u], cur--;
}
update(x);
if(res == -inf) puts("GG");
else writeln(res);
}
if(opt == 2){
read(k, val); int cur = k, v = 0, u = x;
ll res = -inf;
pushdown(x);
while(u && cur >= 0){
modify2(u, v, cur, val);
res = max(res, query2(u, v, cur));
v = u, u = pre[u], cur--;
}
update(x);
if(res == -inf) puts("GG");
else writeln(res);
}
if(opt == 3){
read(val);
int L = dfn[x], R = dfn[x] + sum[x][B] - 1;
pushdown(x), f.modify(1, 1, n, L, R, val), update(x);
ll res = f.query(1, 1, n, L, R);
if(maxn[x] >= B){
tag.modify(1, 1, n, in[x], out[x], val), ans.modify(1, 1, n, in[x], out[x], val);
res = max(res, ans.query(1, 1, n, in[x], out[x]));
}
if(res == -inf) puts("GG");
else writeln(res);
}
}
clr();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 22052kb
input:
1 5 5 1 2 1 3 2 1 2 2 3 2 4 4 5 2 2 1 0 1 2 1 3 3 4 -5 2 5 2 3 3 2 -1
output:
3 6 1 5 4
result:
ok 5 lines
Test #2:
score: -100
Wrong Answer
time: 27ms
memory: 22028kb
input:
10000 3 9 42973045452542 34994498886390 -91733395797555 1 3 1 2 1 1 5 -71952967 3 1 -816873082 1 1 5 -842437577 2 3 7 254550528 3 3 -854082700 2 3 2 699808309 3 3 554885567 1 2 7 595565507 1 3 0 -318374053 3 2 -63158159333100 77264362049163 -99188430131116 1 2 3 2 2 2 4 -305866230 3 1 -549738403 3 5...
output:
GG 42972228579460 GG GG -91735066753337 42972928387769 -91734511867770 GG -91734830241823 GG 77263812310760 7065382376488 GG 7066167929218 -85116594064352 -85115697573094 96492412785032 -20428913156111 -20428197540063 96491742171666 -14945310996805 GG -22853125202435 GG 98377163227355 98376397205230...
result:
wrong answer 4th lines differ - expected: '42972483129988', found: 'GG'