luogu#P5385. [Cnoi2019] 须臾幻境

    ID: 3856 远端评测题 2000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2019O2优化动态树 LCT可持久化线段树

[Cnoi2019] 须臾幻境

Background

There used to be a sad and touching story here, but the problem setter deleted the file and it was lost.

Problem Description

Initially, you are given an undirected graph GG with nn vertices and mm edges. The vertices are numbered 1,2,,n1,2,\cdots,n.

Now, number all mm edges in GG in order and arrange them into an edge sequence of length mm: E=(e1,e2,,em)E=(e_1,e_2,\cdots,e_m), where ei=(ui,vi)e_i=(u_i,v_i) is an ordered pair representing an undirected edge connecting uiu_i and viv_i.

Then Cirno will give you qq query pairs (l,r)(l,r), asking: “If we only keep the edges el,el+1,,ere_l,e_{l+1},\cdots,e_r in this interval, what is the number of connected components in the graph?”

Time is tight. You need to design an algorithm as fast as possible to answer Cirno’s queries. Also, since in some cases queries may depend on each other, your program must run online.

Input Format

The first line contains four integers separated by spaces: nn, mm, qq, tt, where tt is the forced-online parameter used to decrypt the real queries.

The next mm lines describe the edges. The ii-th line contains two integers uiu_i and viv_i separated by a space, representing the edge sequence EE.

The next qq lines each contain two integers l,rl', r' separated by spaces, representing an encrypted query.

The real query pair (l,r)(l,r) is decrypted as follows.

DecodeQuery( l', r', m, t, last_ans )
	(l, r) <- (l', r')
    IF t > 0 THEN 
        l <- (l + t * last_ans) Mod |E| + 1
        r <- (r + t * last_ans) Mod |E| + 1
    IF l' > r' THEN 
        Swap(l, r)
    RETURN (l, r)

Here, last_ans is the answer to the previous query. Initially, last_ans = 0.

Output Format

Output qq lines, each being the answer to one query.

5 5 4 0
1 2
3 4
2 3
4 5
1 5
1 3
2 5
3 4
5 5
2
1
3
4
见附件中 sample2.in
见附件中 sample2.out

Hint

Sample Explanation

Constraints and Notes

For 100%100\% of the testdata, it is guaranteed that 1V1051\le |V| \le 10^5, 1E2×1051\le |E| \le 2\times 10^5, 1q1051\le q \le 10^5, and t{0,1}t \in \{0,1\}. Note that the testdata may contain multiple edges and self-loops.

Subtasks

Subtask 1 (1515 points): V,E,q5000|V|, |E|, q \le 5000.

Subtask 2 (2525 points): t=0t = 0.

Subtask 3 (2020 points): V104|V| \le 10^4, E,q3×104|E|, q \le 3\times 10^4.

Subtask 4 (4040 points): No special restrictions.

Translated by ChatGPT 5