Submission #1088532

# Submission time Handle Problem Language Result Execution time Memory
1088532 2024-09-14T14:56:49 Z Sunbae Palembang Bridges (APIO15_bridge) C++17
100 / 100
124 ms 8184 KB
#include <bits/stdc++.h>
typedef long long ll;
#define F first
#define S second
#define mp make_pair
using namespace std;
using pii = pair<int,int>;
const int N = 1e5 + 5, M = 2e5 + 5;
int sz, m, med[2][N], bit[M], cp[M];
ll BIT[M], Ans[2][N];
pii A[N];
int pos(int x){ return upper_bound(cp, cp+m, x) - cp;}
void upd(int i, int v){ for(; i<M; i+=i&-i) bit[i] += v;}
void UPD(int i, ll v){ for(; i<M; i+=i&-i) BIT[i] += v;}
int qry(int i){ int res = 0; for(; i; i-=i&-i) res += bit[i]; return res;}
ll QRY(int i){ ll res = 0; for(; i; i-=i&-i) res += BIT[i]; return res;}
int _find(int k){
	int i = 0, sum = 0;
	for(int j = 18; j>=0; --j){
		if(i+(1<<j) < M && sum + bit[i+(1<<j)] < k) sum += bit[i+=(1<<j)];
	}
	return i + 1;
}
signed main(){
	int k, n; scanf("%d %d", &k, &n);
	ll tot = 0;
	for(int i = 0, p[2]; i<n; ++i){
		char c[2]; scanf(" %c %d %c %d", c, p, c+1, p+1); if(p[0] > p[1]) swap(p[0], p[1]);
		if(c[0] == c[1]) tot += p[1] - p[0];
		else{
			cp[m++] = p[0]; cp[m++] = p[1];
			A[sz++] = mp(p[0], p[1]); 
		}
	}
	auto cmp = [&](pii x, pii y){ return x.F + x.S < y.F + y.S;};
	sort(A, A+sz, cmp); sort(cp, cp+m); m = unique(cp, cp+m) - cp;
	int st = 0, ed = sz;
	for(int j = 0; j<2; ++j){
		memset(bit, 0, sizeof(bit)); memset(BIT, 0, sizeof(BIT));
		for(int i = st, cnt = 0; i != ed; !j ? ++i : --i){
			pii idx = mp(pos(A[i].F), pos(A[i].S));
			upd(idx.F, +1); upd(idx.S, +1);
			UPD(idx.F, +A[i].F); UPD(idx.S, +A[i].S);
			cnt += 2;
			int I = _find((cnt+1)/2);

			Ans[j][i] = (ll)qry(I) * (ll)cp[I-1] - QRY(I) + QRY(M-1) - QRY(I) - (ll)(qry(M-1) - qry(I)) * cp[I-1];
		}
		st = sz-1; ed = -1;
	}
	tot += sz;
	if(k == 1 || !sz){ printf("%lld", tot + Ans[0][sz-1]); return 0;}
	ll mn = 2e18;
	for(int i = 0; i+1<sz; ++i) mn = min(mn, Ans[0][i] + Ans[1][i+1]);
	printf("%lld", tot + mn);
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:25:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |  int k, n; scanf("%d %d", &k, &n);
      |            ~~~~~^~~~~~~~~~~~~~~~~
bridge.cpp:28:19: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |   char c[2]; scanf(" %c %d %c %d", c, p, c+1, p+1); if(p[0] > p[1]) swap(p[0], p[1]);
      |              ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2648 KB Output is correct
2 Correct 2 ms 2752 KB Output is correct
3 Correct 2 ms 2760 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 3 ms 2652 KB Output is correct
7 Correct 3 ms 2652 KB Output is correct
8 Correct 3 ms 2652 KB Output is correct
9 Correct 2 ms 2652 KB Output is correct
10 Correct 2 ms 2652 KB Output is correct
11 Correct 3 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 2 ms 2768 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 2 ms 2652 KB Output is correct
9 Correct 2 ms 2648 KB Output is correct
10 Correct 2 ms 2652 KB Output is correct
11 Correct 2 ms 2648 KB Output is correct
12 Correct 55 ms 6632 KB Output is correct
13 Correct 121 ms 8080 KB Output is correct
14 Correct 83 ms 6736 KB Output is correct
15 Correct 68 ms 5968 KB Output is correct
16 Correct 54 ms 7252 KB Output is correct
17 Correct 77 ms 8184 KB Output is correct
18 Correct 73 ms 7504 KB Output is correct
19 Correct 88 ms 8016 KB Output is correct
20 Correct 58 ms 7508 KB Output is correct
21 Correct 80 ms 7720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2824 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2716 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 2 ms 2748 KB Output is correct
10 Correct 1 ms 2648 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 2 ms 2652 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2648 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 2 ms 2768 KB Output is correct
15 Correct 3 ms 2652 KB Output is correct
16 Correct 2 ms 2652 KB Output is correct
17 Correct 2 ms 2652 KB Output is correct
18 Correct 2 ms 2824 KB Output is correct
19 Correct 2 ms 2652 KB Output is correct
20 Correct 2 ms 2652 KB Output is correct
21 Correct 2 ms 2652 KB Output is correct
22 Correct 2 ms 2652 KB Output is correct
23 Correct 2 ms 2652 KB Output is correct
24 Correct 2 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 2 ms 2652 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 2 ms 2652 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 2 ms 2648 KB Output is correct
18 Correct 2 ms 2648 KB Output is correct
19 Correct 2 ms 2652 KB Output is correct
20 Correct 2 ms 2652 KB Output is correct
21 Correct 2 ms 2652 KB Output is correct
22 Correct 2 ms 2792 KB Output is correct
23 Correct 2 ms 2676 KB Output is correct
24 Correct 2 ms 2660 KB Output is correct
25 Correct 56 ms 6628 KB Output is correct
26 Correct 66 ms 6816 KB Output is correct
27 Correct 124 ms 7500 KB Output is correct
28 Correct 117 ms 8088 KB Output is correct
29 Correct 123 ms 8020 KB Output is correct
30 Correct 59 ms 5576 KB Output is correct
31 Correct 58 ms 7340 KB Output is correct
32 Correct 76 ms 8112 KB Output is correct
33 Correct 75 ms 7752 KB Output is correct
34 Correct 90 ms 8152 KB Output is correct
35 Correct 60 ms 7760 KB Output is correct
36 Correct 82 ms 7792 KB Output is correct
37 Correct 50 ms 6672 KB Output is correct