ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#214630 | #2376. 树与异或 | a_sad_soul | 100 | 5403ms | 91080kb | C++11 | 1.8kb | 2024-11-20 21:08:44 | 2024-11-20 23:07:36 |
answer
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e6+10;
int tree[MAXN];
int n,q;
int lowbit(int i){return i&(-i);}
void add(int i,int v){
for(;i<=n;i+=lowbit(i))tree[i]^=v;
}
int query(int i){
int re=0;
for(;i;i-=lowbit(i))re^=tree[i];return re;
}
int Query(int l,int r){
if(l>n||r<=0)return 0;
return query(r)^query(l-1);
}
vector<int>e[MAXN];
int mx1[MAXN],mn1[MAXN],mx2[MAXN],mn2[MAXN];
int bfn[MAXN];
int fa[MAXN];
void dfs(int u,int f){fa[u]=f;
for(int v:e[u])if(v!=f)dfs(v,u);
}int a[MAXN];
void bfs(){
dfs(1,0);
queue<int>q;q.push(1);int cnt=0;
while(!q.empty()){
int u=q.front();q.pop();
bfn[u]=++cnt;
for(int v:e[u])if(v!=fa[u])q.push(v);
}
for(int i=1;i<=n;++i)mn1[i]=mn2[i]=1e9,mx1[i]=mx2[i]=-1e9;
for(int i=1;i<=n;++i){
if(fa[i])mx1[fa[i]]=max(mx1[fa[i]],bfn[i]),mn1[fa[i]]=min(mn1[fa[i]],bfn[i]);
if(fa[fa[i]]){
int x=fa[fa[i]];mx2[x]=max(mx2[x],bfn[i]);mn2[x]=min(mn2[x],bfn[i]);
}
}
for(int i=1;i<=n;++i)add(bfn[i],a[i]);
}
int solve(int i,int v){
add(bfn[i],a[i]);a[i]=v;add(bfn[i],a[i]);
int re=0;
if(fa[i]){re^=Query(mn1[fa[i]],mx1[fa[i]])^a[fa[i]];}
else re^=a[i];
if(fa[fa[i]])re^=a[fa[fa[i]]];
re=re^Query(mn1[i],mx1[i])^Query(mn2[i],mx2[i]);
return re;
}
const int mod = 1e9+7;
typedef long long ll;
int main(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;++i)scanf("%d",&a[i]);
for(int i=1;i<n;++i){int u,v;scanf("%d%d",&u,&v);e[u].push_back(v),e[v].push_back(u);}
bfs();
ll ans=0;
for(ll i=1;i<=q;++i){
int x,y;scanf("%d%d",&x,&y);
ans=(ans+solve(x,y)*i%mod*i%mod)%mod;
}
printf("%lld\n",ans);
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 7ms
memory: 24776kb
input:
1000 994 780089107 4670366 871010085 73730768 526852049 572589363 885190993 1049357301 450850404 460...
output:
503748432
result:
ok 1 number(s): "503748432"
Test #2:
score: 10
Accepted
time: 3ms
memory: 24776kb
input:
1000 999 26885995 181771373 378079250 245122183 660787775 479226046 575400611 257120086 214404794 48...
output:
301019013
result:
ok 1 number(s): "301019013"
Test #3:
score: 10
Accepted
time: 8ms
memory: 24776kb
input:
1000 994 978408146 31636243 945016735 512129400 92881624 689719983 649699545 964802847 100094796 941...
output:
250985726
result:
ok 1 number(s): "250985726"
Test #4:
score: 10
Accepted
time: 80ms
memory: 31244kb
input:
100000 99994 194640838 777315353 114710204 754050649 372592717 210279787 542056883 638262010 7998293...
output:
897014293
result:
ok 1 number(s): "897014293"
Test #5:
score: 10
Accepted
time: 84ms
memory: 32964kb
input:
100000 100000 458877901 215980825 777376132 863774865 34430451 828397116 94813690 931300514 10548367...
output:
351450518
result:
ok 1 number(s): "351450518"
Test #6:
score: 10
Accepted
time: 75ms
memory: 31244kb
input:
100000 99991 56406146 757409402 876799234 573444553 883023109 326033134 328116703 623771278 41269529...
output:
440189625
result:
ok 1 number(s): "440189625"
Test #7:
score: 10
Accepted
time: 1260ms
memory: 91076kb
input:
1000000 999998 814298729 663807029 628693441 45635199 148428014 913833778 1034283337 600802841 58045...
output:
447637970
result:
ok 1 number(s): "447637970"
Test #8:
score: 10
Accepted
time: 1327ms
memory: 91080kb
input:
1000000 999993 112899321 425466951 471078924 857561298 449437156 710041336 1033281580 23390934 44420...
output:
449713864
result:
ok 1 number(s): "449713864"
Test #9:
score: 10
Accepted
time: 1337ms
memory: 91076kb
input:
1000000 1000000 273043260 1042513348 30746190 864557483 187812119 562837674 263192174 364182254 3537...
output:
922464355
result:
ok 1 number(s): "922464355"
Test #10:
score: 10
Accepted
time: 1222ms
memory: 91076kb
input:
1000000 999995 83067658 1064735371 623243759 757522311 539363923 924297206 75317137 543750195 822898...
output:
318242296
result:
ok 1 number(s): "318242296"
Extra Test:
score: 0
Extra Test Passed