답안 #489620

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
489620 2021-11-23T12:24:46 Z fhvirus 3단 점프 (JOI19_jumps) C++17
100 / 100
1131 ms 85616 KB
// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll;
#define ff first
#define ss second
#define pb emplace_back
#define AI(x) begin(x),end(x)
template<class I>bool chmax(I&a,I b){return a<b?(a=b,true):false;}
template<class I>bool chmin(I&a,I b){return b<a?(a=b,true):false;} 
#ifdef OWO
#define debug(args...) SDF(#args, args)
#define OIU(args...) ostream& operator<<(ostream&O,args)
#define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
#else
#pragma GCC optimize("Ofast")
#define debug(...) ((void)0)
#endif

const int kN = 500005;
int N, A[kN];
int Q, L[kN], R[kN];

void input(){
	cin >> N;
	for(int i = 1; i <= N; ++i)
		cin >> A[i];
	cin >> Q;
	for(int i = 1; i <= Q; ++i)
		cin >> L[i] >> R[i];
}

int suf[kN];
vector<int> candidate[kN];
vector<int> querys[kN];
int ans[kN];

struct SGT {
	// val1: raw A
	// val2: A + (the modified max)
	int n;
	vector<int> val1, val2, tag;
	[[ gnu::pure ]] int id(int l, int r){ return (l+r) | (l!=r); }
	void build(int l, int r, int *A){
		int i = id(l, r);
		if(l == r){
			val1[i] = A[l];
			return;
		}
		int m = (l+r) / 2;
		build(l, m, A);
		build(m+1, r, A);
		val1[i] = max(val1[id(l, m)], val1[id(m+1, r)]);
	}
	SGT(int nn, int *A):
		n(nn), val1(nn*2+2), val2(nn*2+2, 0), tag(nn*2+2, 0){ build(1, n, A); }
	void upd(int i, int v){
		tag[i] = max(tag[i], v);
		val2[i] = max(val2[i], val1[i] + tag[i]);
	}
	void modify(int l, int r, int ql, int qr, int v){
		int i = id(l, r);
		if(ql <= l and r <= qr){
			upd(i, v);
			return;
		}
		int m = (l+r) / 2;
		upd(id(l, m), tag[i]);
		upd(id(m+1, r), tag[i]);
		tag[i] = 0;
		if(ql <= m) modify(l, m, ql, qr, v);
		if(m < qr) modify(m+1, r, ql, qr, v);
		val2[i] = max(val2[i], val2[id(l, m)]);
		val2[i] = max(val2[i], val2[id(m+1, r)]);
		return;
	}
	int query(int l, int r, int ql, int qr){
		int i = id(l, r);
		if(ql <= l and r <= qr) return val2[i];
		int m = (l+r) / 2;
		upd(id(l, m), tag[i]);
		upd(id(m+1, r), tag[i]);
		tag[i] = 0;
		return max(
			ql <= m ? query(l, m, ql, qr) : 0,
			m < qr ? query(m+1, r, ql, qr) : 0
		);
	}
	void modify(int ql, int qr, int v){ modify(1, n, ql, qr, v); }
	int query(int ql, int qr){ return query(1, n, ql, qr); }
};

void solve(){
	for(int i = N; i >= 1; --i)
		suf[i] = max(suf[i+1], A[i]);

	// make pairs
	vector<int> stk;
	for(int i = 1; i <= N; ++i){
		while(!stk.empty()){
			int j = end(stk)[-1];
			candidate[j].pb(i);
			if(A[j] > A[i]) break;
			stk.pop_back();
		}
		stk.pb(i);
	}
	for(int i = 1; i <= Q; ++i)
		querys[L[i]].pb(i);

	SGT sgt(N, A);
	for(int i = N; i >= 1; --i){
		for(int b: candidate[i])
			if(b+(b-i) <= N)
				sgt.modify(b+(b-i), N, A[i] + A[b]);
		for(int q: querys[i])
			ans[q] = sgt.query(L[q]+2, R[q]);
	}
}

void ouput(){
	for(int i = 1; i <= Q; ++i)
		cout << ans[i] << '\n';
}

signed main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	input();
	solve();
	ouput();
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB Output is correct
2 Correct 13 ms 23756 KB Output is correct
3 Correct 13 ms 23820 KB Output is correct
4 Correct 13 ms 23848 KB Output is correct
5 Correct 13 ms 23744 KB Output is correct
6 Correct 15 ms 23756 KB Output is correct
7 Correct 13 ms 23816 KB Output is correct
8 Correct 13 ms 23756 KB Output is correct
9 Correct 15 ms 23756 KB Output is correct
10 Correct 15 ms 23756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB Output is correct
2 Correct 13 ms 23756 KB Output is correct
3 Correct 13 ms 23820 KB Output is correct
4 Correct 13 ms 23848 KB Output is correct
5 Correct 13 ms 23744 KB Output is correct
6 Correct 15 ms 23756 KB Output is correct
7 Correct 13 ms 23816 KB Output is correct
8 Correct 13 ms 23756 KB Output is correct
9 Correct 15 ms 23756 KB Output is correct
10 Correct 15 ms 23756 KB Output is correct
11 Correct 325 ms 40012 KB Output is correct
12 Correct 305 ms 39736 KB Output is correct
13 Correct 287 ms 39868 KB Output is correct
14 Correct 282 ms 39812 KB Output is correct
15 Correct 371 ms 40004 KB Output is correct
16 Correct 280 ms 39236 KB Output is correct
17 Correct 376 ms 39124 KB Output is correct
18 Correct 316 ms 39112 KB Output is correct
19 Correct 307 ms 39680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 174 ms 37328 KB Output is correct
2 Correct 103 ms 37252 KB Output is correct
3 Correct 116 ms 38116 KB Output is correct
4 Correct 183 ms 37544 KB Output is correct
5 Correct 205 ms 37652 KB Output is correct
6 Correct 202 ms 37636 KB Output is correct
7 Correct 172 ms 37552 KB Output is correct
8 Correct 235 ms 37556 KB Output is correct
9 Correct 171 ms 37716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB Output is correct
2 Correct 13 ms 23756 KB Output is correct
3 Correct 13 ms 23820 KB Output is correct
4 Correct 13 ms 23848 KB Output is correct
5 Correct 13 ms 23744 KB Output is correct
6 Correct 15 ms 23756 KB Output is correct
7 Correct 13 ms 23816 KB Output is correct
8 Correct 13 ms 23756 KB Output is correct
9 Correct 15 ms 23756 KB Output is correct
10 Correct 15 ms 23756 KB Output is correct
11 Correct 325 ms 40012 KB Output is correct
12 Correct 305 ms 39736 KB Output is correct
13 Correct 287 ms 39868 KB Output is correct
14 Correct 282 ms 39812 KB Output is correct
15 Correct 371 ms 40004 KB Output is correct
16 Correct 280 ms 39236 KB Output is correct
17 Correct 376 ms 39124 KB Output is correct
18 Correct 316 ms 39112 KB Output is correct
19 Correct 307 ms 39680 KB Output is correct
20 Correct 174 ms 37328 KB Output is correct
21 Correct 103 ms 37252 KB Output is correct
22 Correct 116 ms 38116 KB Output is correct
23 Correct 183 ms 37544 KB Output is correct
24 Correct 205 ms 37652 KB Output is correct
25 Correct 202 ms 37636 KB Output is correct
26 Correct 172 ms 37552 KB Output is correct
27 Correct 235 ms 37556 KB Output is correct
28 Correct 171 ms 37716 KB Output is correct
29 Correct 1131 ms 79020 KB Output is correct
30 Correct 949 ms 78468 KB Output is correct
31 Correct 944 ms 80432 KB Output is correct
32 Correct 1081 ms 79020 KB Output is correct
33 Correct 1080 ms 79020 KB Output is correct
34 Correct 1123 ms 79060 KB Output is correct
35 Correct 1060 ms 79068 KB Output is correct
36 Correct 1099 ms 78972 KB Output is correct
37 Correct 1091 ms 78900 KB Output is correct
38 Correct 863 ms 85616 KB Output is correct
39 Correct 858 ms 85316 KB Output is correct
40 Correct 740 ms 85288 KB Output is correct
41 Correct 746 ms 85228 KB Output is correct
42 Correct 821 ms 85084 KB Output is correct
43 Correct 776 ms 84872 KB Output is correct
44 Correct 816 ms 84144 KB Output is correct
45 Correct 938 ms 84072 KB Output is correct
46 Correct 937 ms 83976 KB Output is correct
47 Correct 892 ms 83616 KB Output is correct
48 Correct 788 ms 83588 KB Output is correct
49 Correct 788 ms 83504 KB Output is correct
50 Correct 895 ms 80928 KB Output is correct
51 Correct 897 ms 80612 KB Output is correct
52 Correct 894 ms 80340 KB Output is correct
53 Correct 912 ms 80464 KB Output is correct
54 Correct 899 ms 79780 KB Output is correct
55 Correct 904 ms 79280 KB Output is correct