#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include<bits/stdc++.h>
using namespace std;
#define ll int
#define endl '\n'
struct segTree{
vector<ll> v;
vector<ll> upd;
int size = 1;
void init(int n){
while(size < n) size *= 2;
v.assign(2 * size, 0);
upd.assign(2 * size, 0);
}
void set(int l, int r, int x, int lx, int rx, ll u){
if(lx >= l && rx <= r){
v[x] += u * (rx - lx);
upd[x] += u;
return;
}
if(lx >= r || l >= rx) return;
int m = (lx + rx) / 2;
set(l, r, 2 * x + 1, lx, m, u);
set(l, r, 2 * x + 2, m, rx, u);
v[x] = v[2 * x + 1] + v[2 * x + 2] + upd[x] * (rx - lx);
return;
}
void set(int l, int r, int u){
return set(l, r, 0, 0, size, u);
}
void build(vector<ll> &a, int x, int lx, int rx){
if (rx - lx == 1){
if (lx < (int)a.size()){
v[x] = a[lx];
}
return;
}
int m = (lx + rx) / 2;
build(a, 2 * x + 1, lx, m);
build(a, 2 * x + 2, m, rx);
v[x] = v[2 * x + 1] + v[2 * x + 2];
}
void build(vector<ll>& a){
build(a, 0, 0, size);
}
pair<ll, int> sum(int l, int r, int x, int lx, int rx){
if(lx >= l && rx <= r) return {v[x], rx - lx};
else if(lx >= r || rx <= l) return {0, 0};
int m = (lx + rx) / 2;
pair<ll, int> s1 = sum(l, r, 2 * x + 1, lx, m);
pair<ll, int> s2 = sum(l, r, 2 * x + 2, m, rx);
return {s1.first + s2.first + upd[x] * (s1.second + s2.second), (s1.second + s2.second)};
}
ll sum(int l, int r){
pair<ll, int> p = sum(l, r, 0, 0, size);
return p.first;
}
};
bool cmp(pair<pair<int,int>, pair<int,int>> a, pair<pair<int,int>, pair<int,int>> b){
return a.first.first > b.first.first;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,q; cin>>n>>q;
vector<int> v(n); for(int& i : v) cin>>i;
vector<int> pre(n+1);
vector<int> inv;
for(int i = 1; i<n; i++){
pre[i+1] = pre[i] + (v[i] < v[i-1]);
if(v[i] < v[i-1]) inv.push_back(i-1);
}
vector<tuple<int,int,int,int,int>> st;
for(int _ = 0; _<q; _++){
int l,r,k; cin>>l>>r>>k;
if(n > 100000){
if(pre[r] - pre[l] == 0) cout<<1<<endl;
else cout<<0<<endl;
}
else{
l--; r--;
int mx = 0;
int idx = lower_bound(inv.begin(),inv.end(),l) - inv.begin();
// cerr<<"INV ";
for(int x = idx; x < inv.size(); x++){
// cerr<<inv[x]<<" ";
if(inv[x] >= r) break;
int i = inv[x];
int l1 = max(0, k-v[i]+1);
int r1 = v[inv[x]]-1;
if(l1 > r1) continue;
// cerr<<"HERE! "<<inv[x]<<" "<<r<<" "<<l1<<" "<<r1<<" "<<endl;
st.push_back({i,r,l1,r1,_});
// cerr<<i+1<<" "<<r<<endl;
// cerr<<"QUERY: "<<i<<" "<< seg.query(i+1,r,v[i])<<endl;
}
// cerr<<endl;
}
}
if(n > 100000) return 0;
vector<vector<pair<pair<int,int>,pair<int,int>>>> a(n); // at time i
for(auto x : st){
int l,r,l1,r1,k; tie(l,r,l1,r1,k) = x;
a[l].push_back({{1,k},{l1,r1}});
a[r].push_back({{-1,k},{l1,r1}});
}
for(int i = 0; i<n; i++){
a[i].push_back({{0,0},{v[i],v[i]}});
}
for(int i = 0; i<n; i++){
sort(a[i].begin(),a[i].end(),cmp);
}
segTree seg; seg.init(1001);
unordered_multiset<int> act;
unordered_set<int> ans;
for(auto x : a){
for(auto y : x){
int tp = y.first.first;
int k = y.first.second;
int l = y.second.first;
int r = y.second.second;
// cerr<<"HERE "<<tp<<" "<<l<<" "<<r<<endl;
if(tp == 1){
seg.set(l,r+1,1); act.insert(k);
}
else if(tp == 0){
if(seg.sum(l,l+1) == 0) continue;
for(int i : act) ans.insert(i);
}
else{
seg.set(l,r+1,-1); act.erase(act.find(k));
}
}
}
// cout<<"Q "<<q<<endl;
for(int i = 0; i<q; i++){
if(ans.count(i)) cout<<"0"<<endl;
else cout<<"1"<<endl;
}
}
Compilation message
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:94:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for(int x = idx; x < inv.size(); x++){
| ~~^~~~~~~~~~~~
sortbooks.cpp:91:9: warning: unused variable 'mx' [-Wunused-variable]
91 | int mx = 0;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
404 ms |
25284 KB |
Output is correct |
2 |
Correct |
371 ms |
29416 KB |
Output is correct |
3 |
Correct |
392 ms |
29436 KB |
Output is correct |
4 |
Correct |
437 ms |
29532 KB |
Output is correct |
5 |
Correct |
447 ms |
27212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
202 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |