Submission #1133160

#TimeUsernameProblemLanguageResultExecution timeMemory
1133160AgageldiHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++17
100 / 100
760 ms133340 KiB
/*
ID: agageld1
LANG: C++17
TASK:
*/
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define N 4000005
#define ff first
#define ss second
#define pb push_back
#define sz(s) (int)s.size()
#define rep(c, a, b) for(c = a; c <= b; c++)

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, t, a[N], x[N], mx, q, fn[N], answer[N];
vector <pair<int,int>> v[N];
stack <int> s;

void upd(int y,int k) {
	while(y <= 2e6) {
		fn[y] = max(fn[y],k);
		y += (y & (-y));
	}
}
int solve(int l,int r) {
	int ans = 0;
	while(r >= l){
		ans = max(ans, fn[r]);
		r -= (r&(-r));
	}
	return ans;
}

int main () {
	ios::sync_with_stdio(0);cin.tie(0);
	cin >> n >> q;
	for(int i= 1;i<=n;i++) {
		cin >> a[i];
	}
	for(int i = 1; i <= q; i++) {
		int l, r;
		cin >> l >> r >> x[i];
		v[l].pb({r,i});
	}
	for(int i = n; i >= 1; i--) {
		sort(v[i].begin(),v[i].end());
		while(sz(s) > 0 && a[s.top()] < a[i]) {
			upd(s.top(),a[i] + a[s.top()]);
			s.pop();
		}
		s.push(i);
		for(int j = 0; j < sz(v[i]); j++) {
			answer[v[i][j].ss] = (solve(i,v[i][j].ff) > x[v[i][j].ss] ? 0 : 1);
		}
	}
	for(int i = 1; i <= q;i++) {
		cout << answer[i] << "\n";
	}
}
#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...