Home BOJ. Candy Box (2243)
Post
Cancel

BOJ. Candy Box (2243)

[Link] https://www.acmicpc.net/problem/2243


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
62
63
64
65
66
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cstring>
// #include <bits/stdc++.h>
using namespace std;
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i < n; i++)
#define fori(s, e) for(int i = s; i < e; i++)
#define endl "\n"
#define nulls '\0'
#define dkdk " "

const int MAX = 1e6 + 1;
const int log = 20;
const NUM = 1e6;
int n;

int seg[1 << log+1];

void update(int v, int amount, int idx, int l, int r) {
  if(r < v || v < l) return;
  int mid = l+r>>1;
  seg[idx]+=amount;
  if(l == r) return;
  update(v, amount, idx<<1, l, mid);
  update(v, amount, idx<<1|1, mid + 1, r);
}


int pull(int rank) {
  int seg_idx = 1, l = 1, r = NUM;
  while(l != r) {
    int left_cnt = seg[seg_idx<<1];
    if(left_cnt >= rank) {
      seg_idx<<=1;
      r = l+r>>1;
    } else {
      rank -= left_cnt;
      seg_idx = seg_idx<<1|1;
      int mid = l+r>>1;
      l = mid + 1;
    }
  }
  update(l, -1, 1, 1, NUM);
  return l;
}

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  // freopen("input.txt", "r", stdin);
  cin >> n;
  while(n--) {
    int a, b; cin >> a >> b;
    if(a == 1) {
      cout << pull(b) << endl;
    } else {
      int c; cin >> c;
      update(b, c, 1, 1, NUM);
    }
  }
  return 0;
}
This post is licensed under CC BY 4.0 by the author.