QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#629315#7608. Cliquesrotcar07TL 146ms28656kbC++202.9kb2024-10-11 10:36:032024-10-11 10:36:04

Judging History

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

  • [2024-10-11 10:36:04]
  • 评测
  • 测评结果:TL
  • 用时:146ms
  • 内存:28656kb
  • [2024-10-11 10:36:03]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
constexpr int maxn=2e5+5;
int n,fa[maxn],dfn[maxn],top[maxn],tqm,sz[maxn],son[maxn],q,dep[maxn];
vector<int> e[maxn];
void dfs(int p,int f){
    dep[p]=dep[f]+1;
    fa[p]=f;sz[p]=1;
    for(int x:e[p])if(x!=f) dfs(x,p),sz[son[p]]>=sz[x]?:son[p]=x;
}
void dfs2(int p,int f){
    dfn[p]=++tqm;top[p]=f;
    if(son[p]) dfs2(son[p],f);
    for(int i:e[p])if(i!=fa[p]&&i!=son[p]) dfs2(i,i);
}
constexpr int mod=1e9+7,i2=5e8+4;
inline void reduce(int&x){x>=mod&&(x-=mod);}
inline int red(const int x){return (x>mod?x-mod:x);}
struct node{
    int x,y,g;
    node(int x=1,int y=1,int g=1):x(x),y(y),g(g){}
    inline node operator + (const node&o)const{return node(red(x+o.x),red(y+o.y));}
    inline operator int()const{return red(x+mod-y);}
    inline void app(const long long w){x=x*w%mod,g=g*w%mod;y=y*w%mod;}
}t[maxn<<2];
#define ls p<<1
#define rs p<<1|1
inline void pu(int p){t[p]=t[ls]+t[rs];}
inline void pd(int p){if(t[p].g!=1)t[ls].app(t[p].g),t[rs].app(t[p].g),t[p].g=1;}
void modify(int p,int l,int r,int ql,int qr,int val){
    if(ql<=l&&r<=qr) return t[p].app(val),void();
    int mid=l+r>>1;pd(p);
    if(ql<=mid) modify(ls,l,mid,ql,qr,val);
    if(qr>mid) modify(rs,mid+1,r,ql,qr,val);pu(p);
}
void modify(int p,int l,int r,int k,int val){
    if(l==r) return t[p].y=t[p].y*1ll*val%mod,void();
    int mid=l+r>>1;pd(p);
    if(k<=mid) modify(ls,l,mid,k,val);
    else modify(rs,mid+1,r,k,val);
    pu(p);
}
int query(int p,int l,int r,int ql,int qr){
    if(ql<=l&&r<=qr) return t[p];
    int mid=l+r>>1;pd(p);
    int res=0;
    if(ql<=mid) res=query(ls,l,mid,ql,qr);
    if(qr>mid) reduce(res+=query(rs,mid+1,r,ql,qr));
    return res;
}
int query(int p,int l,int r,int k){
    if(l==r) return t[p].y;
    int mid=l+r>>1;pd(p);
    return k<=mid?query(ls,l,mid,k):query(rs,mid+1,r,k);
}
int main(){
    std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n;for(int i=1,u,v;i<n;i++) cin>>u>>v,e[u].push_back(v),e[v].push_back(u);
    dfs(1,0);dfs2(1,1);cin>>q;
    int ans=0;
    while(q--){
        char op;int u,v;cin>>op>>u>>v;bool w=op=='+';
        int res=0;
        auto f=[&](int x,int u,bool w){
            if(!w) modify(1,1,n,dfn[x],dfn[u],i2);
            reduce(res+=query(1,1,n,dfn[x],dfn[u]));
            if(w) modify(1,1,n,dfn[x],dfn[u],2);
        };
        while(top[u]!=top[v]){
            if(dep[top[u]]<dep[top[v]]) swap(u,v);
            int x=top[u];f(x,u,w);u=fa[x];
        }
        if(dfn[u]>dfn[v]) swap(u,v);
        f(u,v,w);
        if(w){
            modify(1,1,n,dfn[u],i2);
            reduce(res+=query(1,1,n,dfn[u]));
            reduce(ans+=res);
        }
        else{
            reduce(res+=query(1,1,n,dfn[u]));
            modify(1,1,n,dfn[u],2);
            reduce(ans+=mod-res);
        }
        cout<<ans<<'\n';
    }
}

详细

Test #1:

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

input:

5
1 2
5 1
2 3
4 2
6
+ 4 5
+ 2 2
+ 1 3
- 2 2
+ 2 3
+ 4 4

output:

1
3
7
3
7
9

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 17940kb

input:

20
8 7
19 10
12 14
3 16
17 13
7 10
5 6
1 9
15 12
5 2
16 13
3 11
20 14
18 6
1 14
16 20
11 10
3 4
20 6
30
+ 10 20
+ 14 20
+ 12 17
- 14 20
- 12 17
+ 4 6
+ 8 20
+ 3 6
- 10 20
+ 2 17
+ 1 16
+ 6 10
+ 9 10
+ 5 15
+ 7 8
- 7 8
+ 2 5
+ 3 18
+ 1 20
+ 8 16
- 3 18
- 5 15
+ 4 20
+ 14 16
- 4 6
+ 8 19
+ 4 7
- 1 16
...

output:

1
3
7
3
1
3
7
15
7
15
31
63
127
255
257
255
259
515
1027
1283
643
385
769
1537
769
785
865
481
737
369

result:

ok 30 lines

Test #3:

score: 0
Accepted
time: 146ms
memory: 28656kb

input:

131072
3641 72511
10338 18718
8949 15478
79108 62887
42154 28540
65359 102645
93744 48493
3277 103211
43542 61315
93315 118634
24021 57937
31034 436
30411 6208
1388 25159
93424 128520
119820 87281
5860 97559
59648 8225
57244 58766
119685 13716
130165 60958
79806 116338
97486 80167
101963 95499
51263...

output:

1
2
4
8
13
27
43
67
119
215
359
423
847
1167
1935
2447
2975
4511
5023
8639
6911
13759
12991
15103
23295
43775
47999
91647
85375
167295
169343
301695
600703
666239
1332351
2664575
2678911
2687103
5374207
5898495
11731455
11731967
22283263
22807551
21955327
43910399
43911423
44207359
44207360
44469504...

result:

ok 46368 lines

Test #4:

score: -100
Time Limit Exceeded

input:

131073
52869 122762
63296 22816
9889 23435
9452 109352
95210 125504
104234 105533
42422 123259
31189 77343
88679 25422
13860 61424
49617 27681
71743 44108
2612 55383
89088 79225
49508 86216
45176 26493
60940 104568
10733 11047
118351 29678
6184 107897
69675 73910
39495 78910
14871 125941
64731 68683...

output:

1
3
7
11
15
19
31
35
71
143
287
511
1023
1031
1991
1031
1287
1927
2055
3151
4687
4943
9295
17999
18959
37775
73759
74559
148031
148095
296191
151743
160063
110399
110463
127103
63615
63679
64191
90303
138431
212479
212607
423551
423583
426527
770591
865599
1054015
1794879
3587647
3587903
5168959
743...

result: