so easy
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_1z1x1
\vdots⋮
z_q\quad x_qzqxq
qq is the number of queries, zz is the type of operations, and xx is the index of operations. 1≤x<n<10^91≤x<n<109, 1 \leq q<10^61≤q<106 and zz is 11 or 22
Output
Output the answer for each query.
样例输入
5 3 1 2 2 2 2 1
样例输出
3 1
题意
给定一个 1~n 的序列,以及 q 次操作,每个操作有两种选项:①使数字 x 无效;②查询 x (含)后第一个有效的数字。
思路
很自然的可以想到用并查集来模仿链表的操作,但是由于 n 的范围太大(1e9),所以用并查集是不行的。但是,我们可以采用 map,来标记 x 的后继,并模仿并查集的寻根操作进行查找后继以及路径压缩。然后,注意 cin 关同步仍然会 TLE。
代码
#include <bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1e9;
int n, q, z, x;
map<int, int> m;
void read()
{
scanf("%d%d", &n, &q);
}
int find(int r)
{
if(!m.count(r))
return r;
return m[r] = find(m[r]); //路径压缩
}
void solve()
{
int ans = 0;
for(int i = 0; i < q; ++i)
{
scanf("%d%d", &z, &x);
if(z == 1)
m[x] = find(x+1);
else if((ans = find(x)) <= n)
printf("%d\n", ans);
}
}
int main()
{
read();
solve();
return 0;
}