Ants
Time limit3sMemory limit128 MB
Given a forest of towns formed by persistent range-add copies of parent towns, answer range-sum queries on each newly created version using online, XOR-derived parameters that depend on previous answers.
- Level
Hard8 of 10
- Topics
- Segment tree, Prefix sum, Tree, Binary search
- Solved
- No attempts yet
Problem
Ant-land keeps expanding, and new towns are founded like this: when a town becomes overcrowded, some of its residents leave and found a new town (some ants always stay behind in the old town). At the very start, Ant-land has just one town.
By ancient tradition, every town must contain exactly anthills, numbered through , and each anthill holds a known number of ants. When a new town is founded, all of its anthills are built at once. Out of respect for tradition, the founders model the new town on the town they left: anthill in the new town starts with the same capacity as anthill in the old town, for every .
Some innovator ants, however, tweak the plan. At the moment a new town is founded, the chieftains order that the capacities of anthills through (inclusive) each be increased by the same amount .
Once the new town's anthills are built, its prestigious quarter turns out to consist of all anthills numbered through (inclusive), and the chieftains ask: how many ants can the prestigious quarter hold in total?
Write a program that answers this question every time a new town is founded.
Input
The first line contains two integers and : is the total number of towns in Ant-land after every new town has been founded, and is the number of anthills in each town.
The second line contains integers , where is the capacity of anthill in the first town.
Each of the next lines describes the founding of one new town with six integers :
- is the index of the town the new town is founded from. The first town has index . Each newly founded town receives the smallest positive integer not yet used as a town index (so towns are indexed in the order they are founded).
- The values are derived from a running value :
starts at . After a new town is founded, is set to that town's answer (the total capacity of its prestigious quarter, anthills through ), and this updated is used when deriving the parameters of the next founding. It is guaranteed that and for every town.
Output
For each newly founded town, print a single integer on its own line: the total number of ants its prestigious quarter can hold.
Constraints
- for every , and for every founding
- for every newly founded town
- for every newly founded town
Note
Worked example (this matches the first sample). Town has capacities .
- Town 2, founded from town : , so . Adding to anthills – gives . The prestigious quarter (anthills –) holds , so the answer is and becomes .
- Town 3, founded from town : with , . Starting from town 's and adding to anthills – gives . The prestigious quarter (anthill ) holds , so the answer is and becomes .
- Town 4, founded from town : with , . Starting from town 's and adding to anthills – gives . The prestigious quarter (anthills –) holds , so the answer is .
Each new town is an independent copy of the town it was founded from, so a founding never changes the capacities of any earlier town.