H - Country's Value


  • محدودیت زمان: ۵ ثانیه
  • محدودیت حافظه: ۵۱۲ مگابایت

We have a country with nn cities. The cities in this country are connected by n1n - 1 two-way roads. We know that from each city, we can reach any other city by traversing some roads, meaning the structure of this country is a tree.

The cities are numbered from 11 to nn. We know the population of city ii is wiw_i. The \textit{difference} between two cities is defined as the XOR of the populations of the cities along the path between the two cities (including the start and end). The \textit{value} of the country is the sum of the differences between every pair of distinct cities.

Calculating the initial value of the country is not a hard task for the mayor. Therefore, we ask you to design a dynamic system. You will receive qq queries.

In each query, the population of city vv changes to xx. We want you to write a program that calculates the initial value of the country and the value of the country after each change.

ورودی🔗

The first line of input contains an integer nn, representing the number of cities in the country.

1n1051 \leq n\leq 10^5

The second line contains nn integers w1,w2,,wnw_1, w_2, \dots, w_n, representing the initial population of each city.

0wi1080 \leq w_i \leq 10^8

Each of the next n1n - 1 lines contain two integers uu and vv, representing a road between cities uu and vv in the country.

1u,vn1 \leq u, v\leq n

The following line contains an integer qq, representing the number of queries.

1q1051 \leq q \leq 10^5

The next qq lines each contain two integers vv and xx, representing an operation that changes the population wvw_v of city vv to xx. It is guarantied that the roads form a tree.

0x1080 \leq x \leq 10^8

1vn1 \leq v\leq n

خروجی🔗

The output consists of q+1q + 1 lines, where each line contains an integer representing the current value of the country.

مثال‌ها🔗

ورودی نمونه ۱🔗

5
10 11 8 3 17
1 2
1 3
2 4
2 5
3
4 16
1 5
5 5
Plain text

خروجی نمونه ۱🔗

123
157
202
170
Plain text
ارسال پاسخ برای این سؤال
در حال حاضر شما دسترسی ندارید.