ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#214649 | #2376. 树与异或 | naroto2022 | 30 | 2ms | 1232kb | C++ | 1.7kb | 2024-11-20 22:40:27 | 2024-11-20 23:11:05 |
answer
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long
using namespace std;
const int mod=1e9+7;
const int MN=1e6+5;
const int rt=1;
ll n,q,val[MN],head[MN],tot,f[MN][3],deep[MN],x,ans[MN],res;
struct edge{ll to,nxt;}e[MN<<1];
void write(ll n){if(n<0){putchar('-');write(-n);return;}if(n>9)write(n/10);putchar(n%10+'0');}
ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
void add(ll u, ll v){e[++tot].nxt=head[u];head[u]=tot;e[tot].to=v;}
void build(ll u, ll fa){
deep[u]=deep[fa]+1;
for(int i=head[u]; i; i=e[i].nxt){
ll v=e[i].to;
if(v==fa) continue;
build(v,u);
f[u][1]^=f[v][0];f[u][2]^=f[v][1];
}
}
void dfs(ll u){
for(int i=head[u]; i; i=e[i].nxt){
ll v=e[i].to;
if(deep[v]<=deep[u]) continue;
f[v][2]^=f[v][0];
f[v][1]^=f[u][0];f[v][2]^=f[u][1];
dfs(v);
}
}
void change(ll u, ll lst, ll llst, ll step, ll v1, ll v2){
if(step==3) return;
for(int i=head[u]; i; i=e[i].nxt){
ll v=e[i].to;
if(v==lst||v==llst) continue;
f[v][step]^=v1;f[v][step]^=v2;
change(v,u,lst,step+1,v1,v2);
}
}
int main(){
n=read();q=read();
for(int i=1; i<=n; i++) val[i]=f[i][0]=read();
for(int i=1; i<n; i++){
ll u=read(),v=read();
add(u,v);add(v,u);
}
build(rt,0);dfs(rt);
for(int i=1; i<=q; i++){
x=read();ll num=f[x][0];f[x][0]=read();
change(x,0,0,1,num,f[x][0]);
ans[i]=f[x][0]^f[x][1]^f[x][2];
}
for(int i=1; i<=q; i++) res=(res+i*i%mod*ans[i]%mod)%mod;
write(res);putchar('\n');
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 0ms
memory: 1232kb
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: 0ms
memory: 1228kb
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: 2ms
memory: 1232kb
input:
1000 994 978408146 31636243 945016735 512129400 92881624 689719983 649699545 964802847 100094796 941...
output:
250985726
result:
ok 1 number(s): "250985726"
Test #4:
score: 0
Time Limit Exceeded
input:
100000 99994 194640838 777315353 114710204 754050649 372592717 210279787 542056883 638262010 7998293...
output:
result:
Test #5:
score: 0
Time Limit Exceeded
input:
100000 100000 458877901 215980825 777376132 863774865 34430451 828397116 94813690 931300514 10548367...
output:
result:
Test #6:
score: 0
Time Limit Exceeded
input:
100000 99991 56406146 757409402 876799234 573444553 883023109 326033134 328116703 623771278 41269529...
output:
result:
Test #7:
score: 0
Time Limit Exceeded
input:
1000000 999998 814298729 663807029 628693441 45635199 148428014 913833778 1034283337 600802841 58045...
output:
result:
Test #8:
score: 0
Time Limit Exceeded
input:
1000000 999993 112899321 425466951 471078924 857561298 449437156 710041336 1033281580 23390934 44420...
output:
result:
Test #9:
score: 0
Time Limit Exceeded
input:
1000000 1000000 273043260 1042513348 30746190 864557483 187812119 562837674 263192174 364182254 3537...
output:
result:
Test #10:
score: 0
Time Limit Exceeded
input:
1000000 999995 83067658 1064735371 623243759 757522311 539363923 924297206 75317137 543750195 822898...