题目收集
题目列表题单
返回列表

Turing Grandma

CodeforcesP0031· ZJU Summer 2026 Contest 3原题链接
数学

创建于 2026/07/16 · 更新于 2026/07/16

题目描述

Turing Grandma likes knitting wool sweaters. The iii-th sweater she knits is represented by a weighted tree DiD_iDi​. And you are given the pattern weighted tree TTT with nnn vertices numbered from 111 to nnn.

For the current weights of TTT, the sequence of sweaters D0,D1,D2,…D_0, D_1, D_2, \ldotsD0​,D1​,D2​,… is defined as follows.

The initial sweater D0D_0D0​ consists of a single vertex.

For k≥1k \ge 1k≥1, Grandma uses several smaller sweaters of type Dk−1D_{k-1}Dk−1​ to knit a larger sweater DkD_kDk​ as follows:

  1. She takes nnn pairwise disjoint smaller sweaters, each being a copy of Dk−1D_{k-1}Dk−1​, and numbers them from 111 to nnn.
  2. In the iii-th smaller sweater, she chooses the vertex corresponding to the iii-th key vertex of Dk−1D_{k-1}Dk−1​.
  3. For every edge connecting aaa and bbb with current weight www in TTT, she connects the chosen vertex in the aaa-th smaller sweater and the chosen vertex in the bbb-th smaller sweater by an edge of weight www.
  4. In the larger sweater DkD_kDk​, the chosen vertex in the iii-th smaller sweater becomes the iii-th key vertex of DkD_kDk​.

For every 1≤i≤n1 \le i \le n1≤i≤n, the iii-th key vertex of D0D_0D0​ is defined to be this single vertex.

For a weighted tree GGG, let dist⁡G(u,v)\operatorname{dist}_G(u, v)distG​(u,v) be the sum of edge weights on the simple path between vertices uuu and vvv.

For every positive integer kkk, define the complexity of the sweater DkD_kDk​ as

comp⁡(Dk)=∑{u,v}⊆V(Dk)dist⁡Dk(u,v).\operatorname{comp}(D_k)=\sum_{\{u,v\}\subseteq V(D_k)} \operatorname{dist}_{D_k}(u,v).comp(Dk​)={u,v}⊆V(Dk​)∑​distDk​​(u,v).

There are qqq queries. Before each query, the generator changes the weight of exactly one edge in TTT. The query is then answered using the current edge weights after this change.

Let AiA_iAi​ be the answer to the iii-th query. Since the output may be large, you only need to print

(A1⋅1)xor⁡(A2⋅2)xor⁡⋯xor⁡(Aq⋅q).(A_1 \cdot 1)\operatorname{xor}(A_2 \cdot 2)\operatorname{xor}\cdots\operatorname{xor}(A_q \cdot q).(A1​⋅1)xor(A2​⋅2)xor⋯xor(Aq​⋅q).

Here xor⁡\operatorname{xor}xor denotes bitwise xor. The multiplication by the query index is ordinary integer multiplication and is not taken modulo 998244353998244353998244353.

Please pay attention to the input format.

输入格式

The first line contains four integers nnn, qqq, KKK, and seed\mathit{seed}seed, where nnn (2≤n≤1062 \le n \le 10^62≤n≤106) is the number of vertices, qqq (1≤q≤4⋅1071 \le q \le 4 \cdot 10^71≤q≤4⋅107) is the number of queries, KKK (1≤K≤10181 \le K \le 10^{18}1≤K≤1018) is the upper bound on query values, and seed\mathit{seed}seed (0≤seed≤2610 \le \mathit{seed} \le 2^{61}0≤seed≤261) is the initial seed of the generator.

Each of the next n−1n - 1n−1 lines contains two integers pvp_vpv​ and wvw_vwv​ for v=2,3,…,nv = 2, 3, \ldots, nv=2,3,…,n, meaning that there is an edge between vvv and pvp_vpv​ with initial weight wvw_vwv​, where 1≤pv<v1 \le p_v \lt v1≤pv​<v and 1≤wv≤1001 \le w_v \le 1001≤wv​≤100.

There is no further input. Edge updates and query values are generated by the following code. The loop is executed for i=1,2,…,qi = 1, 2, \ldots, qi=1,2,…,q.

  
unsigned long long x = 123456789, y = 362436069, seed;  
  
unsigned long long gen() {  
    unsigned long long t;  
    x ^= x « 16;  
    x ^= x » 5;  
    x ^= x « 1;  
    t = x;  
    x = y;  
    y = seed;  
    seed = t ^ x ^ y;  
    return seed;  
}  
  
for (int i = 1; i <= q; ++i) {  
    int v = gen() % (n - 1) + 2;
    int w = gen() % 100 + 1;
    // Change the weight of the edge between v and p\_v to w.  
    long long k_i = gen() % K + 1;
    // Answer the i-th query with value k_i.  
}  

输出格式

Print (A1⋅1)xor⁡(A2⋅2)xor⁡⋯xor⁡(Aq⋅q).(A_1 \cdot 1)\operatorname{xor}(A_2 \cdot 2)\operatorname{xor}\cdots\operatorname{xor}(A_q \cdot q).(A1​⋅1)xor(A2​⋅2)xor⋯xor(Aq​⋅q).

样例输入 #1

2 3 2 1
1 1

样例输出 #1

842