so easy(unordered_map+并查集)

There are nn points in an array with index from 11 to nn, and there are two operations to those points.

1: 1 \ x1 x marking the point xx is not available

2: 2 \ x2 x query for the index of the first available point after that point (including xx itself) .

 

样例输入

5 3
1 2
2 2
2 1

样例输出

3
1
 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 unordered_map<int, int> fa;
 6 
 7 int findfa(int x)
 8 {
 9     if (!fa.count(x)) return x;
10     return fa[x] = findfa(fa[x]);
11 }
12 
13 int main()
14 {
15     int n, q;
16     int op, x;
17     scanf("%d %d", &n, &q);
18     while (q--)
19     {
20         scanf("%d %d", &op, &x);
21         if(op == 1) fa[x] = findfa(x + 1);
22         else printf("%d\n", findfa(x));
23     }
24     return 0;
25 }

 

 

 

 

posted @ 2019-11-05 20:44  Xxiaoyu  阅读(155)  评论(0)    收藏  举报