#P3765. 总统选举

    ID: 2708 远端评测题 1000~5000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>线段树平衡树洛谷原创O2优化枚举,暴力众数构造洛谷月赛

总统选举

Description

There are nn people in the State of Qiu, numbered 1,2,,n1, 2, \dots, n. Initially, each person casts one vote in the range 1n1 \sim n, indicating support for the person with that ID to become president.

There are mm primaries. In each primary, voters with indices [li,ri][l_i, r_i] are selected for a small-scale primary. Within that interval, the person who gets more than half of the votes in the interval wins. If no one wins, Xiao C designates a candidate with ID sis_i as the winner (the winner may be outside the interval). The result of each primary must be announced, and after each primary, kik_i people decide to switch their votes to the winner of that primary.

After all primaries end, announce the candidate who finally becomes president.

Input Format

The first line contains two integers n,mn, m, the population of Qiu and the number of primaries.

The second line contains nn integers, representing the votes cast by voters numbered 1n1 \sim n.

Then follow mm lines. Each line first contains four integers li,ri,si,kil_i, r_i, s_i, k_i, where sis_i means that if no one wins this primary, the person with ID sis_i is considered the winner. Then follow kik_i integers, representing the voters who decide to switch their votes.

Output Format

Output m+1m+1 lines in total. The first mm lines represent the result of each primary. The last line represents the candidate who finally becomes president. If in the end no one still meets the winning condition, output 1-1.

5 4
1 2 3 4 5
1 2 1 1 3
5 5 1 2 2 4
2 4 2 0
3 4 2 1 4
1
5
5
2
-1

Hint

For the first 20%20 \% of the testdata, 1n,m50001 \leq n, m \leq 5000.

For the first 40%40 \% of the testdata, 1n,m500001 \leq n, m \leq 50000, ki50000\sum k_i \leq 50000.

For the first 50%50 \% of the testdata, 1n,m1051 \leq n, m \leq {10}^5, ki2×105\sum k_i \leq 2 \times {10}^5.

For data points 6~7, it is guaranteed that all votes always remain in 1101 \sim 10.

For 100%100 \% of the testdata, 1n,m5×1051 \leq n, m \leq 5 \times {10}^5, ki106\sum k_i \leq 10^6, 1lirin1 \leq l_i \leq r_i \leq n, 1sin1 \leq s_i \leq n.

Translated by ChatGPT 5