QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#587830#9373. Query on TreeMiniLongWA 39ms22300kbC++178.9kb2024-09-24 21:51:482024-09-24 21:51:49

Judging History

你现在查看的是最新测评结果

  • [2024-09-24 21:51:49]
  • 评测
  • 测评结果:WA
  • 用时:39ms
  • 内存:22300kb
  • [2024-09-24 21:51:48]
  • 提交

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){
    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){
    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)), debug("[%d,%d]\n", L, l - 1);
        if(r < R) res = max(res, f.query(1, 1, n, r + 1, R)), debug("[%d,%d]\n", r + 1, R);
        return res;
    }
    debug("[%d,%d]\n", L, R);
    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;
                debug("[%d,%d] %d\n", L, R, val);
                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;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 22276kb

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: 0
Accepted
time: 28ms
memory: 22300kb

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
-85115611272570
-85114714781312
96492412785032
-20428913156111
-20428197540063
96491742171666
-14945310996805
96491180203461
-...

result:

ok 200000 lines

Test #3:

score: -100
Wrong Answer
time: 39ms
memory: 19972kb

input:

10000
4 32
-1057044448491 -93293078803919 -24212938548357 74427756948193
1 3
1 2
3 4
3 1 -82365883
1 2 9 -730670945
2 4 2 -618745828
2 1 2 774032949
3 3 6733210
3 3 -843683844
3 1 327410390
3 1 -865685600
1 4 6 -951367966
3 2 107763506
1 3 2 721870187
2 3 3 -530847597
2 2 1 451029291
3 2 231297873
3...

output:

74427674582310
GG
74427055836482
74427829869431
-24212858893909
-24213702577753
74428157279821
74427291594221
GG
-93294273345019
-93293551474832
74426760746624
-1057589620769
-93293399995265
74426065455072
74426267352173
-93293391928270
74426742959101
74427616219598
-1058067327518
-24216157529247
-2...

result:

wrong answer 5th lines differ - expected: '74427836602641', found: '-24212858893909'