#P10660. BZOJ2759 一个动态树好题

    ID: 12159 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>O2优化动态树 LCT扩展欧几里德算法

BZOJ2759 一个动态树好题

Problem Description

There are nn unknowns x1,x2,,xnx_1,x_2,\dots,x_n and a system of nn congruence equations: xiki×xpi+bi(mod10007)x_i\equiv k_i\times x_{p_i} + b_i \pmod {10007}.

You need to perform qq operations. Each operation is one of the following two types:

  • A a: Query the current solution of xax_a. If there is no solution, output -1. If there are multiple solutions, output -2. Otherwise, output xax_a.
  • C a x y z: Modify one equation: set kax,pay,bazk_a \gets x,p_a \gets y,b_a \gets z.

Input Format

The first line contains an integer nn.

The next nn lines each contain three integers ki,pi,bik_i,p_i,b_i.

The next line contains an integer qq.

The following qq lines each contain one operation, as described above.

Output Format

For each query, output one integer per line.

5
2 2 1
2 3 2
2 4 3
2 5 4
2 3 5
5
A 1
A 2
C 5 3 1 1
A 4
A 5
4276
7141
4256
2126

Hint

For all testdata, ki,bi,xi[0,10007)Zk_i,b_i,x_i \in [0,10007) \cap \Z. 1n3×1041\leq n\leq 3\times 10^4, 0q1050\leq q\leq 10^5, and query operations make up about 80%80\% of all operations.

Translated by ChatGPT 5