답안 #218953

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
218953 2020-04-03T09:51:08 Z mieszko11b 별자리 3 (JOI20_constellation3) C++14
100 / 100
1128 ms 81112 KB
#include <bits/stdc++.h>

using namespace std;

using ii = pair<int, int>;
using ll = long long;

#define X first
#define Y second

const int inf = int(1e9)+ 7;

struct Star {
	int x, y, c;
	
	Star(int x, int y, int c) {
		this->x = x;
		this->y = y;
		this->c = c;
	}
};

struct SegmentTree {
	int lv;
	vector<ll> d;
	
	SegmentTree(int n) {
		lv = 2;
		while(lv < n + 3)
			lv *= 2;
		d.resize(2 * lv + 2);
	}
	
	void insert(int a, int b, ll v) {
		if(b < a)
			return ;
			
		int va = a + lv;
		int vb = b + lv;
		d[va] += v;
		if(va != vb)
			d[vb] += v;
		while(va / 2 != vb / 2) {
			if(va % 2 == 0)
				d[va + 1] += v;
			if(vb % 2 == 1)
				d[vb - 1] += v;
			va /= 2;
			vb /= 2;
		}
	}
	
	ll query(int x) {
		int w = x + lv;
		ll res = 0;
		while(w) {
			res += d[w];
			w /= 2;
		}
		return res;
	}
};

int n, m;
int a[200007];
int pl[200007], pr[200007];
int maxr[200007], maxl[200007];
vector<Star> St[200007];

struct Tree {
	int jp[20][200007];
	vector<int> ch[200007];
	int ord[200007], max_ord[200007];
	SegmentTree *ST;
	int nxt_num;
	
	void dfs(int w) {
		ord[w] = nxt_num++;
		for(int u : ch[w])
			dfs(u);
		max_ord[w] = nxt_num - 1;
	}
	
	Tree(int *p) {
		for(int i = 1 ; i <= n ; i++) {
			jp[0][i] = p[i];
			ch[p[i]].push_back(i);
		}
		for(int j = 1 ; j <= 18 ; j++)
			for(int i = 1 ; i <= n ; i++)
				jp[j][i] = jp[j - 1][jp[j - 1][i]];
		ST = new SegmentTree(n);
		
		nxt_num = 0;
		dfs(0);
	}
	
	int first_atleast(int i, int h) {
		for(int j = 18 ; j >= 0 ; j--)
			if(a[jp[j][i]] < h)
				i = jp[j][i];
		i = jp[0][i];
		return i;
	}
	
	void insert(int i, ll v) {
		//~ cout << "ins " << i << " " << v << endl;
		ST->insert(ord[i], max_ord[i], v);
	}
	
	ll query(int a, int b) {
		if(ord[a] > ord[b])
			swap(a, b);
		//a przodkiem b
		//~ cout << "query " << a << " " << b << " = " << ST->query(ord[b]) - ST->query(a) << endl;
		return ST->query(ord[b]) - ST->query(ord[a]);
	}
};

Tree *LT, *RT;
ll sum_all;

int main() {
	scanf("%d", &n);
	for(int i = 1 ; i <= n ; i++)
		scanf("%d", &a[i]);
	a[0] = a[n + 1] = inf;
	
	stack<int> S;
	S.push(0);
	for(int i = 1 ; i <= n ; i++) {
		while(a[S.top()] <= a[i])
			S.pop();
		pl[i] = S.top();
		//~ maxr[S.top()] = i;
		S.push(i);
	}
	while(!S.empty())
		S.pop();
	S.push(0);
	for(int i = n ; i >= 1 ; i--) {
		while(a[S.top()] < a[i])
			S.pop();
		pr[i] = S.top();
		//~ maxl[S.top()] = i;
		S.push(i);
	}
	while(!S.empty())
		S.pop();
		
	S.push(0);
	for(int i = 1 ; i <= n ; i++) {
		while(a[S.top()] <= a[i])
			S.pop();
		//~ pl[i] = S.top();
		maxr[S.top()] = i;
		S.push(i);
	}
	while(!S.empty())
		S.pop();
	S.push(0);
	for(int i = n ; i >= 1 ; i--) {
		while(a[S.top()] < a[i])
			S.pop();
		//~ pr[i] = S.top();
		maxl[S.top()] = i;
		S.push(i);
	}
	while(!S.empty())
		S.pop();
	
	LT = new Tree(pl);
	RT = new Tree(pr);
	
	scanf("%d", &m);
	while(m--) {
		int x, y, c;
		scanf("%d%d%d", &x, &y, &c);
		sum_all += c;
		int l = LT->first_atleast(x, y);
		int r = RT->first_atleast(x, y);
		
		if(a[l] <= a[r])
			St[l].emplace_back(x, y, c);
		if(a[r] <= a[l])
			St[r].emplace_back(x, y, c);
		//~ cout << l << endl;
	}
	
	vector<ii> V(n);
	for(int i = 0 ; i < n ; i++)
		V[i] = {a[i + 1], i + 1};
	
	sort(V.begin(), V.end());
	
	//~ cout << maxl[3] << endl;
	
	for(auto p : V) {
		int i = p.Y;
		ll dpl = LT->query(maxl[i], pl[i]) + RT->query(maxl[i], i);
		ll dpr = LT->query(maxr[i], i) + RT->query(maxr[i], pr[i]);
		for(auto s : St[i]) {
			if(s.x > i)
				dpr = max(dpr, (ll)s.c + LT->query(s.x, i) + RT->query(s.x, pr[i]));
			else
				dpl = max(dpl, (ll)s.c + LT->query(s.x, pl[i]) + RT->query(s.x, i));
		}
		
		LT->insert(i, dpl);
		RT->insert(i, dpr);
		
		//~ cout << i << " " << dpl << " " << dpr << endl;
	}
	//~ cout << maxr[0] << endl;
	ll res = LT->query(maxr[0], 0) + RT->query(maxr[0], 0);
	for(auto s : St[0]) {
		//~ cout << s.x << " " << s.y << endl;
		res = max(res, (ll)s.c + LT->query(s.x, 0) + RT->query(s.x, 0));
	}
	printf("%lld\n", sum_all - res);
	
	return 0;
}

Compilation message

constellation3.cpp: In function 'int main()':
constellation3.cpp:124:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
constellation3.cpp:126:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &a[i]);
   ~~~~~^~~~~~~~~~~~~
constellation3.cpp:175:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &m);
  ~~~~~^~~~~~~~~~
constellation3.cpp:178:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &x, &y, &c);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14848 KB Output is correct
2 Correct 13 ms 14848 KB Output is correct
3 Correct 12 ms 14848 KB Output is correct
4 Correct 12 ms 14720 KB Output is correct
5 Correct 12 ms 14720 KB Output is correct
6 Correct 12 ms 14848 KB Output is correct
7 Correct 13 ms 14720 KB Output is correct
8 Correct 13 ms 14720 KB Output is correct
9 Correct 13 ms 14848 KB Output is correct
10 Correct 14 ms 14848 KB Output is correct
11 Correct 12 ms 14720 KB Output is correct
12 Correct 12 ms 14848 KB Output is correct
13 Correct 12 ms 14720 KB Output is correct
14 Correct 13 ms 14720 KB Output is correct
15 Correct 12 ms 14720 KB Output is correct
16 Correct 12 ms 14720 KB Output is correct
17 Correct 13 ms 14720 KB Output is correct
18 Correct 12 ms 14720 KB Output is correct
19 Correct 12 ms 14848 KB Output is correct
20 Correct 12 ms 14720 KB Output is correct
21 Correct 13 ms 14720 KB Output is correct
22 Correct 12 ms 14720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14848 KB Output is correct
2 Correct 13 ms 14848 KB Output is correct
3 Correct 12 ms 14848 KB Output is correct
4 Correct 12 ms 14720 KB Output is correct
5 Correct 12 ms 14720 KB Output is correct
6 Correct 12 ms 14848 KB Output is correct
7 Correct 13 ms 14720 KB Output is correct
8 Correct 13 ms 14720 KB Output is correct
9 Correct 13 ms 14848 KB Output is correct
10 Correct 14 ms 14848 KB Output is correct
11 Correct 12 ms 14720 KB Output is correct
12 Correct 12 ms 14848 KB Output is correct
13 Correct 12 ms 14720 KB Output is correct
14 Correct 13 ms 14720 KB Output is correct
15 Correct 12 ms 14720 KB Output is correct
16 Correct 12 ms 14720 KB Output is correct
17 Correct 13 ms 14720 KB Output is correct
18 Correct 12 ms 14720 KB Output is correct
19 Correct 12 ms 14848 KB Output is correct
20 Correct 12 ms 14720 KB Output is correct
21 Correct 13 ms 14720 KB Output is correct
22 Correct 12 ms 14720 KB Output is correct
23 Correct 15 ms 15232 KB Output is correct
24 Correct 15 ms 15232 KB Output is correct
25 Correct 16 ms 15232 KB Output is correct
26 Correct 16 ms 15232 KB Output is correct
27 Correct 15 ms 15360 KB Output is correct
28 Correct 16 ms 15232 KB Output is correct
29 Correct 15 ms 15232 KB Output is correct
30 Correct 15 ms 15232 KB Output is correct
31 Correct 15 ms 15232 KB Output is correct
32 Correct 15 ms 15232 KB Output is correct
33 Correct 15 ms 15232 KB Output is correct
34 Correct 15 ms 15232 KB Output is correct
35 Correct 15 ms 15232 KB Output is correct
36 Correct 17 ms 15360 KB Output is correct
37 Correct 15 ms 15360 KB Output is correct
38 Correct 15 ms 15360 KB Output is correct
39 Correct 15 ms 15232 KB Output is correct
40 Correct 15 ms 15232 KB Output is correct
41 Correct 15 ms 15232 KB Output is correct
42 Correct 14 ms 15232 KB Output is correct
43 Correct 15 ms 15232 KB Output is correct
44 Correct 15 ms 15232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14848 KB Output is correct
2 Correct 13 ms 14848 KB Output is correct
3 Correct 12 ms 14848 KB Output is correct
4 Correct 12 ms 14720 KB Output is correct
5 Correct 12 ms 14720 KB Output is correct
6 Correct 12 ms 14848 KB Output is correct
7 Correct 13 ms 14720 KB Output is correct
8 Correct 13 ms 14720 KB Output is correct
9 Correct 13 ms 14848 KB Output is correct
10 Correct 14 ms 14848 KB Output is correct
11 Correct 12 ms 14720 KB Output is correct
12 Correct 12 ms 14848 KB Output is correct
13 Correct 12 ms 14720 KB Output is correct
14 Correct 13 ms 14720 KB Output is correct
15 Correct 12 ms 14720 KB Output is correct
16 Correct 12 ms 14720 KB Output is correct
17 Correct 13 ms 14720 KB Output is correct
18 Correct 12 ms 14720 KB Output is correct
19 Correct 12 ms 14848 KB Output is correct
20 Correct 12 ms 14720 KB Output is correct
21 Correct 13 ms 14720 KB Output is correct
22 Correct 12 ms 14720 KB Output is correct
23 Correct 15 ms 15232 KB Output is correct
24 Correct 15 ms 15232 KB Output is correct
25 Correct 16 ms 15232 KB Output is correct
26 Correct 16 ms 15232 KB Output is correct
27 Correct 15 ms 15360 KB Output is correct
28 Correct 16 ms 15232 KB Output is correct
29 Correct 15 ms 15232 KB Output is correct
30 Correct 15 ms 15232 KB Output is correct
31 Correct 15 ms 15232 KB Output is correct
32 Correct 15 ms 15232 KB Output is correct
33 Correct 15 ms 15232 KB Output is correct
34 Correct 15 ms 15232 KB Output is correct
35 Correct 15 ms 15232 KB Output is correct
36 Correct 17 ms 15360 KB Output is correct
37 Correct 15 ms 15360 KB Output is correct
38 Correct 15 ms 15360 KB Output is correct
39 Correct 15 ms 15232 KB Output is correct
40 Correct 15 ms 15232 KB Output is correct
41 Correct 15 ms 15232 KB Output is correct
42 Correct 14 ms 15232 KB Output is correct
43 Correct 15 ms 15232 KB Output is correct
44 Correct 15 ms 15232 KB Output is correct
45 Correct 1103 ms 76936 KB Output is correct
46 Correct 1094 ms 76280 KB Output is correct
47 Correct 1128 ms 75768 KB Output is correct
48 Correct 1106 ms 76792 KB Output is correct
49 Correct 1080 ms 75256 KB Output is correct
50 Correct 1062 ms 75360 KB Output is correct
51 Correct 1062 ms 75388 KB Output is correct
52 Correct 1084 ms 76536 KB Output is correct
53 Correct 1088 ms 76408 KB Output is correct
54 Correct 948 ms 78712 KB Output is correct
55 Correct 934 ms 77048 KB Output is correct
56 Correct 925 ms 76408 KB Output is correct
57 Correct 931 ms 75748 KB Output is correct
58 Correct 835 ms 80984 KB Output is correct
59 Correct 846 ms 81112 KB Output is correct
60 Correct 289 ms 80248 KB Output is correct
61 Correct 950 ms 77472 KB Output is correct
62 Correct 914 ms 77688 KB Output is correct
63 Correct 981 ms 77620 KB Output is correct
64 Correct 903 ms 75384 KB Output is correct
65 Correct 885 ms 78328 KB Output is correct
66 Correct 940 ms 76796 KB Output is correct