-
Notifications
You must be signed in to change notification settings - Fork 0
/
2243.cpp
61 lines (52 loc) · 1.22 KB
/
2243.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#define N 1000001
using namespace std;
int n, a, b, c;
int t[N * 4];
void update(int left, int right, int index, int target, int diff)
{
if (left > target || right < target)
return;
t[index] += diff;
if (left == right)
return;
int mid = (left + right) / 2;
update(left, mid, index * 2, target, diff);
update(mid + 1, right, index * 2 + 1, target, diff);
}
int query(int left, int right, int index, int target)
{
if (left == right)
return left;
int mid = (left + right) / 2;
if (t[index * 2] < target)
return query(mid + 1, right, index * 2 + 1, target - t[index * 2]);
return query(left, mid, index * 2, target);
}
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int i;
cin >> n;
for (i = 0; i < n; i++)
{
cin >> a;
if (a == 1)
{
cin >> b;
int tidx = query(1, N, 1, b);
printf("%d\n", tidx);
update(1, N, 1, tidx, -1);
}
else
{
cin >> b >> c;
update(1, N, 1, b, c);
}
}
return 0;
}