Submission #1133156

#TimeUsernameProblemLanguageResultExecution timeMemory
1133156Halym2007Hedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++17
100 / 100
887 ms67052 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define sz size()
#define ff first
#define ss second
#define pb push_back
#define pii pair <int, int>
#define dur exit(0)
#define dur1 return(0)
const int N = 1e6 + 5;
int st[4 * N], jog[N], k[N], a[N]; 
vector <pii> v[N];

void update (int idx, int l, int r, int x, int y) {
	if (l == r) {
		st[idx] = y;
		return;
	}
	int mid = (l + r) / 2;
	if (x <= mid)
		update (idx * 2, l, mid, x, y);
	else 
		update (idx * 2 + 1, mid + 1, r, x, y);
	st[idx] = max (st[idx * 2], st[idx * 2 + 1]);
}

int jogap (int idx, int l, int r, int x, int y) {
	if (x <= l and r <= y) {
		return st[idx];
	}
	if (x > r or l > y) return 0;
	int mid = (l + r) / 2;
	return max (jogap (idx * 2, l, mid, x, y), jogap (idx * 2 + 1, mid + 1, r, x, y));
	
}

void build (int idx, int l, int r) {
	if (l == r) {
		st[idx] = a[l];
		return;
	}
	int mid = (l + r) / 2;
	build (idx * 2, l, mid);
	build (idx * 2 + 1, mid + 1, r);
	st[idx] = max (st[idx * 2], st[idx * 2 + 1]);
}


int main () {
//	freopen ("input.txt", "r", stdin);
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n, q;
	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 >> k[i];
		v[l].pb ({r, i});
	}
	stack <int> s;
	for (int i = n; i > 0; i--) {
		while (!s.empty() and a[s.top()] <= a[i]) {
			if (a[s.top()] < a[i]) {
				update (1, 1, n, s.top(), a[i] + a[s.top()]);
			}
			s.pop();
		}
		s.push(i);
		for (pii j : v[i]) {
			int ans = jogap (1, 1, n, i, j.ff);
			if (ans > k[j.ss]) {
				jog[j.ss] = 0;;
			}
			else jog[j.ss] = 1;
		}
	}
	for (int i = 1; i <= q; ++i) {
		cout << jog[i] << "\n";
	}
	return 0;
}
#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...