-
Notifications
You must be signed in to change notification settings - Fork 9
/
KQUERY - K-query.cpp
80 lines (79 loc) · 1.63 KB
/
KQUERY - K-query.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
#include<bits/stdc++.h>
using namespace std;
#define mx 30005
int tree[mx*4] = {0};
struct leaf
{
int val;
int ind;
}arr[mx];
bool compl1(leaf ob1, leaf ob2)
{
return ob1.val>ob2.val;
}
struct query
{
int k;
int l;
int r;
int id;
}q[200005];
int ans[200005];
bool compq1(query ob1, query ob2)
{
return ob1.k>ob2.k;
}
void update(int pos, int b, int e, int i, int j)
{
if(b>e || b>j || e<i) return;
if(b>=i && e<=j){
tree[pos] = 1;
return;
}
int l = pos*2+1;
int r = l+1;
int mid = (b+e)/2;
update(l,b,mid,i,j);
update(r, mid+1,e,i,j);
tree[pos] = tree[l]+tree[r];
}
int Query(int pos, int b, int e, int i, int j)
{
if(b>j || e<i || b>e) return 0;
if(b>=i && e<=j) return tree[pos];
int l = pos*2+1;
int r = l+1;
int mid = (b+e)/2;
int r1 = Query(l,b,mid,i,j);
int r2 = Query(r, mid+1,e,i,j);
return r1 + r2;
}
int main()
{
int n,qr,a;
scanf("%d",&n);
for(int i=0; i<n; i++){
scanf("%d",&arr[i].val);
arr[i].ind = i;
}
sort(arr, arr+n,compl1);
int idl = 0;
scanf("%d",&qr);
for(int i=0; i<qr; i++){
scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].k);
q[i].id=i;
q[i].l--;
q[i].r--;
}
sort(q,q+qr,compq1);
for(int i=0; i<qr; i++){
for(idl; idl<n && arr[idl].val> q[i].k; idl++){
update(0,0,n-1,arr[idl].ind,arr[idl].ind);
}
ans[q[i].id] = Query(0,0,n-1,q[i].l, q[i].r);
}
for(int i=0; i<qr; i++){
printf("%d\n",ans[i]);
}
return 0;
}