Submission #236438

#TimeUsernameProblemLanguageResultExecution timeMemory
236438kshitij_sodaniHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++17
64 / 100
3092 ms249412 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;
int tree3[4000001];
int tree2[4000001];
vector<int> tree[4000001];
void build(int no,int l,int r){
	if(l==r){
		tree3[no]=it[l];
		tree[no].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);
		tree3[no]=max(tree3[no*2+1],tree3[no*2+2]);
		int ind=0;
		int ind2=0;
		while(ind<tree[no*2+1].size() and ind2<tree[no*2+2].size()){
			if(tree[no*2+1][ind]<tree[no*2+2][ind2]){
				tree[no].pb(tree[no*2+1][ind]);
				ind+=1;
			}
			else{
				tree[no].pb(tree[no*2+2][ind2]);
				ind2+=1;
			}
		}
		while(ind<tree[no*2+1].size()){
			tree[no].pb(tree[no*2+1][ind]);
			ind+=1;
		}
		while(ind2<tree[no*2+2].size()){
			tree[no].pb(tree[no*2+2][ind2]);
			ind2+=1;
		}
	//	auto x=lower_bound(tree[no*2+2].begin(),tree[no*2+2].end(),tree3[no*2+1]);
		if(tree[no*2+2][0]<tree3[no*2+1]){
			int low=0;
			int high=(int)(tree[no*2+2].size())-1;
			int mid;
			while(low<high-1){
				mid=(low+high)/2;
				if(tree[no*2+2][mid]<tree3[no*2+1]){
					low=mid;
				}
				else{
					high=mid;
				}
			}

			int ind=low;
			if(tree[no*2+2][high]<tree3[no*2+1]){
				ind=max(ind,high);
			}
		//	tree2[no]=tree3[no*2+1]+tree[no*2*2][ind];
		}
		
		else{
			tree2[no]=0;
		}
		auto x=lower_bound(tree[no*2+2].begin(),tree[no*2+2].end(),tree3[no*2+1]);
		if(x!=tree[no*2+2].begin()){
			x--;
			tree2[no]=tree3[no*2+1]+(*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]<<","<<tree3[no]<<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]);
		if(tree[no][0]<ma){
			int low=0;
			int high=(int)(tree[no].size())-1;
			int mid;
			while(low<high-1){
				mid=(low+high)/2;
				if(tree[no][mid]<ma){
					low=mid;
				}
				else{
					high=mid;
				}
			}

			int ind=low;
			if(tree[no][high]<ma){
				ind=max(ind,high);
			}
			ans=max(ans,ma+tree[no][ind]);
		}
		/*auto x=lower_bound(tree[no].begin(),tree[no].end(),ma);
		if(x!=tree[no].begin()){
			x--;
			ans=max(ans,(*x)+ma);
		}*/
		ma=max(tree3[no],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:27:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while(ind<tree[no*2+1].size() and ind2<tree[no*2+2].size()){
         ~~~^~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:27:41: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while(ind<tree[no*2+1].size() and ind2<tree[no*2+2].size()){
                                     ~~~~^~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:37:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while(ind<tree[no*2+1].size()){
         ~~~^~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:41:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while(ind2<tree[no*2+2].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...