제출 #1335922

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

#define int long long
mt19937_64 rng(1);
//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;
}

vector<int> solve(int n, int m, int a[], vector<pair<pair<int,int>,int>> qns){
	root = new node(0,n-1);
	vector<int> ans(m);
	vector<qn> in(m);
	for (int i = 0; i < m; i++) {
		int a=qns[i].first.first,b=qns[i].first.second,c=qns[i].second;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[pos] == 0) {
					updated[pos] = 1;
					root->upd(pos,pos, 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;
	}
	return ans;
}
vector<int> solve2(int n, int q, int s[], vector<pair<pair<int,int>,int>> test) {
	vector<int> aaaa(q);
	for (int tc = 0; tc < q; tc++) {
		int a=test[tc].first.first, b=test[tc].first.second, c=test[tc].second;
		a--;b--;
		int prefmax=-2e9;
		int ans=0;
		for (int i = a; i <= b; i++) {
			if (prefmax > s[i]) {ans=max(ans, prefmax+s[i]);}
			else prefmax = s[i];
		}
		if (ans > c) aaaa[tc]=0;
		else aaaa[tc]=1;
	}
	return aaaa;
}

signed main() {
	//while (true){
	int n; cin >> n;
	int q; cin >> q;
	int a[n]; for (int i = 0; i < n; i++) cin >> a[i];
	vector<pair<pair<int,int>,int>> qns(q);
	for (int i = 0; i < q; i++) {
		pair<int,int> pos; cin >> pos.first >> pos.second;
		if (pos.second < pos.first) swap(pos.first, pos.second);
		int v; cin >> v;
		qns[i]={{pos}, v};
	}
	for (auto i:solve(n,q,a,qns)) cout << i << endl;
	//vector<int> sol1 = solve(n,q,a,qns), sol2 = solve2(n,q,a,qns);
	//for (int i = 0; i < q; i++) {
	//	if (sol1[i] != sol2[i]) {
	//		for (int j = 0; j < n; j++) cout << a[j] << space;
	//		cout << endl;
	//		cout << qns[i].first.first << space << qns[i].first.second << space << qns[i].second << endl;
	//		cout << sol1[i] << space << sol2[i];
	//		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...