Submission #236423

#TimeUsernameProblemLanguageResultExecution timeMemory
236423kshitij_sodaniHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++17
64 / 100
1022 ms262148 KiB
#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
int n;
int it[1000001];
int aa,bb,cc;
pair<int,vector<int>> tree[4000001];
int tree2[4000001];
void build(int no,int l,int r){
	if(l==r){
		tree[no].a=it[l];
		tree[no].b.pb(it[l]);
		tree2[no]=0;
	}
	else{
		int mid=(l+r)/2;
		build(no*2+1,l,mid);
		build(no*2+2,mid+1,r);
		tree[no].a=max(tree[no*2+1].a,tree[no*2+2].a);
		int ind=0;
		int ind2=0;
		while(ind<tree[no*2+1].b.size() and ind2<tree[no*2+2].b.size()){
			if(tree[no*2+1].b[ind]<tree[no*2+2].b[ind2]){
				tree[no].b.pb(tree[no*2+1].b[ind]);
				ind+=1;
			}
			else{
				tree[no].b.pb(tree[no*2+2].b[ind2]);
				ind2+=1;
			}
		}
		while(ind<tree[no*2+1].b.size()){
			tree[no].b.pb(tree[no*2+1].b[ind]);
			ind+=1;
		}
		while(ind2<tree[no*2+2].b.size()){
			tree[no].b.pb(tree[no*2+2].b[ind2]);
			ind2+=1;
		}
		auto x=lower_bound(tree[no*2+2].b.begin(),tree[no*2+2].b.end(),tree[no*2+1].a);
		if(x!=tree[no*2+2].b.begin()){
			x--;
			tree2[no]=tree[no*2+1].a+(*x);
		}
		else{
			tree2[no]=0;
		}
		tree2[no]=max(tree2[no],tree2[no*2+1]);
		tree2[no]=max(tree2[no],tree2[no*2+2]);
	
	}
//	cout<<l<<","<<r<<","<<tree2[no]<<","<<tree[no].a<<endl;
}
int ans=0;
int ma=0;
void query(int no,int l,int r,int aa,int bb){
	if(r<aa or l>bb){
		return;
	}
	if(aa<=l and r<=bb){
		ans=max(ans,tree2[no]);
		auto x=lower_bound(tree[no].b.begin(),tree[no].b.end(),ma);
		if(x!=tree[no].b.begin()){
			x--;
			ans=max(ans,(*x)+ma);
		}
		ma=max(tree[no].a,ma);
	}
	else{
		int mid=(l+r)/2;
		query(no*2+1,l,mid,aa,bb);
		query(no*2+2,mid+1,r,aa,bb);

	}
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int m;
	cin>>n>>m;
	for(int i=0;i<n;i++){
		cin>>it[i];
	}

	build(0,0,n-1);
	for(int i=0;i<m;i++){
		cin>>aa>>bb>>cc;
		aa-=1;
		bb-=1;
		ans=0;
		ma=0;
		query(0,0,n-1,aa,bb);
		cout<<((ans<=cc)?1:0)<<endl;
	}


	return 0;
}

Compilation message (stderr)

sortbooks.cpp: In function 'void build(int, int, int)':
sortbooks.cpp:26:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while(ind<tree[no*2+1].b.size() and ind2<tree[no*2+2].b.size()){
         ~~~^~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:26:43: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while(ind<tree[no*2+1].b.size() and ind2<tree[no*2+2].b.size()){
                                       ~~~~^~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:36:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while(ind<tree[no*2+1].b.size()){
         ~~~^~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:40:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while(ind2<tree[no*2+2].b.size()){
         ~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...