#C7707. Mystic Island System Operations
Mystic Island System Operations
Mystic Island System Operations
You are given a system of M mystical islands connected by portals. Each portal connects two islands and has an associated power level. Initially, all portals have a power level of 0
.
You are to perform two types of operations on this system:
- Type 1 Operation:
1 p q k
– Strengthen the portal connecting islands p and q by adding a power of k. Note that since the connection is undirected, the portal is uniquely defined by the unordered pair \((p,q)\). Formally, if \(p > q\), swap them so that the portal is always stored as \((\min(p,q), \max(p,q))\). - Type 2 Operation:
2 r
– Query the total power connected to island r. The total power is defined as: \[ P(r)=\sum_{x \in N(r)} w\big(\min(r,x), \max(r,x)\big) \] where \(N(r)\) is the set of islands directly connected to island r and \(w(i,j)\) is the current power level of the portal between islands \(i\) and \(j\).
You need to simulate the operations and print the answer for every type 2 operation.
inputFormat
The input is read from stdin and is formatted as follows:
- An integer
M
representing the number of islands. - An integer
P
representing the number of portals. P
lines follow, each containing two integersa b
indicating that there is a portal between islandsa
andb
.- An integer
Q
representing the number of operations. Q
lines follow, each representing an operation. A line starting with1
is followed by three integersp q k
(strengthen operation), and a line starting with2
is followed by a single integerr
(query operation).
outputFormat
For each type 2 operation, output a line to stdout containing the total power for the specified island.
## sample6
5
1 2
2 3
3 4
4 5
5 6
5
1 2 3 10
2 3
1 4 3 5
2 4
2 6
10
5
0
</p>