-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfenwicktree.cpp
More file actions
69 lines (57 loc) · 1.3 KB
/
Copy pathfenwicktree.cpp
File metadata and controls
69 lines (57 loc) · 1.3 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
#include<bits/stdc++.h>
# define ll long long
using namespace std;
int sum(vector<int>&bit,int i){
int s=0;
while(i>0){
s+=bit[i];
i-=i&(-i);
}
return s;
}
void update(vector<int>&bit,int i,int n,int v){
while(i<=n){
bit[i]+=v;
i+=i&(-i);
}
}
int solve(int n, vector<int> arr) {
int BIT_SIZE=100005;
vector<int>bit(BIT_SIZE,0);
vector<int>small(n+1,0);
vector<int>big(n+1,0);
unordered_set<int>s;
for(int i=0;i<n;i++){
small[i]=sum(bit,arr[i]-1);
if(s.find(arr[i])==s.end()){ //no of elemnts less on left side
update(bit,arr[i],BIT_SIZE,1);
s.insert(arr[i]);
}
}
s.clear();
for(int i=0;i<=BIT_SIZE;i++) bit[i]=0;
for(int i=n-1;i>=0;i--){ // no of elements greater on right side
big[i]= sum(bit,BIT_SIZE)-sum(bit,arr[i]);
if(s.find(arr[i])==s.end()){
update(bit,arr[i],BIT_SIZE,1);
s.insert(arr[i]);
}
}
int ans=0;
for(int i=0;i<n;i++){
ans=max({ans,big[i],small[i]});
}
return ans;
}
int main() {
int N;
cin >> N;
vector<int> arr(N);
for(int i_arr = 0; i_arr < N; i_arr++)
{
cin >> arr[i_arr];
}
int out_;
out_ = solve(N, arr);
cout << out_;
}