Submission #941549

#TimeUsernameProblemLanguageResultExecution timeMemory
941549LCJLYTriple Jump (JOI19_jumps)C++14
19 / 100
4008 ms26960 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long 
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<int,int>pii;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
typedef pair<int,pii>pi2;

inline int combine(int a, int b){
	return max(a,b);
}

struct node{
	int s,e,m;
	node *l,*r;
	int v;
	
	node(int ss, int ee):s(ss),e(ee),m((s+e)>>1),v(0){
		if(s!=e){
			l=new node(s,m);
			r=new node(m+1,e);
		}
	}
	
	void upd(int x, int y){
		if(s==e){
			v=max(v,y);
			return;
		}
		if(x<=m)l->upd(x,y);
		else r->upd(x,y);
		v=combine(l->v,r->v);
	}
	
	int query(int x, int y){
		if(x<=s&&y>=e) return v;
		if(y<=m) return l->query(x,y);
		else if(x>m) return r->query(x,y);
		return combine(l->query(x,m),r->query(m+1,y));
	}
};

void solve(){
	int n;
	cin >> n;
	int arr[n+1];
	for(int x=1;x<=n;x++){
		cin >> arr[x];
	}
	
	int q;
	cin >> q;
	pair<pii,int>que[q];
	for(int x=0;x<q;x++){
		cin >> que[x].first.first >> que[x].first.second;
		que[x].second=x;
	}
	
	int ans[q];
	sort(que,que+q);
	reverse(que,que+q);
	
	node st(0,n+5);
	int ptr=n-2;
	
	for(int x=0;x<q;x++){
		int l=que[x].first.first;
		int r=que[x].first.second;
		while(l<=ptr){
			int add=0;
			int ptr2=ptr+1;
			for(int y=ptr+2;y<=n;y++){
				if(y%2==ptr%2){
					add=max(add,arr[ptr2]);
					ptr2++;
				}
				st.upd(y,arr[y]+add+arr[ptr]);
			}
			ptr--;
		}
		ans[que[x].second]=st.query(0,r);
	}
	
	for(int x=0;x<q;x++) cout << ans[x] << "\n";
}	

int32_t main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//cin >> t;
	while(t--){
		solve();
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...