QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#221870 | #7608. Cliques | ucup-team1525# | TL | 0ms | 24436kb | C++17 | 2.7kb | 2023-10-21 14:56:21 | 2023-10-21 14:56:22 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=4e5;
const int mod=1e9+7,iv=(mod+1)/2;
int inc(int x,int y){ return (x+=y)>=mod?x-mod:x; }
int sub(int x,int y){ return (x-=y)<0?x+mod:x; }
int n;
vector<int> e[N+5];
int fa[N+5];
int sz[N+5],son[N+5],top[N+5];
int dfn[N+5],rdfn[N+5],dfns;
struct Segtr{
int tr[N*4+5],tag[N*4+5];
#define lc id<<1
#define rc id<<1|1
void build(int l,int r,int id){
tag[id]=1;
if(l==r){
tr[id]=rdfn[l]<=n?1:mod-1;
return;
}
int mid=l+r>>1;
build(l,mid,lc);
build(mid+1,r,rc);
tr[id]=inc(tr[lc],tr[rc]);
}
void push_down(int id){
if(tag[id]==1) return;
int v=tag[id]; tag[id]=1;
tr[lc]=1ll*tr[lc]*v%mod; tag[lc]=1ll*tag[lc]*v%mod;
tr[rc]=1ll*tr[rc]*v%mod; tag[rc]=1ll*tag[rc]*v%mod;
}
void add(int l,int r,int st,int en,int x,int id){
if(st<=l&&en>=r){
tr[id]=1ll*tr[id]*x%mod;
tag[id]=1ll*tag[id]*x%mod;
return;
}
int mid=l+r>>1;
push_down(id);
if(st<=mid) add(l,mid,st,en,x,lc);
if(en>mid) add(mid+1,r,st,en,x,rc);
tr[id]=inc(tr[lc],tr[rc]);
}
}T;
void dfs1(int u,int tf){
fa[u]=tf; sz[u]=1;
for(auto v:e[u]){
if(v!=tf){
dfs1(v,u);
sz[u]+=sz[v];
if(sz[v]>sz[son[u]]) son[u]=v;
}
}
}
void dfs2(int u,int tf){
top[u]=tf;
dfn[u]=++dfns; rdfn[dfns]=u;
if(!son[u]) return;
dfs2(son[u],son[u]);
for(auto v:e[u]){
if(v!=fa[u]&&v!=son[u])
dfs2(v,v);
}
}
int LCA(int u,int v){
while(top[u]!=top[v]){
if(dfn[top[u]]<dfn[top[v]]) swap(u,v);
u=fa[top[u]];
}
return dfn[u]<dfn[v]?u:v;
}
void work(int u,int v,int x){
int lca=LCA(u,v);
while(top[u]!=top[lca]){
T.add(1,dfns,dfn[top[u]],dfn[u],x,1);
u=fa[top[u]];
}
if(u!=lca) T.add(1,dfns,dfn[lca]+1,dfn[u],x,1);
while(top[v]!=top[lca]){
T.add(1,dfns,dfn[top[v]],dfn[v],x,1);
v=fa[top[v]];
}
T.add(1,dfns,dfn[lca],dfn[v],x,1);
}
int main(){
scanf("%d",&n);
for(int i=1,u,v;i<n;i++){
scanf("%d %d",&u,&v);
e[u].push_back(i+n);
e[v].push_back(i+n);
e[i+n].push_back(u);
e[i+n].push_back(v);
}
dfs1(1,0);
dfs2(1,1);
T.build(1,dfns,1);
int q;
scanf("%d",&q);
while(q--){
char s[5];
int u,v;
scanf("%s %d %d",s,&u,&v);
if(s[0]=='+') work(u,v,2);
else work(u,v,iv);
printf("%d\n",sub(T.tr[1],1));
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 24436kb
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: 21948kb
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: -100
Time Limit Exceeded
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...