제출 #1165656

#제출 시각아이디문제언어결과실행 시간메모리
1165656yellowtoadHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++20
100 / 100
1990 ms237972 KiB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int n, q, a[1000010], maxx[4000010], cur;
vector<int> node[4000010];

int search(int id, int x) {
	int l = 0, r = node[id].size()-1;
	while (l <= r) {
		int mid = (l+r)/2;
		if (node[id][mid] >= x) r = mid-1;
		else l = mid+1;
	}
	if (r >= 0) return x+node[id][r];
	else return 0;
}

void build(int id, int x, int y) {
	if (x == y) {
		node[id].push_back(a[x]);
		return;
	}
	int mid = (x+y)/2;
	build(id*2,x,mid);
	build(id*2+1,mid+1,y);
	maxx[id] = max(maxx[id*2],maxx[id*2+1]);
	int l = 0, r = 0;
	while ((l < node[id*2].size()) || (r < node[id*2+1].size())) {
		if ((r == node[id*2+1].size()) || ((l < node[id*2].size()) && (node[id*2][l] < node[id*2+1][r]))) {
			node[id].push_back(node[id*2][l]);
			l++;
		} else {
			if (node[id*2].back() > node[id*2+1][r]) maxx[id] = max(maxx[id],node[id*2].back()+node[id*2+1][r]);
			node[id].push_back(node[id*2+1][r]);
			r++;
		}
	}
}

int query(int id, int x, int y, int l, int r) {
	if ((l <= x) && (y <= r)) {
		int tmp = search(id,cur);
		cur = max(cur,node[id].back());
		return max(tmp,maxx[id]);
	}
	if ((y < l) || (r < x)) return 0;
	int mid = (x+y)/2, tmp = query(id*2,x,mid,l,r), tnp = query(id*2+1,mid+1,y,l,r);
	return max(tmp,tnp);
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> q;
	for (int i = 1; i <= n; i++) cin >> a[i];
	build(1,1,n);
	while (q--) {
		int l, r, k;
		cin >> l >> r >> k;
		cur = 0;
		cout << (query(1,1,n,l,r) <= k) << "\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...