QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#354722#5094. 小 H 分糖果ANIG0 56ms265392kbC++235.9kb2024-03-15 21:47:302024-03-15 21:47:31

Judging History

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

  • [2024-03-15 21:47:31]
  • 评测
  • 测评结果:0
  • 用时:56ms
  • 内存:265392kb
  • [2024-03-15 21:47:30]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll __int128
const int N=1e5+5,inf=1e18;
struct gj{
    signed a;signed b;
    friend gj operator+(gj a,gj b){
        gj c={a.a+b.a,a.b+b.b};
        while(c.b>inf)c.a++,c.b-=inf;
        while(c.b<0)c.a--,c.b+=inf;
        return c;
    }
    friend gj operator-(gj a,gj b){
        gj c={a.a-b.a,a.b-b.b};
        while(c.b>inf)c.a++,c.b-=inf;
        while(c.b<0)c.a--,c.b+=inf;
        return c;
    }
    ll gets(){
        return (ll)a*inf+b;
    }
};
struct msg{
    signed sm;
    int rs;
    gj pf;
    friend msg operator+(msg a,msg b){
        return {a.sm+b.sm,a.rs+b.rs,a.pf+b.pf};
    }
    friend msg operator-(msg a,msg b){
        return {a.sm-b.sm,a.rs-b.rs,a.pf-b.pf};
    }
}emp;
namespace trr{
    struct node{
        signed lson,rson;
        msg sm;
    }p[N*250];
    vector<int>bk;
    int nw(){
        int y=bk.back();bk.pop_back();
        return y;
    }
    void init(int x){
        if(!p[x].lson)p[x].lson=nw();
        if(!p[x].rson)p[x].rson=nw();
    }
    void add(int x,int l,int r,msg sm,int nl=1,int nr=1e5){
        if(l<=nl&&r>=nr){
       //     cout<<"add"<<x<<" "<<nl<<" "<<nr<<" "<<sm.sm<<endl;
            p[x].sm=p[x].sm+sm;
            return;
        }
        int mid=nl+nr>>1;
        init(x);
        if(l<=mid)add(p[x].lson,l,r,sm,nl,mid);
        if(r>mid)add(p[x].rson,l,r,sm,mid+1,nr);
        if(p[x].lson&&p[p[x].lson].sm.sm==0&&!p[p[x].lson].lson&&!p[p[x].lson].rson)bk.push_back(p[x].lson),p[x].lson=0;
        if(p[x].rson&&p[p[x].rson].sm.sm==0&&!p[p[x].rson].lson&&!p[p[x].rson].rson)bk.push_back(p[x].rson),p[x].rson=0;
    }
    msg gets(int x,int d,int nl=1,int nr=1e5){
        if(!d)return emp;
        if(!x)return emp;
     //   cout<<nl<<" "<<nr<<" "<<d<<" "<<p[x].sm.sm<<endl;
        if(nl==nr)return p[x].sm;
        int mid=nl+nr>>1;
        if(d<=mid)return gets(p[x].lson,d,nl,mid)+p[x].sm;
        else return gets(p[x].rson,d,mid+1,nr)+p[x].sm;
    }
}
namespace tr{
    struct node{
        signed lson,rson,rt;
    }p[100*N];
    int idx;
    void init(int x,int op){
        if(!p[x].lson&&op==0)p[x].lson=++idx,p[idx].rt=trr::nw();
        if(!p[x].rson&&op==1)p[x].rson=++idx,p[idx].rt=trr::nw();
    }
    void add(int x,int d,int l,int r,int sm,int nl=0,int nr=1e9){
        if(sm>0)trr::add(p[x].rt,l,r,{sm,sm*d,{0,sm*d*d}});
        else trr::add(p[x].rt,l,r,{sm,sm*d,{-1,inf+sm*d*d}});
      //  cout<<x<<" "<<nl<<"-"<<nr<<" "<<p[x].rt<<" "<<p[x].lson<<endl;
        if(nl==nr)return;
        int mid=nl+nr>>1;
        if(d<=mid)init(x,0);
        else init(x,1);
        if(d<=mid)add(p[x].lson,d,l,r,sm,nl,mid);
        else add(p[x].rson,d,l,r,sm,mid+1,nr);
    }
    msg gets(int x,int l,int r,int d,int nl=0,int nr=1e9){
        if(!x||!d)return emp;
       // cout<<"ok"<<endl;
        if(l<=nl&&r>=nr)return trr::gets(p[x].rt,d);
        int mid=nl+nr>>1;
        if(r<=mid)return gets(p[x].lson,l,r,d,nl,mid);
        if(l>mid)return gets(p[x].rson,l,r,d,mid+1,nr);
        return gets(p[x].lson,l,r,d,nl,mid)+gets(p[x].rson,l,r,d,mid+1,nr);
    }
    int solve(int x,int x1,int x2,int x3,int x4,int k,int he,int sz,int nl=0,int nr=1e9){
        if(nl==nr)return nl;
        int mid=nl+nr>>1;
        msg c=trr::gets(p[p[x].rson].rt,x1)+trr::gets(p[p[x].rson].rt,x2)-trr::gets(p[p[x].rson].rt,x3)-trr::gets(p[p[x].rson].rt,x4);
        int ans=c.rs+he-mid*(c.sm+sz);
       // cout<<nl<<" "<<nr<<" "<<mid<<" "<<c.sm<<" "<<c.rs<<" "<<ans<<" "<<trr::gets(p[p[x].rson].rt,x2).rs<<endl;
        if(ans<=k)return solve(p[x].lson,x1,x2,x3,x4,k,he+c.rs,sz+c.sm,nl,mid);
        return solve(p[x].rson,x1,x2,x3,x4,k,he,sz,mid+1,nr);
    }
}
int n,m,w[N],fa[N],dep[N],mk[N],dfn[N],eds[N],idx,fs[N][20];
ll al;
vector<int>p[N];
void dfs(int x){
    mk[x]=1;dfn[x]=++idx;
    for(int i=1;i<=18;i++)fs[x][i]=fs[fs[x][i-1]][i-1];
    for(auto c:p[x]){
        if(mk[c])continue;
        dep[c]=dep[x]+1;
        fs[c][0]=x;
        dfs(c);
        fa[c]=x;
    }
    mk[x]=0;eds[x]=idx;
}
int up(int x,int k){
    while(k){
        x=fs[x][__lg(k&-k)];
        k^=k&-k;
    }
    return x;
}
int lca(int a,int b){
    if(dep[a]>dep[b])swap(a,b);
    b=up(b,dep[b]-dep[a]);
    if(a==b)return a;
    for(int i=18;i>=0;i--){
        if(fs[a][i]!=fs[b][i])a=fs[a][i],b=fs[b][i];
    }
    return fs[a][0];
}
msg gets(int x,int l=0,int r=1e9){
    return tr::gets(1,l,r,dfn[x]);
}
void print(ll x){
    if(x>9)print(x/10);
    putchar(x%10+'0');
}
signed main(){
    //fin();fout();
    cin>>n>>m;
    for(int i=1;i<250*N;i++)trr::bk.push_back(i);
    for(int i=1;i<n;i++){
        int x,y;
        cin>>x>>y;
        p[x].push_back(y);
        p[y].push_back(x);
    }
    tr::idx=1;
    tr::p[1].rt=trr::nw();
    dfs(1);
    for(int i=1;i<=n;i++)cin>>w[i],tr::add(1,w[i],dfn[i],eds[i],1),al+=w[i]*w[i];
    while(m--){
        int op,x,y,k;
        cin>>op>>x>>y;
        if(op==1){
            tr::add(1,w[x],dfn[x],eds[x],-1);
            al-=w[x]*w[x];
            w[x]=y;
            al+=y*y;
            tr::add(1,y,dfn[x],eds[x],1);
        }else{
            cin>>k;
            int z=lca(x,y);
            ll res=0,ans=0;
            msg cc=gets(x)+gets(y)-gets(z)-gets(fa[z]);
            res=al-cc.pf.gets();
        //    cout<<cc.sm<<" "<<cc.rs<<" "<<x<<" "<<y<<" "<<z<<endl;
            if(cc.rs<=k){
                print(res);
                puts("");
                continue;
            }
            int t=tr::solve(1,dfn[x],dfn[y],dfn[z],dfn[fa[z]],k,0,0);
            cc=gets(x,0,t)+gets(y,0,t)-gets(z,0,t)-gets(fa[z],0,t);
            res+=cc.pf.gets();
            cc=gets(x,t+1,1e9)+gets(y,t+1,1e9)-gets(z,t+1,1e9)-gets(fa[z],t+1,1e9);
            ans=cc.rs-t*cc.sm;res+=(ll)t*t*cc.sm;
            res-=(k-ans)*(2*t-1);
            print(res);
            puts("");
        }
    }
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 56ms
memory: 265392kb

input:

866 841
1 864
4 153
9 559
10 410
11 336
12 417
14 666
18 241
21 184
22 849
23 40
25 783
26 189
28 329
29 216
31 864
34 581
40 131
42 625
45 744
47 723
50 633
51 447
52 454
53 88
55 619
60 259
62 680
67 126
72 371
73 742
80 196
81 536
82 647
85 254
87 172
88 489
93 708
94 227
95 340
96 7
7 91
97 594
...

output:

285125508
285374449
285871392
285072359
284419704
284843737
284692039
-
1000000001772562998
285174668
285019779
284651455
287282253
287175619
'
285369672
284880507
285404741
284913527
286053317
288622563
286960150
6000000000616971595
288326074
286937403
287883097
288535226
288195055
,
288632989
3000...

result:

wrong answer 8th lines differ - expected: '284936099', found: '-'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Runtime Error

Test #6:

score: 0
Runtime Error

input:

87080 98363
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52...

output:


result:


Subtask #4:

score: 0
Skipped

Dependency #1:

0%