/* Author : Mychecksdead */
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define PI 3.1415926535
#define pb push_back
#define setp() cout << setprecision(15)
#define all(x) x.begin(), x.end()
#define oset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define debug(x) cerr << #x << " is " << x << '\n';
const int N = 1e6+100, M = 1e5+10, F = 2147483646, K = 20;
int n, m, a[N], dp[N][K];
vector<int> t[4*N];
void build(int l, int r, int k){
if(l == r){
t[k] = {a[l]};
return;
}
int m = (l+r)>>1;
build(l, m, k<<1);
build(m+1, r, k<<1|1);
merge(all(t[k<<1]), all(t[k<<1|1]), back_inserter(t[k]));
}
int find(int l, int r){
int k = int(log2(r-l+1));
return max(dp[l][k], dp[r - (1<<k) + 1][k]);
}
pair<int, int> larger(int l, int r, int ql, int qr, int k, int val){
if(l > qr || r < ql) return {0, 0};
if(ql <= l && r <= qr){
return {(upper_bound(all(t[k]), val) - t[k].begin()), (lower_bound(all(t[k]), val) - t[k].begin())};
}
int m = (l + r) >> 1;
auto v = larger(l, m, ql, qr, k<<1, val);
auto p = larger(m+1, r, ql, qr, k<<1|1, val);
return {v.first + p.first, v.second + p.second};
}
int q(int l, int r, int ql, int qr, int k, int val){
if(l > qr || r < ql) return 0;
if(ql <= l && r <= qr){
int idx = lower_bound(all(t[k]), val) - t[k].begin();
return (idx == 0 ? 0 : a[idx - 1]);
}
int m = (l + r) >> 1;
return max(q(l, m, ql, qr, k<<1, val), q(m + 1, r, ql, qr, k<<1|1, val));
}
int query(int l, int r, int k){
if(l == r){
return 0;
}
int lb = 1, rb = MOD, med = MOD;
while(lb <= rb){
int m = (lb + rb) >> 1;
auto v = larger(1, n, l, r, 1, m);
if(v.first < (r-l+2)/2){
lb = m + 1;
}else if(v.second > (r-l+2)/2){
rb = m - 1;
}else{
med = m;
break;
}
}
int m = (l+r)>>1;
int mx = find(l, m);
if(mx <= med) mx = 0;
return max({q(1, n, m + 1, r, 1, med) + mx, query(l, m, k), query(m + 1, r, k)});
}
void solve(){
cin >> n >> m;
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 1; i <= n; ++i) dp[i][0] = a[i];
for(int j = 1; j < K; ++j){
for(int i = 1; i + (1<<j) <= n + 1; ++i){
dp[i][j] = max(dp[i][j - 1], dp[i + (1<<(j-1))][j - 1]);
}
}
build(1, n, 1);
for(;m--;){
int l, r, k; cin >> l >> r >> k;
cout << (query(l, r, k) <= k) << '\n';
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int T = 1, aa;
// cin >> T;aa=T;
while(T--){
// cout << "Case #" << aa-T << ": ";
solve();
cout << '\n';
}
return 0;
}
Compilation message
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:99:16: warning: unused variable 'aa' [-Wunused-variable]
99 | int T = 1, aa;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
94156 KB |
Output is correct |
2 |
Correct |
47 ms |
94192 KB |
Output is correct |
3 |
Incorrect |
67 ms |
94196 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
94156 KB |
Output is correct |
2 |
Correct |
47 ms |
94192 KB |
Output is correct |
3 |
Incorrect |
67 ms |
94196 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
540 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3045 ms |
115756 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
94156 KB |
Output is correct |
2 |
Correct |
47 ms |
94192 KB |
Output is correct |
3 |
Incorrect |
67 ms |
94196 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
94156 KB |
Output is correct |
2 |
Correct |
47 ms |
94192 KB |
Output is correct |
3 |
Incorrect |
67 ms |
94196 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |