QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#589633 | #9373. Query on Tree | MiniLong | WA | 35ms | 24168kb | C++17 | 9.6kb | 2024-09-25 19:13:45 | 2024-09-25 19:13:45 |
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];
int st[N][15];
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);
}
_rep(i, 1, n){
int u = i, d = 0;
while(u && d <= B){
st[u][d] = !st[u][d] ? dfn[i] : min(st[u][d], dfn[i]);
u = pre[u], d++;
}
}
}
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 = st[u][B], 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 = st[u][B], 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 = st[u][k], R = L + cnt[u][k] - 1;
if(k > 0 && v && cnt[v][k - 1]){
int l = st[v][k - 1], 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 = st[u][k], R = L + cnt[u][k] - 1;
if(k > 0 && v && cnt[v][k - 1]){
int l = st[v][k - 1 ], 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){
// debug("fuck %d k:%d\n", u, k);
if(k >= 0 && cnt[u][k]){
int L = st[u][k], R = L + cnt[u][k] - 1;
f.modify(1, 1, n, L, R, val);
}
if(k > 0 && cnt[u][k - 1]){
int L = st[u][k - 1], R = L + cnt[u][k - 1] - 1;
f.modify(1, 1, n, L, R, val);
}
// debug("eeeee\n");
}
ll query2(int u, int v, int k){
ll res = -inf;
if(k >= 0 && cnt[u][k]){
int L = st[u][k], R = L + cnt[u][k] - 1;
res = max(res, f.query(1, 1, n, L, R));
// debug("u:%d k:%d [%d,%d]\n", u, k, L, R);
}
if(k > 0 && cnt[u][k - 1]){
int L = st[u][k - 1], R = L + cnt[u][k - 1] - 1;
// debug("u:%d k:%d [%d,%d]\n", u, k - 1, L, R);
res = max(res, f.query(1, 1, n, L, R));
}
return res;
}
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 = st[i][B], 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));
if(u == 1){
for(int i = cur - 2; i >= 0; i -= 2){
modify2(u, v, i, val);
res = max(res, query2(u, v, i));
}
}
v = u, u = pre[u], cur--;
}
update(x);
if(res == -inf) puts("GG");
else writeln(res);
}
if(opt == 3){
read(val); ll res = -inf;
pushdown(x);
_rep(i, 0, B - 1){
if(!cnt[x][i]) break;
int L = st[x][i], R = L + cnt[x][i] - 1;
debug("[%d,%d] %d\n", L, R, val);
f.modify(1, 1, n, L, R, val);
res = max(res, f.query(1, 1, n, L, R));
}
update(x);
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);
}
_rep(i, 1, n) debug("%d ", f.query(1, 1, n, dfn[i], dfn[i]));
debug("\n");
debug("====================\n");
}
clr();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 4ms
memory: 24168kb
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: 35ms
memory: 22276kb
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 42972483129988 -91734812202809 42973182938297 -91733557508933 GG -91733875882986 77264056182933 77263506444530 7065382376488 7065749360235 7066534912965 -61566971689846 -61566075198588 96492412785032 -20428913156111 -20428197540063 96491742171666 -20428530470657 96491180203461 9...
result:
wrong answer 15th lines differ - expected: '-85115611272570', found: '-61566971689846'