QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#686839#9492. 树上简单求和xlwang19 622ms54444kbC++145.6kb2024-10-29 15:59:002024-10-29 15:59:00

Judging History

This is the latest submission verdict.

  • [2024-10-29 15:59:00]
  • Judged
  • Verdict: 19
  • Time: 622ms
  • Memory: 54444kb
  • [2024-10-29 15:59:00]
  • 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;
        siz[x]+=siz[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);
}
int TR;
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){if(minn[x]==TR) 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){
    // int num=0;
    while(x){
        // cout<<"*"<<top[x]<<' '<<x<<endl;
        TR=top[x];
        sgt::update(top[x],x,1,n,1,k);
        x=father[top[x]];
        // ++num;
    }
    
    // cerr<<num<<endl;
}
inline void update(int x,int y,ull k){
    int llca=getlca(x,y);
    ull ans=0;
    // cout<<ans<<endl;
    // ull V=14172501950440445025ull;
    // fr(i,0,3000) if(ans+k*i==V){
    //     cout<<"*"<<i<<endl;
    // }
    // cout<<sgt::query(x,y,1,n,1)<<endl;
    change(x,k);
    // cout<<sgt::query(x,y,1,n,1)<<endl;
    change(y,k);
    // cout<<sgt::query(x,y,1,n,1)<<endl;
    // // cout<<dep[x]+dep[y]-2*dep[llca]+1<<endl;
    // // cout<<x<<' '<<y<<' '<<llca<<endl;
    sgt::jia(llca,1,n,1,k);
    // cout<<sgt::query(x,y,1,n,1)<<endl;
    change(llca,-2*k);
    // cout<<sgt::query(x,y,1,n,1)<<endl;
    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(){
    // return system("fc output.out output1.out");
	// freopen("input.in","r",stdin);
	// freopen("output.out","w",stdout);
    cin.tie(0)->sync_with_stdio(0);
    init();work();
    // cerr<<(double)clock()/CLOCKS_PER_SEC<<endl;
    // 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: 19
Accepted

Test #34:

score: 19
Accepted
time: 437ms
memory: 44528kb

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:

ok 200000 lines

Test #35:

score: 19
Accepted
time: 622ms
memory: 38932kb

input:

200000 200000
11863650499568632095 6646669915142734572 4053375998669045948 14662364203830894482 7319511971537928619 4131498054494361157 7103043576860354080 2730587527012777841 8626012955062792888 7098277606750148625 12990209623538415680 100871355087529915 12267084290544303817 7008468684400973426 856...

output:

11796827338356402281
853302424664654652
564419876379363056
11173501553976506440
2595891684253320024
5590778053561483334
16613470245879883493
5698255402711241072
8125718572356459513
11371252602486101178
7605800585391618993
17939653588292877927
15906636118593883045
14840423959426947118
122409373008752...

result:

ok 200000 lines

Test #36:

score: 19
Accepted
time: 373ms
memory: 54444kb

input:

200000 200000
7819034666999754227 1261434745131385029 4729051582132640442 9603230030483067185 9188148629137054368 4647584887897271213 14878584596185020248 5036501082632549589 13434605396022727436 10747373329537943689 2859138487129973374 17399126170759425906 5170686633178482234 1548518177806164267 68...

output:

13979872615900358449
13089809959604496472
1561607543806976909
704865086165131395
4750114500765789463
8620101000132483891
1061990993707638791
8219417779659049453
16783172337878697026
731502258552473136
15516870835064396649
13412140913912394465
5029558179142432386
9655856473800271062
14498308915283050...

result:

ok 200000 lines

Test #37:

score: 19
Accepted
time: 376ms
memory: 54396kb

input:

200000 200000
13667364256171297683 5643454264481609425 13916992424255487273 16139471110620927446 10964822257543206396 18104319795931117637 6754294313610492941 627150985476406559 9787453073837140391 8330282299925244302 16457178623381098023 2644823686047461659 3971198924127091796 9747389824949735337 1...

output:

11367465309833674818
18362877133708215281
7423548724337603968
8957456044849213851
17833913066069915980
7858235162317128668
12543057704351927321
10505054748074388644
6816176482433035300
2467436539272277421
1679282916199502250
4514431222303891247
8020348968469583082
4250522620562980350
344143146282723...

result:

ok 200000 lines

Test #38:

score: 19
Accepted
time: 600ms
memory: 38948kb

input:

200000 200000
8264785020939323331 5620958879723030311 933008402364228745 5711653520387260744 16167130674961631916 10635243671618608635 7034482071437480120 10254956504177159052 10510387087831623788 8381634740474427698 9506597691312026965 14784451691298216046 15821757099494287606 1919888068508281591 1...

output:

5787014875832445646
9634577796009446262
12314294073540302873
2915216425908688602
7120463906657998875
4286046781319255714
6776880553034928756
7781782119312943753
10261843991161497641
6413565360321098258
15025688638596291097
9526784383328827422
4012177064000612489
1287077077700121461
98702777920648956...

result:

ok 200000 lines

Test #39:

score: 19
Accepted
time: 445ms
memory: 46632kb

input:

200000 200000
11664667196988092805 1811572170904050995 15694419630875459125 12737549840477675073 16755302998318006416 4014818780481352253 5609118000649893828 6256332194728466258 15733576495075669968 17960532384856428505 15897732465031414620 17425753576410476171 4620624862371502705 112264419736513372...

output:

6480474289914832244
5686011217288555755
14160731859453855234
17554436276709117739
2341367826083254204
8545580044567165676
264741062779916095
3300445074446425091
1133253489203829542
15906618027983611292
4617068730941954223
9183813939934374009
17667722659841632674
17058280131349615456
5995389694319663...

result:

ok 200000 lines

Test #40:

score: 19
Accepted
time: 440ms
memory: 42068kb

input:

200000 200000
1537940886958943461 9001579450047645548 5527298164056925772 15445874594277229387 11547996012713764596 2685142508745516922 15898551218448062337 16566357055814699599 15778851736432174335 10222916087451023672 14639095824490451029 13130899683058695675 2954207938828868462 150325623383373666...

output:

6794024285749225899
15444253183026756906
16252225983581025191
18270054647591833460
14669804966517502833
7889126920459519979
11162985402720101082
6093427967053788994
10568703419124160247
10842018941076619829
12114458223856608006
16611571295135208112
6660992456255029750
14854289142138250053
8448421034...

result:

ok 200000 lines

Test #41:

score: 19
Accepted
time: 447ms
memory: 41500kb

input:

200000 200000
9312450956088465409 13091411438344500136 13533801408631028803 397220365455228788 9050318983523848941 14099016315633088068 17718824544128458130 18224580765371017222 16359847342241893914 6209442109030804295 8140737573685484628 16329892452717577856 12435238272437023209 1214903064014288477...

output:

17881136615169673858
10075746974396262535
1101717753396811008
11092187645455964376
312406901260616120
15269190090424605570
16293852523769965660
7287397773622456008
12586683442681630396
10525460771223157010
16483491603350167181
5677129374080663282
17770626863503596216
16241388034502960475
16358076653...

result:

ok 200000 lines

Subtask #7:

score: 0
Skipped

Dependency #1:

0%