답안 #243719

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
243719 2020-07-01T16:07:39 Z arnold518 Two Dishes (JOI19_dishes) C++14
100 / 100
3958 ms 99356 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 = 1e6;

int N, M;
ll A[MAXN+10], S[MAXN+10], P[MAXN+10];
ll B[MAXN+10], T[MAXN+10], Q[MAXN+10];
ll ans;

struct Point
{
	int x, y; ll w;
};
vector<Point> V;

ll tree[MAXN*4+10];
bool lazy[MAXN*4+10];

void busy(int node, int tl, int tr)
{
	if(!lazy[node]) return;
	tree[node]=0;
	if(tl!=tr) lazy[node*2]=true, lazy[node*2+1]=true;
	lazy[node]=false;
}

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

int query2(int node, int tl, int tr, ll v)
{
	busy(node, tl, tr);
	if(tl==tr) return tl;
	ll t=0;
	if(!lazy[node*2]) t=tree[node*2];
	int mid=tl+tr>>1;
	if(t>v) return query2(node*2, tl, mid, v);
	else return query2(node*2+1, mid+1, tr, v-t);
}

void update1(int node, int tl, int tr, int p, ll v)
{
	busy(node, tl, tr);
	if(tr<p) return;
	if(tl==tr)
	{
		tree[node]+=v;
		return;
	}
	int mid=tl+tr>>1;
	if(p<=mid) update1(node*2, tl, mid, p, v);
	else update1(node*2+1, mid+1, tr, p, v);
	tree[node]=0;
	if(!lazy[node*2]) tree[node]+=tree[node*2];
	if(!lazy[node*2+1]) tree[node]+=tree[node*2+1];
}

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

int main()
{
	int i, j;

	scanf("%d%d", &N, &M);
	for(i=1; i<=N; i++) scanf("%lld%lld%lld", &A[i], &S[i], &P[i]), A[i]+=A[i-1];
	for(i=1; i<=M; i++) scanf("%lld%lld%lld", &B[i], &T[i], &Q[i]), B[i]+=B[i-1];

	for(i=1; i<=N; i++)
	{
		if(A[i]>S[i]) continue;
		int x=i, y=upper_bound(B+1, B+M+1, S[i]-A[i])-B-1;
		
		ans+=P[i];

		if(y!=M)
		{
			x--; y++;
			if(x!=N && y!=0) V.push_back({x, y, -P[i]});
		}
	}
	for(i=1; i<=M; i++)
	{
		if(B[i]>T[i]) continue;
		int y=i, x=upper_bound(A+1, A+N+1, T[i]-B[i])-A-1;
		
		if(x!=N && y!=0) V.push_back({x, y, Q[i]});
		else ans+=Q[i];
	}
	
	sort(V.begin(), V.end(), [&](const Point &p, const Point &q)
	{
		if(p.x!=q.x) return p.x<q.x;
		if(p.y!=q.y) return p.y>q.y;
		return p.w>q.w;
	});

	for(i=0; i<V.size(); i++)
	{
		int y=V[i].y; ll w=V[i].w;
		if(w>=0) update1(1, 0, M, y, w);
		else
		{
			ll t=query(1, 0, M, 0, y-1)-w;
			int pt=M+1;
			if(query(1, 0, M, 0, M)>t) pt=query2(1, 0, M, t);
			ll p=query(1, 0, M, 0, pt), q=query(1, 0, M, pt, pt);
			update2(1, 0, M, y, pt-1);
			update1(1, 0, M, pt, -q+p-t);
		}
	}
	ans+=query(1, 0, M, 0, M);
	printf("%lld\n", ans);
}

Compilation message

dishes.cpp: In function 'll query(int, int, int, int, int)':
dishes.cpp:37:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid=tl+tr>>1;
          ~~^~~
dishes.cpp: In function 'int query2(int, int, int, ll)':
dishes.cpp:48:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid=tl+tr>>1;
          ~~^~~
dishes.cpp: In function 'void update1(int, int, int, int, ll)':
dishes.cpp:62:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid=tl+tr>>1;
          ~~^~~
dishes.cpp: In function 'void update2(int, int, int, int, int)':
dishes.cpp:81:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid=tl+tr>>1;
          ~~^~~
dishes.cpp: In function 'int main()':
dishes.cpp:124:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0; i<V.size(); i++)
           ~^~~~~~~~~
dishes.cpp:89:9: warning: unused variable 'j' [-Wunused-variable]
  int i, j;
         ^
dishes.cpp:91:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &M);
  ~~~~~^~~~~~~~~~~~~~~~
dishes.cpp:92:64: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(i=1; i<=N; i++) scanf("%lld%lld%lld", &A[i], &S[i], &P[i]), A[i]+=A[i-1];
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
dishes.cpp:93:64: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(i=1; i<=M; i++) scanf("%lld%lld%lld", &B[i], &T[i], &Q[i]), B[i]+=B[i-1];
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 417 ms 27472 KB Output is correct
2 Correct 379 ms 22224 KB Output is correct
3 Correct 224 ms 12664 KB Output is correct
4 Correct 339 ms 19296 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 380 ms 20576 KB Output is correct
7 Correct 111 ms 9848 KB Output is correct
8 Correct 111 ms 9848 KB Output is correct
9 Correct 223 ms 12664 KB Output is correct
10 Correct 398 ms 25552 KB Output is correct
11 Correct 169 ms 14652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 7 ms 640 KB Output is correct
18 Correct 7 ms 640 KB Output is correct
19 Correct 9 ms 768 KB Output is correct
20 Correct 8 ms 640 KB Output is correct
21 Correct 8 ms 768 KB Output is correct
22 Correct 8 ms 640 KB Output is correct
23 Correct 8 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 7 ms 640 KB Output is correct
18 Correct 7 ms 640 KB Output is correct
19 Correct 9 ms 768 KB Output is correct
20 Correct 8 ms 640 KB Output is correct
21 Correct 8 ms 768 KB Output is correct
22 Correct 8 ms 640 KB Output is correct
23 Correct 8 ms 640 KB Output is correct
24 Correct 279 ms 21224 KB Output is correct
25 Correct 248 ms 20576 KB Output is correct
26 Correct 306 ms 21248 KB Output is correct
27 Correct 251 ms 20320 KB Output is correct
28 Correct 332 ms 19048 KB Output is correct
29 Correct 188 ms 12560 KB Output is correct
30 Correct 638 ms 23376 KB Output is correct
31 Correct 130 ms 15208 KB Output is correct
32 Correct 113 ms 11756 KB Output is correct
33 Correct 424 ms 20700 KB Output is correct
34 Correct 468 ms 21840 KB Output is correct
35 Correct 574 ms 24940 KB Output is correct
36 Correct 557 ms 24928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 7 ms 640 KB Output is correct
18 Correct 7 ms 640 KB Output is correct
19 Correct 9 ms 768 KB Output is correct
20 Correct 8 ms 640 KB Output is correct
21 Correct 8 ms 768 KB Output is correct
22 Correct 8 ms 640 KB Output is correct
23 Correct 8 ms 640 KB Output is correct
24 Correct 279 ms 21224 KB Output is correct
25 Correct 248 ms 20576 KB Output is correct
26 Correct 306 ms 21248 KB Output is correct
27 Correct 251 ms 20320 KB Output is correct
28 Correct 332 ms 19048 KB Output is correct
29 Correct 188 ms 12560 KB Output is correct
30 Correct 638 ms 23376 KB Output is correct
31 Correct 130 ms 15208 KB Output is correct
32 Correct 113 ms 11756 KB Output is correct
33 Correct 424 ms 20700 KB Output is correct
34 Correct 468 ms 21840 KB Output is correct
35 Correct 574 ms 24940 KB Output is correct
36 Correct 557 ms 24928 KB Output is correct
37 Correct 328 ms 20704 KB Output is correct
38 Correct 281 ms 20316 KB Output is correct
39 Correct 426 ms 22864 KB Output is correct
40 Correct 457 ms 23116 KB Output is correct
41 Correct 4 ms 384 KB Output is correct
42 Correct 663 ms 23504 KB Output is correct
43 Correct 443 ms 19808 KB Output is correct
44 Correct 481 ms 21072 KB Output is correct
45 Correct 604 ms 24068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 7 ms 640 KB Output is correct
18 Correct 7 ms 640 KB Output is correct
19 Correct 9 ms 768 KB Output is correct
20 Correct 8 ms 640 KB Output is correct
21 Correct 8 ms 768 KB Output is correct
22 Correct 8 ms 640 KB Output is correct
23 Correct 8 ms 640 KB Output is correct
24 Correct 279 ms 21224 KB Output is correct
25 Correct 248 ms 20576 KB Output is correct
26 Correct 306 ms 21248 KB Output is correct
27 Correct 251 ms 20320 KB Output is correct
28 Correct 332 ms 19048 KB Output is correct
29 Correct 188 ms 12560 KB Output is correct
30 Correct 638 ms 23376 KB Output is correct
31 Correct 130 ms 15208 KB Output is correct
32 Correct 113 ms 11756 KB Output is correct
33 Correct 424 ms 20700 KB Output is correct
34 Correct 468 ms 21840 KB Output is correct
35 Correct 574 ms 24940 KB Output is correct
36 Correct 557 ms 24928 KB Output is correct
37 Correct 328 ms 20704 KB Output is correct
38 Correct 281 ms 20316 KB Output is correct
39 Correct 426 ms 22864 KB Output is correct
40 Correct 457 ms 23116 KB Output is correct
41 Correct 4 ms 384 KB Output is correct
42 Correct 663 ms 23504 KB Output is correct
43 Correct 443 ms 19808 KB Output is correct
44 Correct 481 ms 21072 KB Output is correct
45 Correct 604 ms 24068 KB Output is correct
46 Correct 1669 ms 83988 KB Output is correct
47 Correct 1431 ms 81864 KB Output is correct
48 Correct 2220 ms 98796 KB Output is correct
49 Correct 2487 ms 99356 KB Output is correct
50 Correct 3922 ms 98972 KB Output is correct
51 Correct 2383 ms 81084 KB Output is correct
52 Correct 2467 ms 85952 KB Output is correct
53 Correct 3469 ms 97692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 417 ms 27472 KB Output is correct
2 Correct 379 ms 22224 KB Output is correct
3 Correct 224 ms 12664 KB Output is correct
4 Correct 339 ms 19296 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 380 ms 20576 KB Output is correct
7 Correct 111 ms 9848 KB Output is correct
8 Correct 111 ms 9848 KB Output is correct
9 Correct 223 ms 12664 KB Output is correct
10 Correct 398 ms 25552 KB Output is correct
11 Correct 169 ms 14652 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 4 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 4 ms 384 KB Output is correct
22 Correct 4 ms 384 KB Output is correct
23 Correct 4 ms 384 KB Output is correct
24 Correct 4 ms 384 KB Output is correct
25 Correct 4 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 7 ms 640 KB Output is correct
29 Correct 7 ms 640 KB Output is correct
30 Correct 9 ms 768 KB Output is correct
31 Correct 8 ms 640 KB Output is correct
32 Correct 8 ms 768 KB Output is correct
33 Correct 8 ms 640 KB Output is correct
34 Correct 8 ms 640 KB Output is correct
35 Correct 279 ms 21224 KB Output is correct
36 Correct 248 ms 20576 KB Output is correct
37 Correct 306 ms 21248 KB Output is correct
38 Correct 251 ms 20320 KB Output is correct
39 Correct 332 ms 19048 KB Output is correct
40 Correct 188 ms 12560 KB Output is correct
41 Correct 638 ms 23376 KB Output is correct
42 Correct 130 ms 15208 KB Output is correct
43 Correct 113 ms 11756 KB Output is correct
44 Correct 424 ms 20700 KB Output is correct
45 Correct 468 ms 21840 KB Output is correct
46 Correct 574 ms 24940 KB Output is correct
47 Correct 557 ms 24928 KB Output is correct
48 Correct 328 ms 20704 KB Output is correct
49 Correct 281 ms 20316 KB Output is correct
50 Correct 426 ms 22864 KB Output is correct
51 Correct 457 ms 23116 KB Output is correct
52 Correct 4 ms 384 KB Output is correct
53 Correct 663 ms 23504 KB Output is correct
54 Correct 443 ms 19808 KB Output is correct
55 Correct 481 ms 21072 KB Output is correct
56 Correct 604 ms 24068 KB Output is correct
57 Correct 300 ms 20572 KB Output is correct
58 Correct 336 ms 21212 KB Output is correct
59 Correct 588 ms 22992 KB Output is correct
60 Correct 340 ms 22352 KB Output is correct
61 Correct 398 ms 22352 KB Output is correct
62 Correct 5 ms 384 KB Output is correct
63 Correct 691 ms 23760 KB Output is correct
64 Correct 440 ms 19676 KB Output is correct
65 Correct 554 ms 21680 KB Output is correct
66 Correct 575 ms 22864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 417 ms 27472 KB Output is correct
2 Correct 379 ms 22224 KB Output is correct
3 Correct 224 ms 12664 KB Output is correct
4 Correct 339 ms 19296 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 380 ms 20576 KB Output is correct
7 Correct 111 ms 9848 KB Output is correct
8 Correct 111 ms 9848 KB Output is correct
9 Correct 223 ms 12664 KB Output is correct
10 Correct 398 ms 25552 KB Output is correct
11 Correct 169 ms 14652 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 4 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 4 ms 384 KB Output is correct
22 Correct 4 ms 384 KB Output is correct
23 Correct 4 ms 384 KB Output is correct
24 Correct 4 ms 384 KB Output is correct
25 Correct 4 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 7 ms 640 KB Output is correct
29 Correct 7 ms 640 KB Output is correct
30 Correct 9 ms 768 KB Output is correct
31 Correct 8 ms 640 KB Output is correct
32 Correct 8 ms 768 KB Output is correct
33 Correct 8 ms 640 KB Output is correct
34 Correct 8 ms 640 KB Output is correct
35 Correct 279 ms 21224 KB Output is correct
36 Correct 248 ms 20576 KB Output is correct
37 Correct 306 ms 21248 KB Output is correct
38 Correct 251 ms 20320 KB Output is correct
39 Correct 332 ms 19048 KB Output is correct
40 Correct 188 ms 12560 KB Output is correct
41 Correct 638 ms 23376 KB Output is correct
42 Correct 130 ms 15208 KB Output is correct
43 Correct 113 ms 11756 KB Output is correct
44 Correct 424 ms 20700 KB Output is correct
45 Correct 468 ms 21840 KB Output is correct
46 Correct 574 ms 24940 KB Output is correct
47 Correct 557 ms 24928 KB Output is correct
48 Correct 328 ms 20704 KB Output is correct
49 Correct 281 ms 20316 KB Output is correct
50 Correct 426 ms 22864 KB Output is correct
51 Correct 457 ms 23116 KB Output is correct
52 Correct 4 ms 384 KB Output is correct
53 Correct 663 ms 23504 KB Output is correct
54 Correct 443 ms 19808 KB Output is correct
55 Correct 481 ms 21072 KB Output is correct
56 Correct 604 ms 24068 KB Output is correct
57 Correct 1669 ms 83988 KB Output is correct
58 Correct 1431 ms 81864 KB Output is correct
59 Correct 2220 ms 98796 KB Output is correct
60 Correct 2487 ms 99356 KB Output is correct
61 Correct 3922 ms 98972 KB Output is correct
62 Correct 2383 ms 81084 KB Output is correct
63 Correct 2467 ms 85952 KB Output is correct
64 Correct 3469 ms 97692 KB Output is correct
65 Correct 300 ms 20572 KB Output is correct
66 Correct 336 ms 21212 KB Output is correct
67 Correct 588 ms 22992 KB Output is correct
68 Correct 340 ms 22352 KB Output is correct
69 Correct 398 ms 22352 KB Output is correct
70 Correct 5 ms 384 KB Output is correct
71 Correct 691 ms 23760 KB Output is correct
72 Correct 440 ms 19676 KB Output is correct
73 Correct 554 ms 21680 KB Output is correct
74 Correct 575 ms 22864 KB Output is correct
75 Correct 1461 ms 81564 KB Output is correct
76 Correct 1712 ms 83432 KB Output is correct
77 Correct 3196 ms 98968 KB Output is correct
78 Correct 1730 ms 98456 KB Output is correct
79 Correct 3958 ms 98712 KB Output is correct
80 Correct 2518 ms 81728 KB Output is correct
81 Correct 2818 ms 88180 KB Output is correct
82 Correct 3594 ms 98100 KB Output is correct
83 Correct 3758 ms 97308 KB Output is correct