forked from beet-aizu/library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaveletmatrix.cpp
156 lines (138 loc) · 3.16 KB
/
waveletmatrix.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
//BEGIN CUT HERE
struct FullyIndexableDictionary{
int len,blk;
vector<int> bit,sum;
FullyIndexableDictionary(){}
FullyIndexableDictionary(int len)
:len(len),blk((len+31)>>5),bit(blk,0),sum(blk,0){}
void set(int k){
bit[k>>5]|=1<<(k&31);
}
void build(){
sum[0]=0;
for(int i=1;i<blk;i++)
sum[i]=sum[i-1]+__builtin_popcount(bit[i-1]);
}
bool operator[](int k) const{
return bool((bit[k>>5]>>(k&31))&1);
}
int rank(int k){
return sum[k>>5]+__builtin_popcount(bit[k>>5]&((1<<(k&31))-1));
}
int rank(bool v,int k){
return (v?rank(k):k-rank(k));
}
int select(bool v,int k){
if(k<0||rank(v,len)<=k) return -1;
int low=0,high=len;
while(low+1<high){
int mid=(low+high)>>1;
if(rank(v,mid)>=k+1) high=mid;
else low=mid;
}
return high-1;
}
int select(bool v,int i,int l){
return select(v,i+rank(v,l));
}
};
template<class T,int MAXLOG>
struct WaveletMatrix{
int len;
FullyIndexableDictionary mat[MAXLOG];
int zs[MAXLOG],buff1[MAXLOG],buff2[MAXLOG];
WaveletMatrix(vector<T> data){
len=data.size();
vector<T> l(len),r(len);
for(int dep=0;dep<MAXLOG;dep++){
mat[dep]=FullyIndexableDictionary(len+1);
int p=0,q=0;
for(int i=0;i<len;i++){
bool k=(data[i]>>(MAXLOG-(dep+1)))&1;
if(k) r[q++]=data[i],mat[dep].set(i);
else l[p++]=data[i];
}
zs[dep]=p;
mat[dep].build();
swap(l,data);
for(int i=0;i<q;i++) data[p+i]=r[i];
}
}
T access(int k){
T res=0;
bool bit;
for(int dep=0;dep<MAXLOG;dep++){
bit=mat[dep][k];
res=(res<<1)|bit;
k=mat[dep].rank(bit,k)+zs[dep]*dep;
}
return res;
}
// return the number of v in [0,k)
int rank(T v,int k){
int l=0,r=k;
for(int dep=0;dep<MAXLOG;dep++){
buff1[dep]=l;buff2[dep]=r;
bool bit=(v>>(MAXLOG-(dep+1)))&1;
l=mat[dep].rank(bit,l)+zs[dep]*bit;
r=mat[dep].rank(bit,r)+zs[dep]*bit;
}
return r-l;
}
// return the position of k-th v
int select(T v,int k){
rank(v,len);
for(int dep=MAXLOG-1;dep>=0;dep--){
bool bit=(v>>(MAXLOG-(dep+1)))&1;
k=mat[dep].select(bit,k,buff1[dep]);
if(k>=buff2[dep]||k<0) return -1;
k-=buff1[dep];
}
return k;
}
int select(T v,int k,int l){
return select(v,k+rank(v,l));
}
// return k-th largest value in [l,r)
T quantile(int l,int r,int k){
if(r-l<=k||k<0) return -1;
T res=0;
for(int dep=0;dep<MAXLOG;dep++){
int p=mat[dep].rank(1,l);
int q=mat[dep].rank(1,r);
if(q-p>k){
l=p+zs[dep];
r=q+zs[dep];
res|=(1<<(MAXLOG-(dep+1)));
}else{
k-=(q-p);
l-=p;
r-=q;
}
}
return res;
}
};
//END CUT HERE
signed main(){
int n,q;
while(scanf("%d %d",&n,&q)!=EOF){
vector<int> v(n);
for(int i=0;i<n;i++) scanf("%d",&v[i]);
WaveletMatrix<int,32> wm(v);
for(int i=0;i<q;i++){
int l,r,k;
scanf("%d %d %d",&l,&r,&k);
l--;
printf("%d\n",wm.quantile(l,r,(r-l)-k));
}
}
return 0;
}
/*
verified on 2017/11/06
http://rhodon.u-aizu.ac.jp:8080/arena/room.jsp?id=3765
*/