Submission #943592

#TimeUsernameProblemLanguageResultExecution timeMemory
943592penguin133Triple Jump (JOI19_jumps)C++17
100 / 100
918 ms160340 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, A[500005], q, ans[500050];
vector <pi> Q[500005];
struct node{
	int s, e, m, val, val2, lz;
	node *l, *r;
	node(int _s, int _e){
		s = _s, e = _e, m = (s + e) >> 1;
		if(s != e)l = new node(s, m), r = new node(m+1, e), val = max(l->val, r->val);
		else val = A[s];
		val2 = lz = 0;
	}
	void prop(){
		if(lz){
			val2 = max(val2, val + lz);
			if(s != e)l->lz = max(l->lz, lz), r->lz = max(r->lz, lz);
			lz = 0;
		}
	}
	void upd(int a, int b, int c){
		prop();
		if(a == s && b== e)lz = max(lz, c);
		else{
			if(b <= m)l->upd(a, b, c);
			else if(a > m)r->upd(a, b, c);
			else l->upd(a, m, c), r->upd(m+1, b, c);
			l->prop(), r->prop();
			val2 = max(l->val2, r->val2);
		}
	}
	
	int qry(int a, int b){
		prop();
		if(a == s && b == e)return val2;
		if(b <= m)return l->qry(a, b);
		if(a > m)return r->qry(a, b);
		return max(l->qry(a, m), r->qry(m+1, b));
	}
}*root;

vector <int> stuf[500005];

void solve(){
	cin >> n;
	for(int i = 1; i <= n; i++)cin >> A[i];
	root = new node(1, n);
	cin >> q;
	for(int i = 1; i <= q; i++){
		int l, r; cin >> l >> r;
		Q[l].push_back({r, i});
	}
	stack <int> s;
	for(int i = 1; i <= n; i++){
		while(!s.empty() && A[s.top()] < A[i])s.pop();
		if(!s.empty())stuf[s.top()].push_back(i);
		s.push(i);
	}
	while(!s.empty())s.pop();
	for(int i = n; i >= 1; i--){
		while(!s.empty() && A[s.top()] < A[i])s.pop();
		if(!s.empty())stuf[i].push_back(s.top());
		s.push(i);
	}
	for(int i = n; i >= 1; i--){
		for(auto j : stuf[i]){
			if(j * 2 - i <= n)root->upd(j * 2 - i, n, A[i] + A[j]);
		}
		for(auto [a, b] : Q[i])ans[b] = root->qry(i, a);
	}
	for(int i = 1; i <= q; i++)cout << ans[i] << '\n';
}

main(){
	ios::sync_with_stdio(0);cin.tie(0);
	int tc = 1;
	//cin >> tc;
	for(int tc1=1;tc1<=tc;tc1++){
		// cout << "Case #" << tc1 << ": ";
		solve();
	}
}

Compilation message (stderr)

jumps.cpp:84:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   84 | main(){
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...