so easy(unordered_map+并查集)

与世无争的帅哥 提交于 2019-12-03 15:06:04

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 }

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!