Submission #396014

#TimeUsernameProblemLanguageResultExecution timeMemory
396014andremfqHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++17
100 / 100
1562 ms122076 KiB
//nossa ideia estava certa! obs principal se a resposta for i e j, entao i eh o proximo maior que j pela esquerda
#include <bits/stdc++.h>
 
#define ll long long
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define pb push_back
#define sz(x) (int)(x.size())
 
const int MAXN = 1e6+10 ;
const ll inf = 1e15+10 ;
 
using namespace std ;
 
struct Event
{
	bool type ; //0 is an object, 1 is a query
	ll weight ;
	int id ;
 
	Event(bool type = 0 , ll weight = 0 , int id =0 ) : 
	type(type) , weight(weight) , id(id) {}
 
	bool operator < ( Event other ) const
	{
		if(weight != other.weight) return weight > other.weight ;
		return type > other.type ;
	}
 
} ;
 
struct Seg
{
	int n ;
	int tree[MAXN*2] ;
 
	Seg() { for(int i= 0 ; i <MAXN*2 ; i++ ) tree[i] = -2 ; }
 
	void upd(int pos, int val) { for(pos += n ; pos > 0 ; pos >>= 1) tree[pos] = max(tree[pos] , val) ; }
	int qry(int l, int r)
	{
		int ans = -2 ;
		for(l += n , r += n ; l < r ; l >>= 1 , r >>= 1) 
		{
			if(l&1) ans = max(ans, tree[l++]) ;
			if(r&1) ans = max(ans, tree[--r]) ;
		}
		return ans ;
	}
 
} seg ;
 
int N , M ;
int lef[MAXN] , L[MAXN] , R[MAXN] ;
int ans[MAXN] ;
ll w[MAXN] , K[MAXN] ;
vector<Event> sweep ;
 
int main()
{	
	scanf("%d %d", &N, &M ) ;
	for(int i = 1 ; i <= N ; i++ ) scanf("%lld", &w[i] ) ;
 
	w[0] = inf;
	seg.n = N ;
 
	for(int i = 1 ;i <= N ; i++ )
	{
		lef[i] = i-1 ;
		while( w[lef[i]] <= w[i] ) lef[i] = lef[ lef[i] ] ;
 
		if(lef[i]) sweep.push_back( Event(0, w[lef[i]]+w[i] , i) ) ;
	}
 
	for(int i = 1 ; i <= M ; i++ )
	{
		scanf("%d %d %lld", &L[i], &R[i] , &K[i]) ;
		sweep.push_back( Event(1, K[i], i) ) ;
	}
 
	sort(all(sweep) );
 
	for(auto e : sweep )
	{		
		if(!e.type) seg.upd( e.id-1 , lef[e.id] ) ;
		else ans[e.id] = (seg.qry( L[e.id]-1 , R[e.id] ) < L[e.id]) ;
	}
 
	for(int i = 1 ; i <= M ; i++ ) printf("%d\n", ans[i]  ) ;
 
}

Compilation message (stderr)

sortbooks.cpp: In function 'int main()':
sortbooks.cpp:62:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   62 |  scanf("%d %d", &N, &M ) ;
      |  ~~~~~^~~~~~~~~~~~~~~~~~
sortbooks.cpp:63:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   63 |  for(int i = 1 ; i <= N ; i++ ) scanf("%lld", &w[i] ) ;
      |                                 ~~~~~^~~~~~~~~~~~~~~~
sortbooks.cpp:78:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   78 |   scanf("%d %d %lld", &L[i], &R[i] , &K[i]) ;
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...