// cre: Nguyen Ngoc Hung - Train VOI 2024
#include<bits/stdc++.h>
using namespace std;
#define __nnhzzz__ signed main()
#define BIT(i,j) ((i>>j)&1LL)
#define MASK(i) (1LL<<i)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) (int)(x).size()
#define gcd __gcd
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define pii pair<int,int>
#define vpii vector<pii>
#define REP(i,a,b) for(int i = (a); i<=(b); ++i)
#define REPD(i,a,b) for(int i = (a); i>=(b); --i)
#define REPDIS(i,a,b,c) for(int i = (a); i<=(b); i+=c)
#define endl "\n"
#define MP make_pair
// #define int ll
//-------------------------------------------------------------//
const int inf = 1e9+7,LOG = 20,MAXN = 1e6+7,N = 1e2+3;
const int MOD = 1e9+7,MOD1 = 1e9+207,MOD2 = 1e9+407,MOD3 = 998244353;
//-------------------------------------------------------------//
template<typename T> bool mini(T &a, const T &b){if(a>b){a=b;return true;}return false;}
template<typename T> bool maxi(T &a, const T &b){if(a<b){a=b;return true;}return false;}
/*
----------------------------------------------------------------
END OF TEMPLATE
----------------------------------------------------------------
Nguyen Ngoc Hung - nnhzzz
Training for VOI24 gold medal
----------------------------------------------------------------
*/
using T = tuple<int,int,int>;
struct Node{
int val,ma;
Node(int x):val(x),ma(0){};
};
Node merge(const Node &a, const Node &b){
Node res = a; int tmp = 0;
if(a.ma!=0 && maxi(tmp,a.val+a.ma)){
res.val = a.val;
res.ma = a.ma;
}
if(b.ma!=0 && maxi(tmp,b.val+b.ma)){
res.val = b.val;
res.ma = b.ma;
}
return res;
}
struct segment_tree{
vector<Node> st;
vi lazy;
int n;
void pushdown(int id, int l, int r){
if(l==r || lazy[id]==0) return ;
if(st[id<<1].val>=lazy[id]) maxi(st[id<<1].ma,lazy[id]);
if(st[id<<1|1].val>=lazy[id]) maxi(st[id<<1|1].ma,lazy[id]);
maxi(lazy[id<<1],lazy[id]);
maxi(lazy[id<<1|1],lazy[id]);
lazy[id] = 0;
}
void update(int id, int l, int r, int lo, int hi, int x){
if(l>hi || r<lo) return ;
if(l>=lo && r<=hi){
if(st[id].val>=x) maxi(st[id].ma,x);
maxi(lazy[id],x);
pushdown(id,l,r);
return ;
}
pushdown(id,l,r);
int mid = (l+r)>>1;
update(id<<1,l,mid,lo,hi,x);
update(id<<1|1,mid+1,r,lo,hi,x);
st[id] = merge(st[id<<1],st[id<<1|1]);
}
void add(int id, int l, int r, int pos, int x){
if(l>pos || r<pos) return ;
if(l==r){
st[id].val = x;
return ;
}
pushdown(id,l,r);
int mid = (l+r)>>1;
add(id<<1,l,mid,pos,x);
add(id<<1|1,mid+1,r,pos,x);
st[id] = merge(st[id<<1],st[id<<1|1]);
}
int get(int id, int l, int r, int lo, int hi){
if(l>hi || r<lo) return 0;
pushdown(id,l,r);
if(l>=lo && r<=hi){
if(st[id].ma==0) return 0;
return st[id].ma+st[id].val;
}
int mid = (l+r)>>1;
return max(get(id<<1,l,mid,lo,hi),get(id<<1|1,mid+1,r,lo,hi));
}
void update(int l, int r, int x){
update(1,1,n,l,r,x);
}
void add(int pos, int x){
add(1,1,n,pos,x);
}
int get(int l, int r){
return get(1,1,n,l,r);
}
segment_tree(int x):n(x){
st.assign(n<<2|1,Node(0));
lazy.assign(n<<2|1,0);
}
};
struct fenwick_tree{
vi f;
int n;
void update(int id, int val){
for(; id>0; id-=id&-id) maxi(f[id],val);
}
int get(int id){
int res = 0;
for(; id<=n; id+=id&-id) maxi(res,f[id]);
return res;
}
fenwick_tree(int _n):n(_n){
f.assign(n+3,0);
}
};
int a[MAXN];
vector<T> adj[MAXN];
int n,q;
void sub1(){
while(q--){
int ok = 1,l,r,k; cin >> l >> r >> k;
vi b;
REP(i,l,r) b.push_back(a[i]);
REPD(i,r-l,0){
int ma = 0,pos = 0;
REP(j,0,i){
if(maxi(ma,b[j]) || ma==b[j]) pos = j;
}
while(pos!=i){
if(b[pos]+b[pos+1]<=k){
swap(b[pos],b[pos+1]);
++pos;
}else{
ok = 0; break;
}
}
}
if(is_sorted(ALL(b)) && ok==1) cout << 1; else cout << 0; cout << endl;
}
}
void sub3(){
vi fre(n+3,n+1);
a[n+1] = inf;
REPD(i,n,1){
if(a[i]>a[i+1]) fre[i] = i+1;
else fre[i] = fre[i+1];
}
while(q--){
int l,r,k; cin >> l >> r >> k;
if(fre[l]>r) cout << 1; else cout << 0; cout << endl;
}
}
void sub5(){
vi res(q+3,0);
REP(i,1,q){
int l,r,k; cin >> l >> r >> k;
adj[r].emplace_back(l,k,i);
}
// segment_tree seg(n);
fenwick_tree fen(n+3);
stack<int> st;
REP(i,1,n){
// if(i>1) seg.update(1,i-1,a[i]);
while(SZ(st)!=0 && a[st.top()]<=a[i]) st.pop();
if(SZ(st)!=0) fen.update(st.top(),a[st.top()]+a[i]);
int r = i;
for(const auto &it:adj[r]){
int l = get<0>(it),k = get<1>(it),id = get<2>(it);
// if(seg.get(l,r)<=k) res[id] = 1;
// cerr << seg.get(l,r) << endl;
if(fen.get(l)<=k) res[id] = 1;
}
st.push(i);
// seg.add(i,a[i]);
}
REP(i,1,q) cout << res[i] << endl;
}
void solve(){
cin >> n >> q;
REP(i,1,n) cin >> a[i];
sub5(); return ;
if(n<=500 && q<=500){
sub1(); return ;
}
sub5();
}
__nnhzzz__{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "test"
if(fopen(name".inp","r")){
freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);
}
#define name1 "nnhzzz"
if(fopen(name1".inp","r")){
freopen(name1".inp","r",stdin);
freopen(name1".out","w",stdout);
}
int test = 1;
while(test--){
solve();
}
cerr << "\nTime elapsed: " << 1000*clock()/CLOCKS_PER_SEC << "ms\n";
return 0;
}
Compilation message
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:239:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
239 | freopen(name".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:240:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
240 | freopen(name".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:244:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
244 | freopen(name1".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:245:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
245 | freopen(name1".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
25180 KB |
Output is correct |
2 |
Correct |
5 ms |
25180 KB |
Output is correct |
3 |
Correct |
5 ms |
25180 KB |
Output is correct |
4 |
Correct |
5 ms |
25180 KB |
Output is correct |
5 |
Correct |
5 ms |
25180 KB |
Output is correct |
6 |
Correct |
5 ms |
25180 KB |
Output is correct |
7 |
Correct |
5 ms |
25180 KB |
Output is correct |
8 |
Correct |
6 ms |
25180 KB |
Output is correct |
9 |
Correct |
5 ms |
25180 KB |
Output is correct |
10 |
Correct |
5 ms |
25176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
25180 KB |
Output is correct |
2 |
Correct |
5 ms |
25180 KB |
Output is correct |
3 |
Correct |
5 ms |
25180 KB |
Output is correct |
4 |
Correct |
5 ms |
25180 KB |
Output is correct |
5 |
Correct |
5 ms |
25180 KB |
Output is correct |
6 |
Correct |
5 ms |
25180 KB |
Output is correct |
7 |
Correct |
5 ms |
25180 KB |
Output is correct |
8 |
Correct |
6 ms |
25180 KB |
Output is correct |
9 |
Correct |
5 ms |
25180 KB |
Output is correct |
10 |
Correct |
5 ms |
25176 KB |
Output is correct |
11 |
Correct |
6 ms |
25180 KB |
Output is correct |
12 |
Correct |
7 ms |
25436 KB |
Output is correct |
13 |
Correct |
7 ms |
25432 KB |
Output is correct |
14 |
Correct |
7 ms |
25436 KB |
Output is correct |
15 |
Correct |
7 ms |
25436 KB |
Output is correct |
16 |
Correct |
7 ms |
25436 KB |
Output is correct |
17 |
Correct |
7 ms |
25472 KB |
Output is correct |
18 |
Correct |
7 ms |
25436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
611 ms |
59200 KB |
Output is correct |
2 |
Correct |
612 ms |
76116 KB |
Output is correct |
3 |
Correct |
611 ms |
75904 KB |
Output is correct |
4 |
Correct |
626 ms |
76072 KB |
Output is correct |
5 |
Correct |
598 ms |
76092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
28244 KB |
Output is correct |
2 |
Correct |
42 ms |
29268 KB |
Output is correct |
3 |
Correct |
41 ms |
29520 KB |
Output is correct |
4 |
Correct |
42 ms |
29524 KB |
Output is correct |
5 |
Correct |
42 ms |
29520 KB |
Output is correct |
6 |
Correct |
34 ms |
29008 KB |
Output is correct |
7 |
Correct |
35 ms |
29012 KB |
Output is correct |
8 |
Correct |
41 ms |
28976 KB |
Output is correct |
9 |
Correct |
28 ms |
28180 KB |
Output is correct |
10 |
Correct |
39 ms |
29192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
25180 KB |
Output is correct |
2 |
Correct |
5 ms |
25180 KB |
Output is correct |
3 |
Correct |
5 ms |
25180 KB |
Output is correct |
4 |
Correct |
5 ms |
25180 KB |
Output is correct |
5 |
Correct |
5 ms |
25180 KB |
Output is correct |
6 |
Correct |
5 ms |
25180 KB |
Output is correct |
7 |
Correct |
5 ms |
25180 KB |
Output is correct |
8 |
Correct |
6 ms |
25180 KB |
Output is correct |
9 |
Correct |
5 ms |
25180 KB |
Output is correct |
10 |
Correct |
5 ms |
25176 KB |
Output is correct |
11 |
Correct |
6 ms |
25180 KB |
Output is correct |
12 |
Correct |
7 ms |
25436 KB |
Output is correct |
13 |
Correct |
7 ms |
25432 KB |
Output is correct |
14 |
Correct |
7 ms |
25436 KB |
Output is correct |
15 |
Correct |
7 ms |
25436 KB |
Output is correct |
16 |
Correct |
7 ms |
25436 KB |
Output is correct |
17 |
Correct |
7 ms |
25472 KB |
Output is correct |
18 |
Correct |
7 ms |
25436 KB |
Output is correct |
19 |
Correct |
101 ms |
35012 KB |
Output is correct |
20 |
Correct |
109 ms |
34900 KB |
Output is correct |
21 |
Correct |
88 ms |
34500 KB |
Output is correct |
22 |
Correct |
89 ms |
34456 KB |
Output is correct |
23 |
Correct |
98 ms |
34412 KB |
Output is correct |
24 |
Correct |
79 ms |
34084 KB |
Output is correct |
25 |
Correct |
82 ms |
34128 KB |
Output is correct |
26 |
Correct |
86 ms |
34640 KB |
Output is correct |
27 |
Correct |
85 ms |
34448 KB |
Output is correct |
28 |
Correct |
87 ms |
34516 KB |
Output is correct |
29 |
Correct |
91 ms |
34980 KB |
Output is correct |
30 |
Correct |
91 ms |
34976 KB |
Output is correct |
31 |
Correct |
91 ms |
34836 KB |
Output is correct |
32 |
Correct |
96 ms |
34900 KB |
Output is correct |
33 |
Correct |
98 ms |
34952 KB |
Output is correct |
34 |
Correct |
75 ms |
33700 KB |
Output is correct |
35 |
Correct |
75 ms |
33812 KB |
Output is correct |
36 |
Correct |
74 ms |
33872 KB |
Output is correct |
37 |
Correct |
78 ms |
34100 KB |
Output is correct |
38 |
Correct |
75 ms |
33876 KB |
Output is correct |
39 |
Correct |
85 ms |
33848 KB |
Output is correct |
40 |
Correct |
72 ms |
32856 KB |
Output is correct |
41 |
Correct |
82 ms |
33544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
25180 KB |
Output is correct |
2 |
Correct |
5 ms |
25180 KB |
Output is correct |
3 |
Correct |
5 ms |
25180 KB |
Output is correct |
4 |
Correct |
5 ms |
25180 KB |
Output is correct |
5 |
Correct |
5 ms |
25180 KB |
Output is correct |
6 |
Correct |
5 ms |
25180 KB |
Output is correct |
7 |
Correct |
5 ms |
25180 KB |
Output is correct |
8 |
Correct |
6 ms |
25180 KB |
Output is correct |
9 |
Correct |
5 ms |
25180 KB |
Output is correct |
10 |
Correct |
5 ms |
25176 KB |
Output is correct |
11 |
Correct |
6 ms |
25180 KB |
Output is correct |
12 |
Correct |
7 ms |
25436 KB |
Output is correct |
13 |
Correct |
7 ms |
25432 KB |
Output is correct |
14 |
Correct |
7 ms |
25436 KB |
Output is correct |
15 |
Correct |
7 ms |
25436 KB |
Output is correct |
16 |
Correct |
7 ms |
25436 KB |
Output is correct |
17 |
Correct |
7 ms |
25472 KB |
Output is correct |
18 |
Correct |
7 ms |
25436 KB |
Output is correct |
19 |
Correct |
611 ms |
59200 KB |
Output is correct |
20 |
Correct |
612 ms |
76116 KB |
Output is correct |
21 |
Correct |
611 ms |
75904 KB |
Output is correct |
22 |
Correct |
626 ms |
76072 KB |
Output is correct |
23 |
Correct |
598 ms |
76092 KB |
Output is correct |
24 |
Correct |
46 ms |
28244 KB |
Output is correct |
25 |
Correct |
42 ms |
29268 KB |
Output is correct |
26 |
Correct |
41 ms |
29520 KB |
Output is correct |
27 |
Correct |
42 ms |
29524 KB |
Output is correct |
28 |
Correct |
42 ms |
29520 KB |
Output is correct |
29 |
Correct |
34 ms |
29008 KB |
Output is correct |
30 |
Correct |
35 ms |
29012 KB |
Output is correct |
31 |
Correct |
41 ms |
28976 KB |
Output is correct |
32 |
Correct |
28 ms |
28180 KB |
Output is correct |
33 |
Correct |
39 ms |
29192 KB |
Output is correct |
34 |
Correct |
101 ms |
35012 KB |
Output is correct |
35 |
Correct |
109 ms |
34900 KB |
Output is correct |
36 |
Correct |
88 ms |
34500 KB |
Output is correct |
37 |
Correct |
89 ms |
34456 KB |
Output is correct |
38 |
Correct |
98 ms |
34412 KB |
Output is correct |
39 |
Correct |
79 ms |
34084 KB |
Output is correct |
40 |
Correct |
82 ms |
34128 KB |
Output is correct |
41 |
Correct |
86 ms |
34640 KB |
Output is correct |
42 |
Correct |
85 ms |
34448 KB |
Output is correct |
43 |
Correct |
87 ms |
34516 KB |
Output is correct |
44 |
Correct |
91 ms |
34980 KB |
Output is correct |
45 |
Correct |
91 ms |
34976 KB |
Output is correct |
46 |
Correct |
91 ms |
34836 KB |
Output is correct |
47 |
Correct |
96 ms |
34900 KB |
Output is correct |
48 |
Correct |
98 ms |
34952 KB |
Output is correct |
49 |
Correct |
75 ms |
33700 KB |
Output is correct |
50 |
Correct |
75 ms |
33812 KB |
Output is correct |
51 |
Correct |
74 ms |
33872 KB |
Output is correct |
52 |
Correct |
78 ms |
34100 KB |
Output is correct |
53 |
Correct |
75 ms |
33876 KB |
Output is correct |
54 |
Correct |
85 ms |
33848 KB |
Output is correct |
55 |
Correct |
72 ms |
32856 KB |
Output is correct |
56 |
Correct |
82 ms |
33544 KB |
Output is correct |
57 |
Correct |
606 ms |
75600 KB |
Output is correct |
58 |
Correct |
602 ms |
75780 KB |
Output is correct |
59 |
Correct |
553 ms |
74324 KB |
Output is correct |
60 |
Correct |
553 ms |
74208 KB |
Output is correct |
61 |
Correct |
551 ms |
74208 KB |
Output is correct |
62 |
Correct |
549 ms |
74324 KB |
Output is correct |
63 |
Correct |
376 ms |
72004 KB |
Output is correct |
64 |
Correct |
383 ms |
72220 KB |
Output is correct |
65 |
Correct |
501 ms |
74128 KB |
Output is correct |
66 |
Correct |
507 ms |
74004 KB |
Output is correct |
67 |
Correct |
532 ms |
74116 KB |
Output is correct |
68 |
Correct |
617 ms |
84308 KB |
Output is correct |
69 |
Correct |
582 ms |
84276 KB |
Output is correct |
70 |
Correct |
568 ms |
83792 KB |
Output is correct |
71 |
Correct |
564 ms |
75604 KB |
Output is correct |
72 |
Correct |
567 ms |
75344 KB |
Output is correct |
73 |
Correct |
364 ms |
68916 KB |
Output is correct |
74 |
Correct |
360 ms |
69072 KB |
Output is correct |
75 |
Correct |
353 ms |
68744 KB |
Output is correct |
76 |
Correct |
375 ms |
68868 KB |
Output is correct |
77 |
Correct |
355 ms |
68432 KB |
Output is correct |
78 |
Correct |
526 ms |
70820 KB |
Output is correct |
79 |
Correct |
415 ms |
64180 KB |
Output is correct |
80 |
Correct |
502 ms |
69712 KB |
Output is correct |