Submission #335902

#TimeUsernameProblemLanguageResultExecution timeMemory
335902tengiz05Hedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++17
64 / 100
610 ms70764 KiB
#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define PI acos(-1)
#define ld long double
const int mod = 1e9+7, N = 1e6+5;
int msb(int val){return sizeof(int)*8-__builtin_clzll(val);}
int a[N], n, k, sz=1;
struct Node{
	int mx, ans;
	vector<int> v;
};
Node t[1<<20];
vector<int> mrg(vector<int> &l, vector<int> &r){
	vector<int> t={-mod,-mod};
	int i=0, j=0;
	while(i<l.size() && j<r.size()){
		if(l[i] <= r[j]){
			if(l[i]!=-mod)
				t.pb(l[i]);
			i++;
		}else {
			if(r[j]!=-mod)
				t.pb(r[j]);
			j++;
		}
	}
	while(i < l.size())t.pb(l[i]),i++;
	while(j < r.size())t.pb(r[j]),j++;
//	cout << "ans = ";for(auto x : t)cout << x << ' ';cout << '\n';
	return t;
}
Node Merge(Node &l, Node &r){
	Node res;
	res.v = mrg(l.v,r.v);
	res.mx = max(l.mx, r.mx);
	res.ans = max(l.ans, r.ans);
/*	for(auto x : l.v)cout << x << ' ';cout << '\n';
	for(auto x : r.v)cout << x << ' ';cout << '\n';*/
	int p = lower_bound(all(r.v), l.mx)-r.v.begin();p--;
	res.ans = max(res.ans, l.mx+r.v[p]);
/*	cout << p << '\n';
	cout << res.ans << '\n';*/
	return res;
}
/*
 
8 5
1 2 4 3 8 2 4 5
1 2 10
1 2 10
3 6 10
6 8 10
1 8 10
 
 
 
*/
int get(int l, int r){
	vector<int> rel, rer;
	for(l+=sz,r+=sz;l<=r;l>>=1,r>>=1){
		if(l%2==1)rel.pb(l++);
		if(r%2==0)rer.pb(r--);
	}reverse(all(rer));
	for(auto x : rer)rel.pb(x);
	int mx = 0 ;
	int ans = 0;
	for(auto x : rel)ans = max(t[x].ans, ans);
 
	/*cout << "values were: ";
	for(auto x : rel){
		cout << t[x].mx << ' ';
	}cout << '\n';
	cout << ans << " <- this was ans\n";
	*/
	for(int i=0;i<(int)rel.size()-1;i++){
		mx = max(mx, t[rel[i]].mx);
		int pos = lower_bound(all(t[rel[i+1]].v), mx)-t[rel[i+1]].v.begin();
		pos--;
		ans = max(ans, t[rel[i+1]].v[pos]+mx);
	}return ans;
}
 
void solve(int test_case){
	int i, j, q;
	cin >> n >> q;
	while(sz<n)sz<<=1;sz=n;
	for(i=0;i<n;i++){
		cin >> a[i];
		t[i+sz] = {a[i], 0, {-mod, -mod, a[i]}};
	}for(i=sz-1;i>0;i--)t[i] = Merge(t[i*2], t[i*2+1]);
//	for(auto x : t[1].v)cout << x << ' ';cout << '\n';
	while(q--){
		int l, r;
		cin >> l >> r >> k;
		l--, r--;
		bool is = true;
		int m=0;
		int ans = get(l,r);
	/*	for(;l<=r;l++){
			int mx = -mod;
			for(i=l+1;i<=r;i++){
				if(a[i] != a[l] && a[i] < a[l])mx = max(mx, a[i]);
			}
			m = max(m, mx+a[l]);
		}//cout << ans << ' ' << m << '\n';
		assert(ans >= m);*/
		if(ans <= k){
			cout << 1 << '\n';
		}else {
			cout << 0 << '\n';
		}
	}
	return;
}
 
signed main(){
	FASTIO;
#define MULTITEST 0
#if MULTITEST
	int ___T;
	cin >> ___T;
	for(int T_CASE = 1; T_CASE <= ___T; T_CASE++)
		solve(T_CASE);
#else
	solve(1);
#endif
	return 0;
}
 
 
 
 

Compilation message (stderr)

sortbooks.cpp: In function 'std::vector<int> mrg(std::vector<int>&, std::vector<int>&)':
sortbooks.cpp:23:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |  while(i<l.size() && j<r.size()){
      |        ~^~~~~~~~~
sortbooks.cpp:23:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |  while(i<l.size() && j<r.size()){
      |                      ~^~~~~~~~~
sortbooks.cpp:34:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |  while(i < l.size())t.pb(l[i]),i++;
      |        ~~^~~~~~~~~~
sortbooks.cpp:35:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |  while(j < r.size())t.pb(r[j]),j++;
      |        ~~^~~~~~~~~~
sortbooks.cpp: In function 'void solve(int)':
sortbooks.cpp:93:2: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
   93 |  while(sz<n)sz<<=1;sz=n;
      |  ^~~~~
sortbooks.cpp:93:20: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
   93 |  while(sz<n)sz<<=1;sz=n;
      |                    ^~
sortbooks.cpp:103:8: warning: unused variable 'is' [-Wunused-variable]
  103 |   bool is = true;
      |        ^~
sortbooks.cpp:104:7: warning: unused variable 'm' [-Wunused-variable]
  104 |   int m=0;
      |       ^
sortbooks.cpp:91:9: warning: unused variable 'j' [-Wunused-variable]
   91 |  int i, j, q;
      |         ^
#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...