제출 #946394

#제출 시각아이디문제언어결과실행 시간메모리
946394siewjh3단 점프 (JOI19_jumps)C++17
100 / 100
693 ms117952 KiB
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500005;
const int inf = 1e9;
int vec[MAXN];
struct node{
	int s, e, m, ab, c, abc, lazy;
	node *l, *r;
	node (int _s, int _e){
		s = _s, e = _e, m = (s + e) >> 1, ab = 0, abc = 0, lazy = 0;
		if (s == e) c = vec[s];
		else{
			l = new node(s, m);
			r = new node(m + 1, e);
			c = max(l->c, r->c);
		}
	}
	void push(int v){
		if (v <= ab) return;
		ab = v; abc = max(abc, ab + c);
		lazy = v;
	}
	void propo(){
		if (s == e) return;
		if (lazy != 0){
			l->push(lazy); r->push(lazy);
			lazy = 0;
		}
	}
	void update(int x, int y, int v){
		if (x > y) return;
		if (x == s && y == e){
			push(v); return;
		}
		propo();
		if (y <= m) l->update(x, y, v);
		else if (x > m) r->update(x, y, v);
		else{
			l->update(x, m, v); r->update(m + 1, y, v);
		}
		propo();
		abc = max(l->abc, r->abc);
	}
	int query(int x, int y){
		if (x == s && y == e) return abc;
		propo();
		if (y <= m) return l->query(x, y);
		else if (x > m) return r->query(x, y);
		else return max(l->query(x, m), r->query(m + 1, y));
	}
};
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int nums; cin >> nums;
	vector<int> st;
	vector<vector<int>> abp(nums + 1);
	for (int i = 1; i <= nums; i++){
		cin >> vec[i];
		while (!st.empty() && vec[st.back()] < vec[i]){
			abp[st.back()].push_back(i);
			st.pop_back();
		}
		if (!st.empty()){
			abp[st.back()].push_back(i);
			if (vec[st.back()] == vec[i]) st.pop_back();
		}
		st.push_back(i);
	}
	int queries; cin >> queries;
	vector<tuple<int, int, int>> qvec(queries);
	vector<int> ans(queries);
	for (int q = 0; q < queries; q++){
		int l, r; cin >> l >> r;
		qvec[q] = {l, r, q};
	}
	sort(qvec.begin(), qvec.end(), greater<tuple<int, int, int>>());
	node *root = new node(1, nums);
	int ca = nums + 1;
	for (auto &[l, r, q] : qvec){
		while (ca > l){
			ca--;
			for (int b : abp[ca]) root->update(2 * b - ca, nums, vec[ca] + vec[b]);
		}
		ans[q] = root->query(l, r);
	}
	for (int x : ans) cout << x << '\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...