#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define pb emplace_back
#define lb(v,k) (lower_bound(all(v),(k))-v.begin())
#define ub(v,k) (upper_bound(all(v),(k))-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void outset(T s){auto itr=s.begin();while(itr!=s.end()){if(itr!=s.begin())cout<<' ';cout<<*itr;itr++;}cout<<'\n';}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
class segtree{
vi seg;ll N=1;
public:
ll g=-inf;
ll f(ll a,ll b){
return max(a,b);
}
segtree(ll n,ll k){
while(N<n)N*=2;
seg=vi(N*2-1,g);
rep(i,n)seg[i+N-1]=k;
for(ll i=N-2;i>=0;i--)seg[i]=f(seg[i*2+1],seg[i*2+2]);
}
segtree(vi v){
while(N<v.size())N*=2;
seg=vi(N*2-1,g);
rep(i,v.size())seg[i+N-1]=v[i];
for(ll i=N-2;i>=0;i--)seg[i]=f(seg[i*2+1],seg[i*2+2]);
}
void add(ll i,ll x){
i+=N-1;
seg[i]+=x;
while(i>0){
i=(i-1)/2;
seg[i]=f(seg[i*2+1],seg[i*2+2]);
}
}
void update(ll i,ll x){
i+=N-1;
seg[i]=x;
while(i>0){
i=(i-1)/2;
seg[i]=f(seg[i*2+1],seg[i*2+2]);
}
}
ll get(ll l,ll r){
ll res=g;
l+=N-1;r+=N-1;
while(l<r){
if(!(l&1))res=f(res,seg[l]);
if(!(r&1))res=f(res,seg[r-1]);
l=l>>1;
r=(r-1)>>1;
}
return res;
}
void debug(ll n){
rep(i,n)cout<<' '<<seg[i+N-1];cout<<endl;
}
};
int main(){
ll n,q;cin>>n>>q;
vi v(n);rep(i,n)cin>>v[i];
vi st;
vvi tmp(n);
for(int i=n-1;i>=0;i--){
while(st.size()&&v[st.back()]<v[i]){
tmp[i].pb(st.back());
st.pop_back();
}
st.pb(i);
}
vvi query(n);
vi ans(q);
vi r(q),val(q);
rep(i,q){
ll l;cin>>l>>r[i]>>val[i];l--;r[i]--;
query[l].pb(i);
}
segtree seg(n,-1);
for(int i=n-1;i>=0;i--){
for(ll x:tmp[i])seg.update(x,v[i]+v[x]);
for(ll x:query[i])if(seg.get(0,r[x]+1)<=val[x])ans[x]=1;
}
for(ll x:ans)out(x);
}
Compilation message
sortbooks.cpp: In constructor 'segtree::segtree(vi)':
sortbooks.cpp:64:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | while(N<v.size())N*=2;
| ~^~~~~~~~~
sortbooks.cpp: In member function 'void segtree::debug(ll)':
sortbooks.cpp:3:20: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
3 | #define rep(i, n) for(long long i=0;i<(long long)(n);i++)
| ^~~
sortbooks.cpp:97:9: note: in expansion of macro 'rep'
97 | rep(i,n)cout<<' '<<seg[i+N-1];cout<<endl;
| ^~~
sortbooks.cpp:97:39: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
97 | rep(i,n)cout<<' '<<seg[i+N-1];cout<<endl;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
2 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
2 ms |
308 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
356 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
2 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
2 ms |
308 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
356 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
5 ms |
588 KB |
Output is correct |
12 |
Correct |
8 ms |
960 KB |
Output is correct |
13 |
Correct |
8 ms |
1016 KB |
Output is correct |
14 |
Correct |
10 ms |
1144 KB |
Output is correct |
15 |
Correct |
10 ms |
1100 KB |
Output is correct |
16 |
Correct |
8 ms |
1044 KB |
Output is correct |
17 |
Correct |
8 ms |
844 KB |
Output is correct |
18 |
Correct |
9 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2523 ms |
145204 KB |
Output is correct |
2 |
Correct |
2509 ms |
168376 KB |
Output is correct |
3 |
Correct |
2486 ms |
168316 KB |
Output is correct |
4 |
Correct |
2515 ms |
168388 KB |
Output is correct |
5 |
Correct |
2408 ms |
157532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
172 ms |
16324 KB |
Output is correct |
2 |
Correct |
162 ms |
15656 KB |
Output is correct |
3 |
Correct |
170 ms |
15004 KB |
Output is correct |
4 |
Correct |
163 ms |
15220 KB |
Output is correct |
5 |
Correct |
164 ms |
15332 KB |
Output is correct |
6 |
Correct |
135 ms |
14148 KB |
Output is correct |
7 |
Correct |
138 ms |
14108 KB |
Output is correct |
8 |
Correct |
151 ms |
14660 KB |
Output is correct |
9 |
Correct |
96 ms |
5160 KB |
Output is correct |
10 |
Correct |
143 ms |
14620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
2 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
2 ms |
308 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
356 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
5 ms |
588 KB |
Output is correct |
12 |
Correct |
8 ms |
960 KB |
Output is correct |
13 |
Correct |
8 ms |
1016 KB |
Output is correct |
14 |
Correct |
10 ms |
1144 KB |
Output is correct |
15 |
Correct |
10 ms |
1100 KB |
Output is correct |
16 |
Correct |
8 ms |
1044 KB |
Output is correct |
17 |
Correct |
8 ms |
844 KB |
Output is correct |
18 |
Correct |
9 ms |
972 KB |
Output is correct |
19 |
Correct |
438 ms |
34988 KB |
Output is correct |
20 |
Correct |
447 ms |
34996 KB |
Output is correct |
21 |
Correct |
393 ms |
33348 KB |
Output is correct |
22 |
Correct |
411 ms |
33452 KB |
Output is correct |
23 |
Correct |
421 ms |
33416 KB |
Output is correct |
24 |
Correct |
382 ms |
31028 KB |
Output is correct |
25 |
Correct |
393 ms |
31076 KB |
Output is correct |
26 |
Correct |
429 ms |
31920 KB |
Output is correct |
27 |
Correct |
421 ms |
31784 KB |
Output is correct |
28 |
Correct |
446 ms |
32124 KB |
Output is correct |
29 |
Correct |
448 ms |
32948 KB |
Output is correct |
30 |
Correct |
449 ms |
32804 KB |
Output is correct |
31 |
Correct |
428 ms |
32692 KB |
Output is correct |
32 |
Correct |
432 ms |
32832 KB |
Output is correct |
33 |
Correct |
449 ms |
32792 KB |
Output is correct |
34 |
Correct |
369 ms |
30436 KB |
Output is correct |
35 |
Correct |
365 ms |
30628 KB |
Output is correct |
36 |
Correct |
348 ms |
30316 KB |
Output is correct |
37 |
Correct |
346 ms |
30260 KB |
Output is correct |
38 |
Correct |
359 ms |
30488 KB |
Output is correct |
39 |
Correct |
357 ms |
31028 KB |
Output is correct |
40 |
Correct |
316 ms |
22900 KB |
Output is correct |
41 |
Correct |
336 ms |
30272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
2 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
2 ms |
308 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
356 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
5 ms |
588 KB |
Output is correct |
12 |
Correct |
8 ms |
960 KB |
Output is correct |
13 |
Correct |
8 ms |
1016 KB |
Output is correct |
14 |
Correct |
10 ms |
1144 KB |
Output is correct |
15 |
Correct |
10 ms |
1100 KB |
Output is correct |
16 |
Correct |
8 ms |
1044 KB |
Output is correct |
17 |
Correct |
8 ms |
844 KB |
Output is correct |
18 |
Correct |
9 ms |
972 KB |
Output is correct |
19 |
Correct |
2523 ms |
145204 KB |
Output is correct |
20 |
Correct |
2509 ms |
168376 KB |
Output is correct |
21 |
Correct |
2486 ms |
168316 KB |
Output is correct |
22 |
Correct |
2515 ms |
168388 KB |
Output is correct |
23 |
Correct |
2408 ms |
157532 KB |
Output is correct |
24 |
Correct |
172 ms |
16324 KB |
Output is correct |
25 |
Correct |
162 ms |
15656 KB |
Output is correct |
26 |
Correct |
170 ms |
15004 KB |
Output is correct |
27 |
Correct |
163 ms |
15220 KB |
Output is correct |
28 |
Correct |
164 ms |
15332 KB |
Output is correct |
29 |
Correct |
135 ms |
14148 KB |
Output is correct |
30 |
Correct |
138 ms |
14108 KB |
Output is correct |
31 |
Correct |
151 ms |
14660 KB |
Output is correct |
32 |
Correct |
96 ms |
5160 KB |
Output is correct |
33 |
Correct |
143 ms |
14620 KB |
Output is correct |
34 |
Correct |
438 ms |
34988 KB |
Output is correct |
35 |
Correct |
447 ms |
34996 KB |
Output is correct |
36 |
Correct |
393 ms |
33348 KB |
Output is correct |
37 |
Correct |
411 ms |
33452 KB |
Output is correct |
38 |
Correct |
421 ms |
33416 KB |
Output is correct |
39 |
Correct |
382 ms |
31028 KB |
Output is correct |
40 |
Correct |
393 ms |
31076 KB |
Output is correct |
41 |
Correct |
429 ms |
31920 KB |
Output is correct |
42 |
Correct |
421 ms |
31784 KB |
Output is correct |
43 |
Correct |
446 ms |
32124 KB |
Output is correct |
44 |
Correct |
448 ms |
32948 KB |
Output is correct |
45 |
Correct |
449 ms |
32804 KB |
Output is correct |
46 |
Correct |
428 ms |
32692 KB |
Output is correct |
47 |
Correct |
432 ms |
32832 KB |
Output is correct |
48 |
Correct |
449 ms |
32792 KB |
Output is correct |
49 |
Correct |
369 ms |
30436 KB |
Output is correct |
50 |
Correct |
365 ms |
30628 KB |
Output is correct |
51 |
Correct |
348 ms |
30316 KB |
Output is correct |
52 |
Correct |
346 ms |
30260 KB |
Output is correct |
53 |
Correct |
359 ms |
30488 KB |
Output is correct |
54 |
Correct |
357 ms |
31028 KB |
Output is correct |
55 |
Correct |
316 ms |
22900 KB |
Output is correct |
56 |
Correct |
336 ms |
30272 KB |
Output is correct |
57 |
Correct |
2583 ms |
170600 KB |
Output is correct |
58 |
Correct |
2573 ms |
170504 KB |
Output is correct |
59 |
Correct |
2485 ms |
166804 KB |
Output is correct |
60 |
Correct |
2937 ms |
166984 KB |
Output is correct |
61 |
Correct |
2533 ms |
166808 KB |
Output is correct |
62 |
Correct |
2434 ms |
166812 KB |
Output is correct |
63 |
Correct |
1930 ms |
147988 KB |
Output is correct |
64 |
Correct |
1945 ms |
147944 KB |
Output is correct |
65 |
Correct |
2334 ms |
155828 KB |
Output is correct |
66 |
Correct |
2339 ms |
155720 KB |
Output is correct |
67 |
Correct |
2397 ms |
155776 KB |
Output is correct |
68 |
Correct |
2502 ms |
159608 KB |
Output is correct |
69 |
Correct |
2486 ms |
159512 KB |
Output is correct |
70 |
Correct |
2464 ms |
158892 KB |
Output is correct |
71 |
Correct |
2389 ms |
158976 KB |
Output is correct |
72 |
Correct |
2435 ms |
158832 KB |
Output is correct |
73 |
Correct |
1877 ms |
145668 KB |
Output is correct |
74 |
Correct |
1966 ms |
146652 KB |
Output is correct |
75 |
Correct |
1875 ms |
145672 KB |
Output is correct |
76 |
Correct |
1872 ms |
145564 KB |
Output is correct |
77 |
Correct |
1840 ms |
145716 KB |
Output is correct |
78 |
Correct |
2084 ms |
151224 KB |
Output is correct |
79 |
Correct |
1717 ms |
102836 KB |
Output is correct |
80 |
Correct |
2000 ms |
146220 KB |
Output is correct |