-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathD. Multiset.cpp
121 lines (87 loc) · 2.58 KB
/
D. Multiset.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//Link:https://codeforces.com/problemset/problem/1354/D
//we have used segment tree here and some other tricks and here you go
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int nmax = 1e6+10;
int tree[4*nmax];
int arr[nmax];
void build(int id, int l, int r){
if(l == r){
tree[id] = arr[l];
return;
}
int mid = (l+r)/2;
build(2*id, l, mid);
build(2*id+1, mid+1, r);
tree[id] = tree[2*id] + tree[2*id+1]; /// merging childs
}
void update(int id, int l, int r, int k, int val){
if(l == r){
tree[id] += val; /// update operation
return;
}
int mid = (l+r)/2;
if(k <= mid)
update(2*id, l, mid, k, val);
else
update(2*id+1, mid+1, r, k, val);
tree[id] = tree[2*id] + tree[2*id+1]; /// merging child
return;
}
int query(int id, int l, int r, int k){
//Basic:
//let alone we have 1,1,3,4,5,6
//making a tree reversely where we will show count of 1 to 6
// (5)
// (3) (2)
//(2) (1) (1) (1)
//(2) (0) (1) (0)
// 1 2 3 4 5 6
// here we have 1 for 2 times,2 for 0 times.......so, if we want to visit 4th index value ,
//we will start from root =5 then see that left child=3 is less that 4 and thus we will go to right child and (2) and then left child (1) and finally (1) which shows 4 as the 4th index
//base case
if(l == r)
return l; //returning the leaf
int mid = (l+r)/2;
if(tree[2*id] >= k) //left child [2*id] //going to left chinld
return query(2*id, l, mid, k);
else{//going to right child
return query(2*id+1, mid+1, r, k-tree[2*id]);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, q;
cin>>n>>q;
for(int i = 0; i<n; i++){
int x;
cin>>x;//taking input
arr[x]++; //inputs count in a array
}
build(1, 1, n);//building the whole values
int sz = n;//size variabl
for(int i = 0; i<q; i++){
int k;
cin>>k;
if(k > 0){//k can be positive
update(1, 1, n, k, +1);
sz++;
}
else{// negative k
k = -k;
int x = query(1, 1, n, k);
update(1, 1, n, x, -1);//removing 1 content in x index
sz--;
}
}
if(sz == 0){
cout<<0<<"\n";
}
else{
cout<<query(1, 1, n, 1);//1st element
}
return 0;
}