Submission #385686

# Submission time Handle Problem Language Result Execution time Memory
385686 2021-04-04T17:56:01 Z ritul_kr_singh Global Warming (CEOI18_glo) C++17
100 / 100
269 ms 17712 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define nl << '\n'
#define sp << ' ' <<

struct SegmentTree{
	vector<int> a; int sz = 1, ID = -1e18;
	SegmentTree(vector<int> &v){
		while(sz < (int)v.size()) sz += sz;
		a.resize(2*sz, ID);
		build(v, 0, 0, sz);
	}
	void build(vector<int> &v, int x, int lx, int rx){
		if(rx-lx==1){
			if(lx<(int)v.size()) a[x] = v[lx];
			return;
		}
		int mx = (lx+rx)/2;
		build(v, 2*x+1, lx, mx);
		build(v, 2*x+2, mx, rx);
		a[x] = max(a[2*x+1], a[2*x+2]);
	}
	void update(int i, int val, int x, int lx, int rx){
		if(rx-lx==1) return void(a[x] = val);
		int mx = (lx+rx)/2;
		if(i<mx) update(i, val, 2*x+1, lx, mx);
		else update(i, val, 2*x+2, mx, rx);
		a[x] = max(a[2*x+1], a[2*x+2]);
	}
	void update(int i, int val){ update(i, val, 0, 0, sz); }
	int get(int l, int r, int x, int lx, int rx){
		if(r<=lx or rx<=l) return ID;
		if(l<=lx and rx<=r) return a[x];
		int mx = (lx+rx)/2;
		return max(get(l, r, 2*x+1, lx, mx), get(l, r, 2*x+2, mx, rx));
	}
	int get(int l, int r){ return get(l, r+1, 0, 0, sz); }
};

void lis(int n, vector<int> &a, vector<int> &res){
	vector<int> v = {a[0]};
	res[0] = 1;
	for(int i=1; i<n; ++i){
		if(v.back() < a[i]) v.push_back(a[i]), res[i] = v.size();
		else{
			auto f = lower_bound(v.begin(), v.end(), a[i]);
			res[i] = f - v.begin() + 1;
			*f = a[i];
		}
	}
}

signed main(){
	cin.tie(0)->sync_with_stdio(0);
	int n, x; cin >> n >> x;
	vector<int> a(n), pre(n), suf(n);
	for(int &i : a) cin >> i;

	lis(n, a, pre);
	reverse(a.begin(), a.end()); for(int &i : a) i = -i;
	lis(n, a, suf);
	reverse(a.begin(), a.end()); for(int &i : a) i = -i;
	reverse(suf.begin(), suf.end());

	// for(int i : a) cout << i << ' ';
	// cout nl;
	// for(int i : pre) cout << i << ' ';
	// cout nl;
	// for(int i : suf) cout << i << ' ';
	// cout nl;

	vector<array<int, 2>> b(n);
	vector<int> pos(n), suf1(n);
	for(int i=0; i<n; ++i) b[i] = {a[i], i};
	sort(b.begin(), b.end());
	for(int i=0; i<n; ++i) pos[b[i][1]] = i, suf1[i] = suf[b[i][1]];

	SegmentTree st(suf1);
	int ans = 1;
	
	for(int i=0; i<n; ++i){
		st.update(pos[i], -1e18);
		array<int, 2> search = {a[i]-x, (int)1e18};
		int l = upper_bound(b.begin(), b.end(), search) - b.begin();
		if(l<n){
			ans = max(ans, pre[i] + st.get(l, n-1));
		}
	}
	cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 380 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 2 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 380 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 2 ms 364 KB Output is correct
19 Correct 2 ms 364 KB Output is correct
20 Correct 2 ms 364 KB Output is correct
21 Correct 2 ms 364 KB Output is correct
22 Correct 2 ms 364 KB Output is correct
23 Correct 2 ms 364 KB Output is correct
24 Correct 2 ms 364 KB Output is correct
25 Correct 2 ms 364 KB Output is correct
26 Correct 2 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 269 ms 15596 KB Output is correct
2 Correct 265 ms 17516 KB Output is correct
3 Correct 259 ms 17388 KB Output is correct
4 Correct 246 ms 17388 KB Output is correct
5 Correct 163 ms 16764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 4076 KB Output is correct
2 Correct 52 ms 4716 KB Output is correct
3 Correct 52 ms 4588 KB Output is correct
4 Correct 34 ms 4584 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 32 ms 4584 KB Output is correct
7 Correct 52 ms 4668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 7884 KB Output is correct
2 Correct 87 ms 8960 KB Output is correct
3 Correct 165 ms 17388 KB Output is correct
4 Correct 129 ms 16840 KB Output is correct
5 Correct 59 ms 8540 KB Output is correct
6 Correct 104 ms 16260 KB Output is correct
7 Correct 106 ms 17172 KB Output is correct
8 Correct 73 ms 8940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 380 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 2 ms 364 KB Output is correct
19 Correct 2 ms 364 KB Output is correct
20 Correct 2 ms 364 KB Output is correct
21 Correct 2 ms 364 KB Output is correct
22 Correct 2 ms 364 KB Output is correct
23 Correct 2 ms 364 KB Output is correct
24 Correct 2 ms 364 KB Output is correct
25 Correct 2 ms 364 KB Output is correct
26 Correct 2 ms 364 KB Output is correct
27 Correct 269 ms 15596 KB Output is correct
28 Correct 265 ms 17516 KB Output is correct
29 Correct 259 ms 17388 KB Output is correct
30 Correct 246 ms 17388 KB Output is correct
31 Correct 163 ms 16764 KB Output is correct
32 Correct 54 ms 4076 KB Output is correct
33 Correct 52 ms 4716 KB Output is correct
34 Correct 52 ms 4588 KB Output is correct
35 Correct 34 ms 4584 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 32 ms 4584 KB Output is correct
38 Correct 52 ms 4668 KB Output is correct
39 Correct 77 ms 7884 KB Output is correct
40 Correct 87 ms 8960 KB Output is correct
41 Correct 165 ms 17388 KB Output is correct
42 Correct 129 ms 16840 KB Output is correct
43 Correct 59 ms 8540 KB Output is correct
44 Correct 104 ms 16260 KB Output is correct
45 Correct 106 ms 17172 KB Output is correct
46 Correct 73 ms 8940 KB Output is correct
47 Correct 130 ms 8812 KB Output is correct
48 Correct 124 ms 8940 KB Output is correct
49 Correct 246 ms 17388 KB Output is correct
50 Correct 140 ms 16844 KB Output is correct
51 Correct 105 ms 13644 KB Output is correct
52 Correct 148 ms 16840 KB Output is correct
53 Correct 104 ms 16844 KB Output is correct
54 Correct 119 ms 17712 KB Output is correct
55 Correct 210 ms 17664 KB Output is correct