제출 #950420

#제출 시각아이디문제언어결과실행 시간메모리
950420vjudge1Triple Jump (JOI19_jumps)C++17
100 / 100
856 ms139356 KiB
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")

#include<bits/stdc++.h>	

using namespace std;

#define all(a) a.begin(), a.end()                                                   
#define rall(a) a.rbegin(), a.rend()                 
#define sz(a) (int)a.size()
#define s second
#define f first
 
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
     
vector<pii> rid = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
vector<pii> dir = {{-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
 
const int N = 5e5 + 1, mod = 1e9 + 7;

const ll inf = 1e9;
 
double eps = 1e-15;
                                                
bool flg = 0;

array<int, 4> t[4 * N]; 
int add[4 * N], a[N], n;

void push(int u, int tl, int tr) {
	if (!add[u]) return;
	t[u][0] = t[u][3] = add[u];
	t[u][2] = t[u][1] + t[u][0];
	if (tl < tr) {
		add[u * 2] = add[u];
		add[u * 2 + 1] = add[u];
	}
	add[u] = 0;
}

void bld(int u = 1, int tl = 1, int tr = n) {
    if (tl == tr) {
		t[u][1] = a[tl];
		return;
    }
    int m = (tl + tr) / 2;
    bld(u * 2, tl, m);
    bld(u * 2 + 1, m + 1, tr);
    t[u][1] = max(t[u * 2][1], t[u * 2 + 1][1]);
}	

void upt(int l, int r, int v, int u = 1, int tl = 1, int tr = n) {
    push(u, tl, tr);
    if (tl > r || tr < l || t[u][3] >= v) return;
    if (tl >= l && tr <= r && t[u][0] <= v) {
    	add[u] = v;
    	push(u, tl, tr);
    	return;
    }
    int m = (tl + tr) / 2;
    upt(l, r, v, u * 2, tl, m);
    upt(l, r, v, u * 2 + 1, m + 1, tr);
	t[u][0] = max(t[u * 2][0], t[u * 2 + 1][0]);
	t[u][3] = min(t[u * 2][3], t[u * 2 + 1][3]);
	t[u][2] = max(t[u * 2][2], t[u * 2 + 1][2]);
}

int get(int l, int r, int u = 1, int tl = 1, int tr = n) {
	push(u, tl, tr);
	if (tl > r || tr < l) return 0;
	if (tl >= l && tr <= r) return t[u][2];
	int m = (tl + tr) / 2;
	return max(get(l, r, u * 2, tl, m), get(l, r, u * 2 + 1, m + 1, tr));
}	

void slv() {
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	bld();
	int q;
	cin >> q;
	vector<pii> qr[n + 1];
	for (int i = 1; i <= q; i++) {
		int l, r;
		cin >> l >> r;
		qr[l].push_back({r, i});
	}
	vector<pii> h[4 * n];
	stack<int> s;
	for (int i = n; i >= 1; i--) {
		while (sz(s) && a[s.top()] < a[i]) s.pop();
		if (sz(s)) {
		    int x = s.top() - i;
		    if (s.top() + x <= n) {
		    	h[i].push_back({s.top() + x, a[i] + a[s.top()]});
		    }
		}
		s.push(i);	                      	
	}
	while (sz(s)) s.pop();
	for (int i = 1; i <= n; i++) {
	 	while (sz(s) && a[s.top()] < a[i]) s.pop();
	 	if (sz(s)) {
	 		int x = i - s.top();
	 		if (i + x <= n) {
	 			h[s.top()].push_back({i + x, a[i] + a[s.top()]});	
	 		}
	 	}
	 	s.push(i); 
	}
	int ans[q + 1];
	for (int i = n; i >= 1; i--) {
		for (auto [r, v]: h[i]) upt(r, n, v);
		for (auto [r, id]: qr[i]) ans[id] = get(i + 2, r);	
	}	
	cout << '\n';
	for (int i = 1; i <= q; i++) cout << ans[i] << ' ';
}                                                                           
 
main() {
	//freopen("rsq.in", "r", stdin);                                                                                     
	//freopen("rsq.out", "w", stdout);                                                                                     
	ios_base::sync_with_stdio(0);	                                                                                       
	cin.tie(0);
	int tp = 1;
	if (flg) cin >> tp;
	while (tp--) {  	
		slv();
	}
}
//wenomechainsama                                              

컴파일 시 표준 에러 (stderr) 메시지

jumps.cpp:124:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  124 | 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...