Submission #397077

# Submission time Handle Problem Language Result Execution time Memory
397077 2021-05-01T09:49:43 Z SavicS Triple Jump (JOI19_jumps) C++14
100 / 100
1638 ms 111712 KB
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 500005;
const int inf = 1e9 + 5;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k)  print the k-th smallest number in os(0-based)

int n, q;
ll niz[maxn];

int L[maxn];
int R[maxn];

vector<pii> upit[maxn];
vector<pii> ja[maxn];

int res[maxn];

ll bor[4 * maxn][2]; // 0 - vrednost, 1 - vrednost + koliko_sam_dodao
ll lenj[4 * maxn];
void build(int v, int tl, int tr){
	bor[v][1] = -inf;
	if(tl == tr){
		bor[v][0] = niz[tl];
		return;
	}
	int mid = (tl + tr) / 2;
	build(v * 2, tl, mid);
	build(v * 2 + 1, mid + 1, tr);
	bor[v][0] = max(bor[v * 2][0], bor[v * 2 + 1][0]);
}

void propagate(int v, int tl, int tr){
	if(lenj[v]){
		bor[v][1] = max(bor[v][1], bor[v][0] + lenj[v]);
		if(tl != tr){
			lenj[v * 2] = max(lenj[v * 2], lenj[v]);
			lenj[v * 2 + 1] = max(lenj[v * 2 + 1], lenj[v]); 
		}
		lenj[v] = 0;
	}
}

void lazyupd(int v, int tl, int tr, int l, int r, ll val){
	propagate(v, tl, tr);
	if(tl > tr || l > tr || tl > r)return;
	if(tl >= l && tr <= r){
		bor[v][1] = max(bor[v][1], bor[v][0] + val);
		if(tl != tr){
			lenj[v * 2] = max(lenj[v * 2], val);
			lenj[v * 2 + 1] = max(lenj[v * 2 + 1], val);
		}
		return;
	}
	int mid = (tl + tr) / 2;
	lazyupd(v * 2, tl, mid, l, r, val);
	lazyupd(v * 2 + 1, mid + 1, tr, l, r, val);
	bor[v][1] = max(bor[v * 2][1], bor[v * 2 + 1][1]);
}

ll kveri(int v, int tl, int tr, int l, int r){
	propagate(v, tl, tr);
	if(l > tr || tl > r)return 0;
	if(tl >= l && tr <= r)return bor[v][1];
	int mid = (tl + tr) / 2;
	return max(kveri(v * 2, tl, mid, l, r), kveri(v * 2 + 1, mid + 1, tr, l, r));
}


void add(int X, int Y){
	if(X != 0 && Y != 0 && 2 * Y - X <= n){
		ja[X].pb({2 * Y - X, niz[X] + niz[Y]});
	}
}

int main()
{
   	ios::sync_with_stdio(false);
   	cout.tie(nullptr);
  	cin.tie(nullptr);
	cin >> n;
	ff(i,1,n)cin >> niz[i];
	
	stack<int> stek;
	fb(i,n,1){
		while(!stek.empty() && niz[stek.top()] <= niz[i]){
			L[stek.top()] = i;
			stek.pop();
		}
		stek.push(i);		
	}
	
	while(!stek.empty()){
		stek.pop();
	}
	
	ff(i,1,n){
		while(!stek.empty() && niz[stek.top()] <= niz[i]){
			R[stek.top()] = i;
			stek.pop();
		}
		stek.push(i);
	}
	
	// (i, i + 1), (i, R[i]), (L[i], i) - to su parovi za A i B
			
	ff(i,1,n){
		add(i, i + 1);
		add(i, R[i]);
		add(L[i], i);
	}
	
	cin >> q;
	ff(i,1,q){
		int l, r;
		cin >> l >> r;
		upit[l].pb({r, i});
	}
	
	build(1,1,n);
	fb(i,n,1){
		for(auto c : ja[i]){
			lazyupd(1,1,n,c.fi,n,c.se);
		}
		
		// ako hocu da i bude C, za i vazi da mogu da uzmem bilo koju vrednost levo
		
		for(auto c : upit[i]){
			int r = c.fi;
			int id = c.se;
			res[id] = kveri(1,1,n,i,r);
		}
		
	}	
	
	ff(i,1,q)cout << res[i] << '\n';
		
   	return 0;
}
/**

5
5 2 1 5 3
3
1 4
2 5
1 5

// probati bojenje sahovski ili slicno

**/


# Verdict Execution time Memory Grader output
1 Correct 15 ms 23756 KB Output is correct
2 Correct 15 ms 23832 KB Output is correct
3 Correct 15 ms 23756 KB Output is correct
4 Correct 15 ms 23816 KB Output is correct
5 Correct 15 ms 23756 KB Output is correct
6 Correct 15 ms 23756 KB Output is correct
7 Correct 15 ms 23820 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 14 ms 23756 KB Output is correct
10 Correct 14 ms 23848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23756 KB Output is correct
2 Correct 15 ms 23832 KB Output is correct
3 Correct 15 ms 23756 KB Output is correct
4 Correct 15 ms 23816 KB Output is correct
5 Correct 15 ms 23756 KB Output is correct
6 Correct 15 ms 23756 KB Output is correct
7 Correct 15 ms 23820 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 14 ms 23756 KB Output is correct
10 Correct 14 ms 23848 KB Output is correct
11 Correct 370 ms 43324 KB Output is correct
12 Correct 368 ms 42916 KB Output is correct
13 Correct 371 ms 43068 KB Output is correct
14 Correct 362 ms 43260 KB Output is correct
15 Correct 359 ms 43092 KB Output is correct
16 Correct 366 ms 42416 KB Output is correct
17 Correct 363 ms 42436 KB Output is correct
18 Correct 362 ms 42324 KB Output is correct
19 Correct 361 ms 42972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 288 ms 49700 KB Output is correct
2 Correct 203 ms 46984 KB Output is correct
3 Correct 204 ms 47720 KB Output is correct
4 Correct 288 ms 49832 KB Output is correct
5 Correct 288 ms 49600 KB Output is correct
6 Correct 288 ms 49036 KB Output is correct
7 Correct 286 ms 48964 KB Output is correct
8 Correct 283 ms 48948 KB Output is correct
9 Correct 285 ms 49208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23756 KB Output is correct
2 Correct 15 ms 23832 KB Output is correct
3 Correct 15 ms 23756 KB Output is correct
4 Correct 15 ms 23816 KB Output is correct
5 Correct 15 ms 23756 KB Output is correct
6 Correct 15 ms 23756 KB Output is correct
7 Correct 15 ms 23820 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 14 ms 23756 KB Output is correct
10 Correct 14 ms 23848 KB Output is correct
11 Correct 370 ms 43324 KB Output is correct
12 Correct 368 ms 42916 KB Output is correct
13 Correct 371 ms 43068 KB Output is correct
14 Correct 362 ms 43260 KB Output is correct
15 Correct 359 ms 43092 KB Output is correct
16 Correct 366 ms 42416 KB Output is correct
17 Correct 363 ms 42436 KB Output is correct
18 Correct 362 ms 42324 KB Output is correct
19 Correct 361 ms 42972 KB Output is correct
20 Correct 288 ms 49700 KB Output is correct
21 Correct 203 ms 46984 KB Output is correct
22 Correct 204 ms 47720 KB Output is correct
23 Correct 288 ms 49832 KB Output is correct
24 Correct 288 ms 49600 KB Output is correct
25 Correct 288 ms 49036 KB Output is correct
26 Correct 286 ms 48964 KB Output is correct
27 Correct 283 ms 48948 KB Output is correct
28 Correct 285 ms 49208 KB Output is correct
29 Correct 1613 ms 105964 KB Output is correct
30 Correct 1377 ms 99684 KB Output is correct
31 Correct 1363 ms 101680 KB Output is correct
32 Correct 1603 ms 106052 KB Output is correct
33 Correct 1638 ms 106104 KB Output is correct
34 Correct 1625 ms 103580 KB Output is correct
35 Correct 1605 ms 103520 KB Output is correct
36 Correct 1618 ms 103392 KB Output is correct
37 Correct 1606 ms 104788 KB Output is correct
38 Correct 1194 ms 111592 KB Output is correct
39 Correct 1207 ms 111712 KB Output is correct
40 Correct 1178 ms 108216 KB Output is correct
41 Correct 1170 ms 107804 KB Output is correct
42 Correct 1171 ms 107872 KB Output is correct
43 Correct 1182 ms 109472 KB Output is correct
44 Correct 1266 ms 110852 KB Output is correct
45 Correct 1259 ms 110992 KB Output is correct
46 Correct 1232 ms 107840 KB Output is correct
47 Correct 1250 ms 107436 KB Output is correct
48 Correct 1312 ms 107360 KB Output is correct
49 Correct 1249 ms 109408 KB Output is correct
50 Correct 1358 ms 109140 KB Output is correct
51 Correct 1358 ms 109112 KB Output is correct
52 Correct 1354 ms 106620 KB Output is correct
53 Correct 1342 ms 106288 KB Output is correct
54 Correct 1361 ms 106400 KB Output is correct
55 Correct 1358 ms 107964 KB Output is correct