UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#214595#2376. 树与异或nodgd40898ms41212kbC++112.5kb2024-11-20 19:38:292024-11-20 23:03:16

answer

#include <bits/stdc++.h>

using namespace std;

const int BUFFER_SIZE = 1 << 20;
char rb[BUFFER_SIZE], *rp = rb, *rt = rb;
inline char read_char() {
    return rp == rt ? (rt = rb + fread(rb, 1, BUFFER_SIZE, stdin), rp = rb, *rp ++) : *rp ++;
}
inline int read_int() {
    int x = 0;
    char ch = read_char(), flag = 0;
    while (ch != '-' && (ch < '0' || ch > '9')) {
        ch = read_char();
    }
    if (ch == '-') {
        flag = 1;
        ch = read_char();
    }
    for (x = 0; ch >= '0' && ch <= '9'; ch = read_char()) {
        x = x * 10 + (ch - '0');
    }
    return flag ? -x : x;
}

char wb[BUFFER_SIZE], *wp = wb;
inline void write_flush() {
    fwrite(wb, 1, wp - wb, stdout);
    wp = wb;
}
inline void write_char(char c) {
    *wp ++ = c;
    if (wp == wb + BUFFER_SIZE) {
        write_flush();
    }
}
inline void write_int(int x) {
    if (x == 0) {
        write_char('0');
    } else if (x < 0) {
        write_char('-');
        x = -x;
    }
    static char b[32], i;
    for (i = 1; x; i ++) {
        b[i] = x % 10 + '0';
        x /= 10;
    }
    for (i --; i; i --) {
        write_char(b[i]);
    }
}

using namespace std;

const int MAX_N = 1000000 + 5;
const int P = 1000000007;

int N, Q;
int a[MAX_N], f[MAX_N], g[MAX_N][3];
int elast[MAX_N], ey[MAX_N << 1], enext[MAX_N << 1];

void dfs(int u, int fa) {
    f[u] = fa;
    for (int j = elast[u], v; j; j = enext[j]) {
        if ((v = ey[j]) != fa) {
            dfs(v, u);
        }
    }
}

int main() {
    N = read_int(), Q = read_int();
    for (int i = 1; i <= N; i ++) {
        a[i] = read_int();
    }
    for (int i = 1, j = 1; i < N; i ++) {
        int x = read_int(), y = read_int();
        ey[j] = y, enext[j] = elast[x], elast[x] = j ++;
        ey[j] = x, enext[j] = elast[y], elast[y] = j ++;
    }
    dfs(1, 0);
    for (int i = 1; i <= N; i ++) {
        for (int j = 0, y = i; j <= 2 && y; j ++, y = f[y]) {
            g[y][j] ^= a[i];
        }
    }
    int ans = 0;
    for (int i = 1, ii = 0; i <= Q; i ++) {
        int x = read_int(), b = read_int();
        for (int j = 0, y = x; j <= 2 && y; j ++, y = f[y]) {
            g[y][j] ^= a[x] ^ b;
        }
        a[x] = b;
        int res = g[x][2] ^ g[x][1];
        x = f[x], res ^= g[x][1] ^ g[x][0];
        x = f[x], res ^= g[x][0];
        ii += i * 2 - 1;
        ii -= ii >= P ? P : 0;
        ans = (ans + 1ll * res * ii) % P;
    }
    write_int(ans), write_char('\n');
    write_flush();
    return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 1224kb

input:

1000 994
780089107 4670366 871010085 73730768 526852049 572589363 885190993 1049357301 450850404 460...

output:

986389582

result:

wrong answer 1st numbers differ - expected: '503748432', found: '986389582'

Test #2:

score: 10
Accepted
time: 0ms
memory: 1220kb

input:

1000 999
26885995 181771373 378079250 245122183 660787775 479226046 575400611 257120086 214404794 48...

output:

301019013

result:

ok 1 number(s): "301019013"

Test #3:

score: 0
Wrong Answer
time: 0ms
memory: 1224kb

input:

1000 994
978408146 31636243 945016735 512129400 92881624 689719983 649699545 964802847 100094796 941...

output:

659045821

result:

wrong answer 1st numbers differ - expected: '250985726', found: '659045821'

Test #4:

score: 0
Wrong Answer
time: 11ms
memory: 6076kb

input:

100000 99994
194640838 777315353 114710204 754050649 372592717 210279787 542056883 638262010 7998293...

output:

781171918

result:

wrong answer 1st numbers differ - expected: '897014293', found: '781171918'

Test #5:

score: 10
Accepted
time: 8ms
memory: 6076kb

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: 16ms
memory: 6076kb

input:

100000 99991
56406146 757409402 876799234 573444553 883023109 326033134 328116703 623771278 41269529...

output:

440189625

result:

ok 1 number(s): "440189625"

Test #7:

score: 0
Wrong Answer
time: 219ms
memory: 41208kb

input:

1000000 999998
814298729 663807029 628693441 45635199 148428014 913833778 1034283337 600802841 58045...

output:

197197519

result:

wrong answer 1st numbers differ - expected: '447637970', found: '197197519'

Test #8:

score: 10
Accepted
time: 207ms
memory: 41212kb

input:

1000000 999993
112899321 425466951 471078924 857561298 449437156 710041336 1033281580 23390934 44420...

output:

449713864

result:

ok 1 number(s): "449713864"

Test #9:

score: 0
Wrong Answer
time: 221ms
memory: 41212kb

input:

1000000 1000000
273043260 1042513348 30746190 864557483 187812119 562837674 263192174 364182254 3537...

output:

871609367

result:

wrong answer 1st numbers differ - expected: '922464355', found: '871609367'

Test #10:

score: 0
Wrong Answer
time: 216ms
memory: 41208kb

input:

1000000 999995
83067658 1064735371 623243759 757522311 539363923 924297206 75317137 543750195 822898...

output:

53420127

result:

wrong answer 1st numbers differ - expected: '318242296', found: '53420127'