学习set,其他容器每次都要去sort(),除了堆(但其无法lower_bound)
题面
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones.
Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately!
Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately!
Input
The first line of the input contains two positive integers n and m (n, m ≤ 50,000) indicating the number of villages and events. Each of the next m lines describes an event.
There are three different events described in different format shown below:
D x: The x-th village was destroyed.
Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
R: The village destroyed last was rebuilt.
OutputOutput the answer to each of the Army commanders’ request in order on a separate line.
#include <bits/stdc++.h> using namespace std; int main() { int n, m, x, c; while (scanf("%d%d", &n, &m) != EOF) { stack<int> sk; set<int> st; st.insert(0), st.insert(n + 1); while (m--) { getchar(); scanf("%c", &c); if (c == 'R') st.erase(sk.top()), sk.pop(); else { scanf("%d", &x); if (c == 'D') st.insert(x), sk.push(x); else if (st.find(x) != st.end()) printf("0\n"); else { auto l = st.lower_bound(x), r = st.lower_bound(x); printf("%d\n", *r - *--l - 1); } } } } return 0; }