Submission #212301

# Submission time Handle Problem Language Result Execution time Memory
212301 2020-03-22T16:48:19 Z arnold518 Constellation 3 (JOI20_constellation3) C++14
100 / 100
451 ms 50800 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 2e5;

struct Data
{
	int x, y; ll c;
	int l, r;
};

int N, A[MAXN+10], M, S;
Data B[MAXN+10];

struct Node
{
	int y, l, r, p;
	vector<pll> V;
};

Node nodes[MAXN+10];
vector<int> adj[MAXN+10];

struct BIT
{
	int tree[MAXN+10];
	void update(int i, int v) { for(; i<=N; i+=(i&-i)) tree[i]=max(tree[i], v); }
	int query(int i) { int ret=0; for(; i>0; i-=(i&-i)) ret=max(ret, tree[i]); return ret; }
}bit;

struct SEG
{
	ll tree[MAXN*4+10];

	void update(int node, int tl, int tr, int l, int r, ll v)
	{
		if(r<tl || tr<l) return;
		if(l<=tl && tr<=r)
		{
			tree[node]+=v;
			return;
		}
		int mid=tl+tr>>1;
		update(node*2, tl, mid, l, r, v);
		update(node*2+1, mid+1, tr, l, r, v);
	}

	ll query(int node, int tl, int tr, int p)
	{
		if(tl==tr) return tree[node];
		int mid=tl+tr>>1;
		if(p<=mid) return tree[node]+query(node*2, tl, mid, p);
		else return tree[node]+query(node*2+1, mid+1, tr, p);
	}
}seg;

ll dp[MAXN+10];

void dfs(int now)
{
	int i, j;
	ll val=0;

	vector<int> L, R;
	for(int nxt : adj[now])
	{
		dfs(nxt);
		val+=dp[nxt];
		L.push_back(nodes[nxt].l);
		R.push_back(nodes[nxt].r);
	}
	dp[now]=val;

	for(i=0; i<nodes[now].V.size(); i++)
	{
		ll t=0;
		int x=nodes[now].V[i].first;
		int p=lower_bound(R.begin(), R.end(), x)-R.begin();

		if(p<adj[now].size() && L[p]<=x && x<=R[p]) t=val-dp[adj[now][p]]+seg.query(1, 1, N, x);
		else t=val;
		dp[now]=max(dp[now], t+nodes[now].V[i].second);
	}

	for(i=nodes[now].l, j=0; i<=nodes[now].r; i++)
	{
		for(; j<adj[now].size() && L[j]==i; j++) i=R[j]+1;
		if(i>nodes[now].r) continue;
		seg.update(1, 1, N, i, i, val);
	}

	for(int nxt : adj[now]) seg.update(1, 1, N, nodes[nxt].l, nodes[nxt].r, val-dp[nxt]);
}

int main()
{
	int i, j;

	ll sum=0;

	scanf("%d", &N);
	for(i=1; i<=N; i++) scanf("%d", &A[i]);
	scanf("%d", &M);
	for(i=1; i<=M; i++) scanf("%d%d%lld", &B[i].x, &B[i].y, &B[i].c), sum+=B[i].c;
	sort(B+1, B+M+1, [&](const Data &p, const Data &q) { return p.x<q.x; });
	A[0]=N+1; A[N+1]=N+1;

	vector<int> ST;

	ST.push_back(0);
	for(i=1, j=1; i<=N; i++)
	{
		while(!ST.empty() && A[ST.back()]<=A[i]) ST.pop_back();
		ST.push_back(i);
		for(; j<=M && B[j].x==i; j++)
		{
			int lo=0, hi=ST.size();
			while(lo+1<hi)
			{
				int mid=lo+hi>>1;
				if(A[ST[mid]]>=B[j].y) lo=mid;
				else hi=mid;
			}
			B[j].l=ST[lo]+1;
		}
	}
	ST.clear();

	ST.push_back(N+1);
	for(i=N, j=M; i>=1; i--)
	{
		while(!ST.empty() && A[ST.back()]<=A[i]) ST.pop_back();
		ST.push_back(i);
		for(; j>=1 && B[j].x==i; j--)
		{
			int lo=0, hi=ST.size();
			while(lo+1<hi)
			{
				int mid=lo+hi>>1;
				if(A[ST[mid]]>=B[j].y) lo=mid;
				else hi=mid;
			}
			B[j].r=ST[lo]-1;
		}
	}
	ST.clear();

	sort(B+1, B+M+1, [&](const Data &p, const Data &q)
	{
		if(p.y!=q.y) return p.y>q.y;
		return p.l<q.l;
	});

	S++;
	nodes[S].y=N+1;
	nodes[S].l=1;
	nodes[S].r=N;
	nodes[S].p=S;
	for(i=1; i<=M; i++)
	{
		if(!(nodes[S].y==B[i].y && nodes[S].l==B[i].l && nodes[S].r==B[i].r))
		{
			S++;
			nodes[S].y=B[i].y;
			nodes[S].l=B[i].l;
			nodes[S].r=B[i].r;
			nodes[S].p=S;
		}
		nodes[S].V.push_back({B[i].x, B[i].c});
	}

	sort(nodes+1, nodes+S+1, [&](const Node &p, const Node &q)
	{
		if(p.l!=q.l) return p.l<q.l;
		if(p.r!=q.r) return p.r>q.r;
		return p.p<q.p;
	});

	for(i=1; i<=S; i++)
	{
		int par=bit.query(N-nodes[i].r+1);
		adj[par].push_back(nodes[i].p);
		bit.update(N-nodes[i].r+1, nodes[i].p);
	}

	sort(nodes+1, nodes+S+1, [&](const Node &p, const Node &q) { return p.p<q.p; });
	for(i=1; i<=S; i++)
	{
		sort(adj[i].begin(), adj[i].end(), [&](const int &p, const int &q) { return nodes[p].l<nodes[q].l; });
		sort(nodes[i].V.begin(), nodes[i].V.end());
	}

	dfs(1);
	printf("%lld\n", sum-dp[1]);

/*
	for(i=1; i<=S; i++)
	{
		printf("%d =========\n", i);
		printf("VAL : %d %d %d\n", nodes[i].y, nodes[i].l, nodes[i].r);
		printf("V : "); for(auto it : nodes[i].V) printf("%lld %lld / ", it.first, it.second); printf("\n");
		printf("ADJ : "); for(auto it : adj[i]) printf("%d ", it); printf("\n");
	}
*/
	//for(i=1; i<=M; i++) printf("%d %d %lld : %d %d\n", B[i].x, B[i].y, B[i].c, B[i].l, B[i].r);
}

Compilation message

constellation3.cpp: In member function 'void SEG::update(int, int, int, int, int, ll)':
constellation3.cpp:47:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int mid=tl+tr>>1;
           ~~^~~
constellation3.cpp: In member function 'll SEG::query(int, int, int, int)':
constellation3.cpp:55:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int mid=tl+tr>>1;
           ~~^~~
constellation3.cpp: In function 'void dfs(int)':
constellation3.cpp:78:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0; i<nodes[now].V.size(); i++)
           ~^~~~~~~~~~~~~~~~~~~~
constellation3.cpp:84:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(p<adj[now].size() && L[p]<=x && x<=R[p]) t=val-dp[adj[now][p]]+seg.query(1, 1, N, x);
      ~^~~~~~~~~~~~~~~~
constellation3.cpp:91:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(; j<adj[now].size() && L[j]==i; j++) i=R[j]+1;
         ~^~~~~~~~~~~~~~~~
constellation3.cpp: In function 'int main()':
constellation3.cpp:124:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int mid=lo+hi>>1;
             ~~^~~
constellation3.cpp:143:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int mid=lo+hi>>1;
             ~~^~~
constellation3.cpp:105:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
constellation3.cpp:106:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(i=1; i<=N; i++) scanf("%d", &A[i]);
                      ~~~~~^~~~~~~~~~~~~
constellation3.cpp:107:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &M);
  ~~~~~^~~~~~~~~~
constellation3.cpp:108:66: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(i=1; i<=M; i++) scanf("%d%d%lld", &B[i].x, &B[i].y, &B[i].c), sum+=B[i].c;
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12928 KB Output is correct
2 Correct 11 ms 12928 KB Output is correct
3 Correct 11 ms 12928 KB Output is correct
4 Correct 11 ms 12928 KB Output is correct
5 Correct 11 ms 12928 KB Output is correct
6 Correct 11 ms 12928 KB Output is correct
7 Correct 11 ms 12928 KB Output is correct
8 Correct 11 ms 12928 KB Output is correct
9 Correct 12 ms 12928 KB Output is correct
10 Correct 11 ms 12928 KB Output is correct
11 Correct 11 ms 12928 KB Output is correct
12 Correct 11 ms 12928 KB Output is correct
13 Correct 11 ms 12928 KB Output is correct
14 Correct 11 ms 12928 KB Output is correct
15 Correct 13 ms 12928 KB Output is correct
16 Correct 13 ms 12928 KB Output is correct
17 Correct 11 ms 12928 KB Output is correct
18 Correct 11 ms 12928 KB Output is correct
19 Correct 13 ms 12928 KB Output is correct
20 Correct 11 ms 12928 KB Output is correct
21 Correct 11 ms 12928 KB Output is correct
22 Correct 11 ms 12928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12928 KB Output is correct
2 Correct 11 ms 12928 KB Output is correct
3 Correct 11 ms 12928 KB Output is correct
4 Correct 11 ms 12928 KB Output is correct
5 Correct 11 ms 12928 KB Output is correct
6 Correct 11 ms 12928 KB Output is correct
7 Correct 11 ms 12928 KB Output is correct
8 Correct 11 ms 12928 KB Output is correct
9 Correct 12 ms 12928 KB Output is correct
10 Correct 11 ms 12928 KB Output is correct
11 Correct 11 ms 12928 KB Output is correct
12 Correct 11 ms 12928 KB Output is correct
13 Correct 11 ms 12928 KB Output is correct
14 Correct 11 ms 12928 KB Output is correct
15 Correct 13 ms 12928 KB Output is correct
16 Correct 13 ms 12928 KB Output is correct
17 Correct 11 ms 12928 KB Output is correct
18 Correct 11 ms 12928 KB Output is correct
19 Correct 13 ms 12928 KB Output is correct
20 Correct 11 ms 12928 KB Output is correct
21 Correct 11 ms 12928 KB Output is correct
22 Correct 11 ms 12928 KB Output is correct
23 Correct 13 ms 13056 KB Output is correct
24 Correct 14 ms 13184 KB Output is correct
25 Correct 15 ms 13056 KB Output is correct
26 Correct 14 ms 13056 KB Output is correct
27 Correct 14 ms 13184 KB Output is correct
28 Correct 14 ms 13184 KB Output is correct
29 Correct 13 ms 13056 KB Output is correct
30 Correct 14 ms 13056 KB Output is correct
31 Correct 14 ms 13056 KB Output is correct
32 Correct 14 ms 13184 KB Output is correct
33 Correct 14 ms 13184 KB Output is correct
34 Correct 14 ms 13184 KB Output is correct
35 Correct 14 ms 13312 KB Output is correct
36 Correct 15 ms 13312 KB Output is correct
37 Correct 15 ms 13184 KB Output is correct
38 Correct 13 ms 13312 KB Output is correct
39 Correct 13 ms 13056 KB Output is correct
40 Correct 14 ms 13184 KB Output is correct
41 Correct 13 ms 13056 KB Output is correct
42 Correct 13 ms 13056 KB Output is correct
43 Correct 16 ms 13184 KB Output is correct
44 Correct 13 ms 13056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12928 KB Output is correct
2 Correct 11 ms 12928 KB Output is correct
3 Correct 11 ms 12928 KB Output is correct
4 Correct 11 ms 12928 KB Output is correct
5 Correct 11 ms 12928 KB Output is correct
6 Correct 11 ms 12928 KB Output is correct
7 Correct 11 ms 12928 KB Output is correct
8 Correct 11 ms 12928 KB Output is correct
9 Correct 12 ms 12928 KB Output is correct
10 Correct 11 ms 12928 KB Output is correct
11 Correct 11 ms 12928 KB Output is correct
12 Correct 11 ms 12928 KB Output is correct
13 Correct 11 ms 12928 KB Output is correct
14 Correct 11 ms 12928 KB Output is correct
15 Correct 13 ms 12928 KB Output is correct
16 Correct 13 ms 12928 KB Output is correct
17 Correct 11 ms 12928 KB Output is correct
18 Correct 11 ms 12928 KB Output is correct
19 Correct 13 ms 12928 KB Output is correct
20 Correct 11 ms 12928 KB Output is correct
21 Correct 11 ms 12928 KB Output is correct
22 Correct 11 ms 12928 KB Output is correct
23 Correct 13 ms 13056 KB Output is correct
24 Correct 14 ms 13184 KB Output is correct
25 Correct 15 ms 13056 KB Output is correct
26 Correct 14 ms 13056 KB Output is correct
27 Correct 14 ms 13184 KB Output is correct
28 Correct 14 ms 13184 KB Output is correct
29 Correct 13 ms 13056 KB Output is correct
30 Correct 14 ms 13056 KB Output is correct
31 Correct 14 ms 13056 KB Output is correct
32 Correct 14 ms 13184 KB Output is correct
33 Correct 14 ms 13184 KB Output is correct
34 Correct 14 ms 13184 KB Output is correct
35 Correct 14 ms 13312 KB Output is correct
36 Correct 15 ms 13312 KB Output is correct
37 Correct 15 ms 13184 KB Output is correct
38 Correct 13 ms 13312 KB Output is correct
39 Correct 13 ms 13056 KB Output is correct
40 Correct 14 ms 13184 KB Output is correct
41 Correct 13 ms 13056 KB Output is correct
42 Correct 13 ms 13056 KB Output is correct
43 Correct 16 ms 13184 KB Output is correct
44 Correct 13 ms 13056 KB Output is correct
45 Correct 451 ms 35908 KB Output is correct
46 Correct 426 ms 35256 KB Output is correct
47 Correct 447 ms 36068 KB Output is correct
48 Correct 417 ms 35320 KB Output is correct
49 Correct 439 ms 35704 KB Output is correct
50 Correct 424 ms 35192 KB Output is correct
51 Correct 428 ms 35832 KB Output is correct
52 Correct 444 ms 35848 KB Output is correct
53 Correct 424 ms 35320 KB Output is correct
54 Correct 399 ms 50800 KB Output is correct
55 Correct 391 ms 50420 KB Output is correct
56 Correct 380 ms 50036 KB Output is correct
57 Correct 379 ms 49912 KB Output is correct
58 Correct 365 ms 49440 KB Output is correct
59 Correct 339 ms 49532 KB Output is correct
60 Correct 221 ms 50540 KB Output is correct
61 Correct 259 ms 28660 KB Output is correct
62 Correct 353 ms 45420 KB Output is correct
63 Correct 247 ms 28268 KB Output is correct
64 Correct 250 ms 28148 KB Output is correct
65 Correct 340 ms 45168 KB Output is correct
66 Correct 257 ms 28332 KB Output is correct