码迷,mamicode.com
首页 > 其他好文 > 详细

so easy(并查集+unordered_map)

时间:2019-09-09 17:53:50      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:sub   str   else   number   c++   ==   success   operation   style   

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) .

 

Input

n\quad qnq

z_1 \quad x_1z1?x1?

\vdots?

z_q\quad x_qzq?xq?

qq is the number of queries, zz is the type of operations, and xx is the index of operations. 1≤x<n<10^91x<n<109, 1 \leq q<10^61q<106 and zz is 11 or 22

Output

Output the answer for each query.

样例输入

5 3
1 2
2 2
2 1

样例输出

3
1




#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e5 + 100;
unordered_map<int, int> fa;

int findfa(int x) {
    if (!fa.count(x)) return x;
    return fa[x] = findfa(fa[x]);
}


int main() {
    int n, q;
    scanf("%d %d", &n, &q);
    int op, x;
    while (q--) {
        scanf("%d %d", &op, &x);
        if (op == 1) {
            fa[x] = findfa(x + 1);
        } else {
            int ans = findfa(x);
            if (ans > n) ans = -1;
            printf("%d\n", ans);
        }
    }
    return 0;
}

 




so easy(并查集+unordered_map)

标签:sub   str   else   number   c++   ==   success   operation   style   

原文地址:https://www.cnblogs.com/wjc2021/p/11493050.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!