QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#40605#4381. TreasureCrysflyAC ✓1549ms108084kbC++114.9kb2022-07-22 22:45:102022-07-22 22:45:12

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-07-22 22:45:12]
  • Judged
  • Verdict: AC
  • Time: 1549ms
  • Memory: 108084kb
  • [2022-07-22 22:45:10]
  • Submitted

answer

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
//#define int long long
//?????
using namespace std;
inline int read()
{
    char c=getchar();int x=0;bool f=0;
    for(;!isdigit(c);c=getchar())f^=!(c^45);
    for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
    if(f)x=-x;return x;
}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;

#define maxn 200005
#define inf 0x3f3f3f3f

int n,m,Q,ff[maxn],orz[maxn];

int c[maxn];
ll val[maxn];
int fa[maxn];
int getf(int x){
    while(x^ff[x])x=ff[x]=ff[ff[x]];
    return x;
}

vi e[maxn];

struct node{
    int u,v,w;
    bool operator <(const node&b)const{return w<b.w;}
}a[maxn];

int anc[maxn][19],dep[maxn],dfn[maxn],que[maxn],out[maxn],idx;
void dfs(int u){
//    cout<<"dfs "<<u<<endl;
    anc[u][0]=fa[u];
    dep[u]=dep[fa[u]]+1;
    dfn[u]=++idx,que[idx]=u;
    For(i,1,18)anc[u][i]=anc[anc[u][i-1]][i-1];
    for(auto v:e[u])dfs(v);
    out[u]=idx;
}
int jump(int x,int k){
    Rep(i,18,0)
        if(anc[x][i] && val[anc[x][i]]<=k)
            x=anc[x][i];
    return x;
}
int lca(int u,int v){
    if(dep[u]<dep[v])swap(u,v);
    Rep(i,18,0)if(dep[anc[u][i]]>=dep[v])u=anc[u][i];
    if(u==v)return u;
    Rep(i,18,0)if(anc[u][i]^anc[v][i])u=anc[u][i],v=anc[v][i];
    return anc[u][0];
}

vi p[maxn];
bool dfncmp(int x,int y){return dfn[x]<dfn[y];}

ll tr[maxn];//tmp[maxn];
inline void add(int x,int y){
 //   tmp[x]+=y;
    for(;x<=idx;x+=x&-x)tr[x]+=y;
}
inline ll askpre(int x){
    ll res=0;
    for(;x;x^=x&-x)res+=tr[x];
    return res;
}
inline ll ask(int u){return askpre(out[u])-askpre(dfn[u]-1);}

void Out(){
//    For(i,1,n*2-1)cout<<tmp[dfn[i]]<<" \n"[i==n*2-1];
}

int st[maxn],top;
struct T{
    map<int,int>mp;
    vector<vi>g;
    vi up;
    vi to_u;
    vector<ll> f;
    int id(int u){
        if(!mp.count(u)){
            int o=mp.size();
            f.pb(0),up.pb(0),g.pb({}),to_u.pb(u);
            return mp[u]=o+1;
        }return mp[u];
    }
    
    void adde(int u,int v){
    //    cout<<"ADD "<<u<<" "<<v<<endl;
        u=id(u),v=id(v);
        g[u].pb(v);
        up[v]=u;
    }
    void init(int c){
        mp.clear();
        g.resize(1),f.resize(1),up.resize(1),to_u.resize(1);
        vi o=p[c];
        sort(o.begin(),o.end(),dfncmp);
        top=0;
        for(auto u:o){
            if(!top){st[++top]=u;continue;}
            int l=lca(u,st[top]),pre=0;
            while(top && dep[l]<=dep[st[top]]){
                if(pre) adde(st[top],pre);
                pre=st[top],--top;
            }
            if(pre && l!=pre) adde(l,pre);
            st[++top]=l;
            if(st[top]!=u) st[++top]=u;
        }
        while(top>1) adde(st[top-1],st[top]),--top;
    //    For(i,1,mp.size())
    //        cout<<up[i]<<" "<<to_u[i]<<endl;puts("-----");
    }
    inline void addx(int u,int x){
    //    cout<<"u: "<<u<<endl;
        u=to_u[u];
    //    cout<<"addx "<<u<<" "<<x<<endl;
        add(dfn[u],x);
    }
    void upd(int u,int x){
        u=id(u);
        f[u]+=x;
        addx(u,x);
        if(up[u]) addx(up[u],-x);
        ll now=f[u];
        while(up[u]){
        //    cout<<u<<" -> "<<up[u]<<endl;
            u=up[u];
            if(now<=f[u])break;
            ll delt=now-f[u]; f[u]=now;
            addx(u,delt);
            if(up[u]) addx(up[u],-delt);
        }
    }
    void dele(){
        mp.clear();
        g.resize(1),f.resize(1),up.resize(1),to_u.resize(1);
    }
}t[maxn];

void updata(int u,int x){t[c[u]].upd(u,x);}

void work()
{
    n=read(),m=read(),Q=read();
    
    idx=0;
    For(i,1,n*2)ff[i]=i,e[i].clear(),p[i].clear(),fa[i]=val[i]=tr[i]=0;
    
    For(i,1,n)c[i]=read(),p[c[i]].pb(i);
    For(i,1,n)orz[i]=read();
    
    For(i,1,m)a[i].u=read(),a[i].v=read(),a[i].w=read();
    sort(a+1,a+m+1);
    int np=n;
    For(i,1,m){
        int u=a[i].u,v=a[i].v;
        if(getf(u)==getf(v))continue;
        u=getf(u),v=getf(v);
        fa[u]=fa[v]=++np;
        ff[u]=ff[v]=np; 
        e[fa[u]].pb(u),e[fa[v]].pb(v),val[fa[u]]=a[i].w;
    }
    dfs(np);
//    For(i,1,n)For(j,1,n)cout<<lca(i,j)<<" \n"[j==n]; 
//    puts("done");
    
    For(i,1,n)
        if(p[i].size()) t[i].init(i);
//    puts("done2");

    For(i,1,n) updata(i,orz[i]);
//    Out();
    For(_,1,Q){
        int o=read(),x=read(),y=read();
        if(o==0) updata(x,y),val[x]+=y;
        else {
            int u=jump(x,y);
            printf("%lld\n",ask(u));
        }
    }
    
    For(i,1,n)
        if(p[i].size()) t[i].dele();
}

signed main()
{
//    freopen("data.in","r",stdin);
//    freopen("my.out","w",stdout);
    int T=read();
    while(T--)work();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1549ms
memory: 108084kb

input:

5
100000 200000 100000
9290 4185 4966 4886 8843 6033 6979 8201 2305 2757 8296 7988 9228 6595 7582 6493 8427 4170 5054 9813 9196 5628 8707 9425 7632 4559 8841 4547 9120 2959 9696 7680 9848 7845 9978 9244 9021 1920 7331 9278 9969 8149 9608 8951 7695 9631 8327 4278 9674 9989 9494 7887 7676 5213 4831 88...

output:

860799337
882647427
881768794
896437709
708091348
698196612
891198837
904339854
896254269
902186690
73046
877403687
885140462
902762378
99017
907366777
900836408
892711537
900489867
36897
907937094
861445218
907195542
908810550
851522916
897351601
570500543
783111180
900316384
842886769
907103714
81...

result:

ok 250022 lines