-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsparse_table.cpp
More file actions
206 lines (178 loc) · 3.83 KB
/
sparse_table.cpp
File metadata and controls
206 lines (178 loc) · 3.83 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//Classic
//g++ -std=c++17 -O2 -Wall a.cpp -o test
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define vll vector<ll>
#define vi vector<int>
#define vb vector<bool>
#define pi pair<int,int>
#define pll pair<ll,ll>
#define vp vector<pi>
#define vpll vector<pll>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define For(i,a,b) for(ll i=a;i<b;i++)
#define endl "\n"
#define debug2(x,y) cout<<"This side ----> "<<#x<<" -> "<<x<<" | "<<#y<<" -> "<<y<<endl;
#define debug(x) cout<<"This side ----> "<<#x<<" -> "<<x<<endl
//#define ai(a, n) for (int ele = 0; ele < n; ele++) cin >> a[ele];
//#define ao(a, n) {for (int ele = 0; ele < (n); ele++) {cout << a[ele]<<" "; } cout << '\n';}
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(), x.rend()
#define mint map<int,int>
#define mall map<ll,ll>
#define ciN cin
#define g(a,s) get<a>(s)
#define tin tuple<ll,ll,ll>
#define ter(x,y,z) ((x)?y:z)
/////////////
bool isGoogles = 0;
const ll maxn = 1e5 + 10;
const ll max_val = 2e5 + 10;
ll mod = 1000000007;
const ll bits = 19;
ll caseNumber = 1;
////////////////////////////////////////////////////////////////
/*
*/
struct Item {
ll x;
};
struct sparse_table
{
Item NEUTRAL = {1000000009};
vector<vector<Item>> tab;
int siz, bit;
Item merge(Item a, Item b) {
Item c;
c.x = min(a.x, b.x);
return c;
}
Item single(ll a) {
Item b; b.x = a; return b;
}
void init(int n) {
int val = 1;
bit = 0;
while (val <= n) {
bit++; val *= 2;
}
siz = n;
tab.assign(bit, vector<Item>(n, NEUTRAL));
}
void build(vll &v) {
for (int i = 0; i < siz; i++) {
tab[0][i] = single(v[i]);
}
for (int i = 1; i < bit; i++) {
for (int j = 0; j < siz; j++) {
Item f = tab[i - 1][j];
ll done = j + (1LL << (i - 1));
Item s = NEUTRAL;
if (done < siz) {
s = tab[i - 1][done];
}
tab[i][j] = merge(f, s);
}
}
}
Item calc(int l, int r) {
// 0-based indexing.
int len = r - l + 1;
int ind = 0;
for (int i = bit - 1; i > -1; i--) {
if ((len & (1 << i))) {
ind = i; break;
}
}
Item a = tab[ind][l];
ll xx = r - (1LL << ind) + 1;
Item b = tab[ind][xx];
return merge(a, b);
}
};
ll dp[bits][maxn];
void jabru() {
ll n, q; cin >> n >> q;
vll v(n);
ll sum = 0;
For(i, 0, n) {
cin >> v[i];
}
// preprossesing starts
for (int i = 0; i < n; i++) {
dp[0][i] = v[i];
}
for (int i = 1; i < bits; i++)for (int j = 0; j < n; j++)dp[i][j] = 1e18;
for (int i = 1; i < bits; i++) {
for (int j = 0; j < n; j++) {
ll f = dp[i - 1][j];
ll done = j + (1LL << (i - 1));
ll s = LLONG_MAX;
if (done < n) {
s = dp[i - 1][done];
}
dp[i][j] = min(f, s);
}
}
// making for sparse table ends here
// range query starts...
for (int i = 0; i < q; i++) {
ll l, r; cin >> l >> r;
l--; r--;
ll len = r - l + 1;
ll ind = 0;
for (ll j = 18; j > -1; j--) {
if (len & (1LL << j)) {
ind = j; break;
}
}
ll a = dp[ind][l];
// debug(ind);
ll xx = r - (1LL << ind) + 1;
ll b = dp[ind][xx];
cout << min(a, b) << endl;
}
// sum type queries
// long long sum = 0;
// for (int j = K; j >= 0; j--) {
// if ((1 << j) <= R - L + 1) {
// sum += st[L][j];
// L += 1 << j;
// }
// }
}
bool TestCase = 0;
//////////////////////////////////////////////////////////////////
void brain_wash() {
}
/*
*/
//////////////////////////////////////////////////////////
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ll t;
t = 1;
if (TestCase) {
cin >> t;
}
while (t--) {
if (isGoogles) {cout << "Case #" << caseNumber << ": ";} caseNumber++;
jabru();
brain_wash();
}
return 0;
}
/*
*/