QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#686749#9492. 树上简单求和xlwang0 0ms0kbC++145.1kb2024-10-29 15:31:032024-10-29 15:31:04

Judging History

This is the latest submission verdict.

  • [2024-10-29 15:31:04]
  • Judged
  • Verdict: 0
  • Time: 0ms
  • Memory: 0kb
  • [2024-10-29 15:31:03]
  • Submitted

answer

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define fr(i,j,k) for(register int i=j;i<=k;++i)
#define rf(i,j,k) for(register int i=j;i>=k;--i)
#define foredge(i,j) for(register int i=head[j];i;i=e[i].nxt)
#define pb push_back
#define Times printf("Time:%.3lf\n",clock()/CLOCKS_PER_SEC)
#define pii pair<int,int>
#define mk make_pair
using namespace std;
inline int read(){
	int x=0;
	bool f=0;
	char c=getchar();
	while(!isdigit(c)) f|=(c=='-'),c=getchar();
	while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar();
	return f?-x:x;
}
inline void write(int x){
    if(x<0){putchar('-');x=-x;}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
inline void writeln(int x){write(x); puts("");}
inline void writepl(int x){write(x); putchar(' ');}
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline int randfind(int l,int r){return rnd()%(r-l+1)+l;}
//inline void init(){
//	int t=read();
//	while(t--) work();
//}
const int Maxn=2e5+10;
vector<int> vc[Maxn];
int father[Maxn];
int top[Maxn],son[Maxn],siz[Maxn],dep[Maxn];
int n,q;
ull a[Maxn];
inline void init(){
    cin>>n>>q;
    fr(i,1,n) cin>>a[i];
    fr(i,1,n-1){
        int x,y;
        cin>>x>>y;
        vc[x].pb(y);vc[y].pb(x);
    }
    fr(i,1,n-1){int x,y;cin>>x>>y;if(x>y) swap(x,y);assert((x+1)==y);}
}
inline void dfs(int x,int fa){
    dep[x]=dep[fa]+1;siz[x]=1;son[x]=0;father[x]=fa;
    for(auto y:vc[x]) if(y!=fa){
        dfs(y,x);
        if(!son[x] || siz[y]>siz[son[x]]) son[x]=y;
    }
}
inline void dfs1(int x,int fa,int tp){
    // cout<<"top:"<<x<<' '<<fa<<' '<<tp<<endl;
    top[x]=tp;
    if(son[x]) dfs1(son[x],x,tp);
    for(auto y:vc[x]) if(y!=fa && y!=son[x]) dfs1(y,x,y);
}
namespace sgt{
    ull s[Maxn<<2];
    int minn[Maxn<<2],num[Maxn<<2];
    inline int ls(int x){return x<<1;}
    inline int rs(int x){return x<<1|1;}
    inline void pushup(int x){s[x]=s[ls(x)]+s[rs(x)];}
    inline void build(int x,int l,int r){
        if(l==r){s[x]=a[l];return;}
        int mid;mid=(l+r)>>1;
        build(ls(x),l,mid);build(rs(x),mid+1,r);
        pushup(x);
    }
    inline int getmin(int x,int y){if(dep[x]<dep[y]) return x;return y;}
    inline void prepare(int x,int l,int r){
        if(l==r){minn[x]=top[l];num[x]=1;return;}
        int mid;mid=(l+r)>>1;
        prepare(ls(x),l,mid);prepare(rs(x),mid+1,r);
        minn[x]=getmin(minn[ls(x)],minn[rs(x)]);num[x]=0;
        if(minn[ls(x)]==minn[x]) num[x]+=num[ls(x)];
        if(minn[rs(x)]==minn[x]) num[x]+=num[rs(x)];
        // cout<<"tree:"<<x<<' '<<l<<' '<<r<<' '<<minn[x]<<' '<<num[x]<<endl;
        return;
    }
    ull tag[Maxn<<2];
    inline void change(int x,ull k){s[x]+=k*num[x];tag[x]+=k;}
    inline void pushdown(int x){
        if(tag[x]){
            if(minn[x]==minn[ls(x)]) change(ls(x),tag[x]);
            if(minn[x]==minn[rs(x)]) change(rs(x),tag[x]);
            tag[x]=0;
        }
    }
    inline void update(int ql,int qr,int l,int r,int x,ull k){
        if(ql<=l && r<=qr){change(x,k);return;}
        int mid;mid=(l+r)>>1;pushdown(x);
        if(ql<=mid) update(ql,qr,l,mid,ls(x),k);
        if(mid<qr) update(ql,qr,mid+1,r,rs(x),k);
        pushup(x);
    }
    inline void jia(int ql,int l,int r,int x,ull k){
        if(l==r){s[x]+=k;return;}
        int mid;mid=(l+r)>>1;pushdown(x);
        if(ql<=mid) jia(ql,l,mid,ls(x),k);
        else jia(ql,mid+1,r,rs(x),k);
        pushup(x);
    }
    inline ull query(int ql,int qr,int l,int r,int x){
        if(ql<=l && r<=qr) return s[x];
        int mid;mid=(l+r)>>1;pushdown(x);
        ull ans=0;
        if(ql<=mid) ans+=query(ql,qr,l,mid,ls(x));
        if(mid<qr) ans+=query(ql,qr,mid+1,r,rs(x));
        return ans;
    }
}
inline int getlca(int x,int y){
    while(top[x]!=top[y]){
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        x=father[top[x]];
    }
    if(dep[x]>dep[y]) swap(x,y);return x;
}
inline void change(int x,ull k){
    while(x){
        // cout<<"*"<<top[x]<<' '<<x<<endl;
        sgt::update(top[x],x,1,n,1,k);
        x=father[top[x]];
    }
}
inline void update(int x,int y,ull k){
    int llca=getlca(x,y);
    ull ans=0;
    fr(i,x,y) ans+=a[i];
    // cout<<ans<<endl;
    // ull V=14172501950440445025ull;
    // fr(i,0,3000) if(ans+k*i==V){
    //     cout<<"*"<<i<<endl;
    // }
    change(x,k);
    change(y,k);
    // cout<<dep[x]+dep[y]-2*dep[llca]+1<<endl;
    // cout<<x<<' '<<y<<' '<<llca<<endl;
    sgt::jia(llca,1,n,1,k);
    change(llca,-2*k);
    return;
}
inline void query(int x,int y){
    cout<<sgt::query(x,y,1,n,1)<<'\n';
    return;
}
inline void work(){
    dfs(1,0);dfs1(1,0,1);
    sgt::build(1,1,n);sgt::prepare(1,1,n);
    while(q--){
        // cerr<<q<<endl;
        int x,y;ull k;
        cin>>x>>y>>k;
        if(x>y) swap(x,y);
        // cout<<x<<' '<<y<<' '<<getlca(x,y)<<' '<<k<<endl;
        update(x,y,k);query(x,y);
    }
}
signed main(){
	// freopen("input.in","r",stdin);
	// freopen("output.out","w",stdout);
    cin.tie(0)->sync_with_stdio(0);
    init();work();
    // printf("\nTIME:%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}

详细

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 0
Runtime Error

input:

3000 3000
7236742292501328495 17973811477309806363 16075782662531676171 17971236571771878676 11392080645527132110 3685563455925680459 9773593720088356683 8313828403245053795 7736401634567449043 1634817828009987181 6951124933529719486 12775126714635387213 15460977209223753216 397573676785925632 31372...

output:


result:


Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Runtime Error

Test #21:

score: 0
Runtime Error

input:

200000 200000
622783158027686223 2242697872372232537 8481648430436878777 10092474834140799044 15403999682625301609 12614289513474949582 9180944589267018841 7823784919308285798 8257785171198951273 5134508521895120821 8041682272181381093 3835432206618893170 2653803171409877650 5589823419153460372 1007...

output:


result:


Subtask #5:

score: 0
Runtime Error

Test #27:

score: 0
Runtime Error

input:

200000 200000
1958469220619413759 14991498002015735322 6054491201406941902 18206143187746582567 15082377615826460430 2936248617457291604 10073577150351675920 16534472678586906457 2207599132486246393 10301540360769075442 1492580560381080472 551692353431379140 13238280352539145808 8462626987240986565 ...

output:


result:


Subtask #6:

score: 0
Time Limit Exceeded

Test #34:

score: 0
Time Limit Exceeded

input:

200000 200000
6794776813641982926 1561596256197101737 10910039723053043515 7892247858295192798 12233819960547881004 17695389034783066733 9173201689566865598 17626618141377486739 7358781671024283919 6787559733384974662 3884392438269280436 14872846228351316833 9037842441501571648 14299818404271084016 ...

output:

5519324519442957729
13462861144392030499
8898301730697138469
4148979398311169421
15090246851327208067
8628707816538336707
13867297907038176506
10296428352441857103
15654304415409320656
7404566644919251615
9870876264015800597
6356224262148620783
249874952637342736
9023132497407647441
1416175985367538...

result:


Subtask #7:

score: 0
Skipped

Dependency #1:

0%