제출 #1335893

#제출 시각아이디문제언어결과실행 시간메모리
1335893YSH2020Hedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++20
0 / 100
2050 ms182368 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

//smth like seggs
struct node {
	int s,e;
	int m;
	node *l, *r;
	int val=0;
	int lazy=0;
	//range increment range max
	node (int _s, int _e) {
		s = _s; e = _e;
		m = (s+e)/2;
		if (s!=e) {
			l = new node(s,m);
			r = new node(m+1,e);
		}
	}
	void upd(int x, int y, int v) {
		if (x<=s and y>=e) {lazy+=v; val += v; return;}
		prop();
		if (y<=m) l->upd(x,y,v);
		else if (x>=m+1) r->upd(x,y,v);
		else l->upd(x,m,v), r->upd(m+1,y,v);
		val = max(l->val, r->val);
	}
	int qry(int x, int y) {
		if (x<=s and y>=e) return val;
		prop();
		if (y<=m) return l->qry(x,y);
		else if (x>=m+1) return r->qry(x,y);
		else return max(l->qry(x,m), r->qry(m+1,y));
	}
	void prop() {
		l->val += lazy;
		l->lazy += lazy;
		r->val += lazy;
		r->lazy += lazy;
		lazy=0;
	}
}*root;

struct qn {
	int l, r, k, idx;
};
#define space ' '
#define endl '\n'
bool comp(qn a, qn b) {
	return a.l > b.l;
}

signed main() {
	int n,m; cin >> n >> m;
	root = new node(0,n-1);
	int a[n]; for (int i = 0; i < n; i++) cin >> a[i];
	int ans[m];
	vector<qn> in(m);
	for (int i = 0; i < m; i++) {
		int a,b,c; cin >> a >> b >> c; a--;b--;
		in[i] = {a,b,c,i};
	}
	int updated[n]; memset(updated,0,sizeof(updated));
	sort(in.begin(), in.end(), comp);
	stack<pair<int,int>> mono; //key, starting index
	int idx = n-1;
	for (int i = 0; i < m; i++) {
		while (idx >= in[i].l) {
			//from idx
			//idea is that once you get removed, you point update yourself
			int pos = idx+1;
			while (mono.size() && mono.top().first < a[idx]) {
				if (updated[mono.top().second] == 0) {
					updated[mono.top().second] = 1;
					root->upd(mono.top().second, mono.top().second, 2*mono.top().first);
				}
				root->upd(pos,mono.top().second, a[idx]-mono.top().first);
				pos = mono.top().second+1;
				mono.pop();
			}
			mono.push({a[idx], pos-1});
			idx--;
			//for (int j = 0; j < n; j++) cout << root->qry(j,j) << space;
			//cout << endl;
		}
		if (root->qry(in[i].l, in[i].r)>in[i].k) ans[in[i].idx]=0;
		else ans[in[i].idx]=1;
	}
	for (int i = 0; i < m; i++) cout << ans[i] << '\n';
}
//may have bad constant :(
#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...