-
Notifications
You must be signed in to change notification settings - Fork 0
/
KQUERY.cpp
89 lines (67 loc) · 1.41 KB
/
KQUERY.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
// Problem : KQUERY from SPOJ
#include <bits/stdc++.h>
using namespace std;
struct node{
node *left,*right;
int count;
node(int c,node* l,node* r):count(c),left(l),right(r){}
node(){}
node* insert(int l,int r,int idx);
};
node* null = new node;
node* node::insert(int l,int r,int idx){
if(l<=idx && idx<=r){
if(l==r)return new node((this->count)+1, null, null);
int m = (l+r)/2;
return new node((this->count)+1,this->left->insert(l,m,idx),this->right->insert(m+1,r,idx));
}
else{
return this;
}
}
int query(node* Old, node* New, int l, int r, int val){
if(r<val || l>r)return 0;
int m = (l+r)/2;
int lCount = New->count - Old->count;
//cout<<"l:"<<l<<",r:"<<r<<"\n";
if(l >= val){
return lCount;
}
else{
if(l<r){
return query(Old->left, New->left, l, m, val) + query(Old->right, New->right, m+1, r,
val);
}
else{
return 0;
}
}
}
int main(){
int n,m,l,r,i,k,idx;
cin>>n;
int a[n],b[n];
map<int,int> ma;
for(i=0;i<n;i++){
cin>>a[i];
b[i] = a[i];
}
cin>>m;
sort(b,b+n);
for(i=0;i<n;i++)ma[b[i]] = i;
for(i=0;i<n;i++)a[i] = ma[a[i]];
null->left = null->right = null;
null->count = 0;
node* root[n];
for(i=0;i<n;i++){
root[i] = ((i==0)?null:root[i-1])->insert(0,n-1,a[i]);
}
while(m--){
cin>>l>>r>>k;
l--,r--;
idx = upper_bound(b,b+n,k)-b;
//cout<<"idx:"<<idx<<"\n";
cout<<query(((l==0)?null:root[l-1]),root[r],0,n-1,idx)<<"\n";
}
return 0;
}