-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEd_87_D_or_Order_Static_Tree_using_BIT.cpp
More file actions
103 lines (89 loc) · 2.07 KB
/
Ed_87_D_or_Order_Static_Tree_using_BIT.cpp
File metadata and controls
103 lines (89 loc) · 2.07 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Order Static Tree using Fenwick Tree
// https://codeforces.com/contest/1354/problem/D
// https://www.geeksforgeeks.org/order-statistic-tree-using-fenwick-tree-bit/
#include<bits/stdc++.h>
using namespace std;
const int MAXX = 1000010;
int BIT[MAXX] = {0};
/* Updates element at index 'i' of BIT. */
void update(int i, int add) {
while (i > 0 && i < MAXX) {
BIT[i] += add;
i = i + (i & (-i));
}
}
/* Returns cumulative sum of all elements of
fenwick tree/BIT from start upto and
including element at index 'i'. */
int sum(int i)
{
int ans = 0;
while (i > 0)
{
ans += BIT[i];
i = i - (i & (-i));
}
return ans;
}
// Returns lower bound for k in BIT.
int findKthSmallest(int k) {
// Do binary search in BIT[] for given
// value k.
int l = 0;
int h = MAXX;
while (l < h) {
int mid = (l + h) / 2;
if (k <= sum(mid))
h = mid;
else
l = mid+1;
}
return l;
}
// Insert x into BIT. We basically increment
// rank of all elements greater than x.
void insertElement(int x) {
update(x, 1);
}
// Delete x from BIT. We basically decreases
// rank of all elements greater than x.
void deleteElement(int x) {
update(x, -1);
}
// Returns rank of element. We basically
// return sum of elements from start to
// index x.
int findRank(int x) {
return sum(x);
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
int n , q;
cin >> n >> q;
int val;
for(int i=0;i<n;i++){
cin >> val;
insertElement(val);
}
while(q--){
int k;
cin >> k;
if(k<0){
int vall = findKthSmallest(abs(k));
deleteElement(vall);
}else{
if(k>=1 && k<=n)
insertElement(k);
}
}
if(sum(n)>0)
cout << findKthSmallest(1) << endl;
else
cout << 0 << endl;
}