Submission #582495

# Submission time Handle Problem Language Result Execution time Memory
582495 2022-06-23T23:22:41 Z NekoRolly Palembang Bridges (APIO15_bridge) C++17
63 / 100
2000 ms 23988 KB
#include<bits/stdc++.h>
using namespace std;
#define upp(a, b) upper_bound(b.begin(), b.end(), a) - b.begin()
typedef long long ll;
const int N = 2e5+4;
const ll inf = 4e18;

struct node{
	ll a,b,c;
};

node operator+(node a,node b){
	return {a.a + b.a, a.b + b.b, a.c + b.c};
}

struct St{
	int n;
	node t[N<<2];

	void build(int _n){ n = _n;
		for (int i=1; i<2*n; i++) t[i] = {0, 0, 0};
	}

	void up(int i,node val){ i+=n;
		for (t[i] = t[i] + val; i>>=1; ) t[i] = t[i<<1] + t[i<<1|1];
	}

	node que(int l,int r){ node ans = {0, 0, 0};
		for (l+=n, r+=n; l<r; l>>=1, r>>=1){
			if (l&1) ans = ans + t[l++];
			if (r&1) ans = ans + t[--r];
		}
		return ans;
	}
} d3;

ll k,n,n1,n2;
ll ans1,ans2;
pair<ll,ll> a1[N];
vector<ll> d1,d2[N];
ll pr[N],Spr[N],su[N],Ssu[N];

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cin >> k >> n; ans2 = inf;

	for (ll l,r; n; n--){ char cl,cr; cin >> cl >> l >> cr >> r;
		if (l > r) swap(l, r); ans1 += r-l;
		if (cl != cr) a1[n1++] = {l, r};
	}

	if (n1 == 0){ cout << ans1 << "\n"; return 0;}

	ans1 += n1;

	for (int i=0; i<n1; i++){ auto [l, r] = a1[i];
		d1.push_back(l), d1.push_back(r);
	}

	sort(d1.begin(), d1.end());
	d1.erase(unique(d1.begin(), d1.end()), d1.end());
	n2 = d1.size();

	for (int i=0; i<n1; i++){ auto [l, r] = a1[i];
		d2[upp(r, d1)].push_back(upp(l, d1));
		pr[upp(r, d1)]++, su[upp(l, d1)]++, Spr[upp(r, d1)] += r, Ssu[upp(l, d1)] += l;
	}

	for (int i=1; i<=n2; i++) pr[i] += pr[i-1], Spr[i] += Spr[i-1];
	for (int i=n2; i>=1; i--) su[i] += su[i+1], Ssu[i] += Ssu[i+1];

	// for (int i=1; i<=n2; i++) cout << d1[i-1] << " "; cout << "\n";


	// for (int i=1; i<=n2; i++)
		// cout << i << " -> " << pr[i] << " " << Spr[i] << " " << su[i] << " " << Ssu[i] << "\n";

	if (k == 1)	for (int i=1; i<=n2; i++)
		ans2 = min(ans2, Ssu[i+1] - Spr[i-1] + d1[i-1]*(pr[i-1] - su[i+1]));
	else for (int i=1; i+1<=n2; i++){ vector<int> d4;
		for (int j=i+1; j<=n2; j++) for (int l : d2[j-1]) if (l > i) 
			d4.push_back(d1[l-1] + d1[j-2]);
		sort(d4.begin(), d4.end());
		d4.erase(unique(d4.begin(), d4.end()), d4.end());
		d3.build(d4.size());
		for (int j=i+1; j<=n2; j++){
			for (int l : d2[j-1]) if (l > i)
				d3.up(upp(d1[l-1] + d1[j-2], d4)-1, {1, d1[l-1], d1[j-2]});
			node x = d3.que(0, upp(d1[i-1] + d1[j-1], d4));
			node y = d3.que(upp(d1[i-1] + d1[j-1], d4), d3.n);
			ll w1 = x.b - d1[i-1]*x.a + d1[j-1]*y.a - y.c;
			ll w2 = d1[i-1]*pr[i-1] - Spr[i-1] + Ssu[j+1] - d1[j-1]*su[j+1]; 
			ans2 = min(ans2, w1 + w2);
		}
	}

	// cout << ans1 << " " << ans2 << "\n";
	cout << ans1 + 2*ans2 << "\n";

    return 0;
}

/*

2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7

*/

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:49:3: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   49 |   if (l > r) swap(l, r); ans1 += r-l;
      |   ^~
bridge.cpp:49:26: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   49 |   if (l > r) swap(l, r); ans1 += r-l;
      |                          ^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 4 ms 5076 KB Output is correct
5 Correct 5 ms 5076 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 4 ms 5092 KB Output is correct
10 Correct 5 ms 5076 KB Output is correct
11 Correct 4 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5008 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 4 ms 5076 KB Output is correct
5 Correct 4 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 4 ms 5076 KB Output is correct
12 Correct 35 ms 9740 KB Output is correct
13 Correct 165 ms 17528 KB Output is correct
14 Correct 90 ms 9432 KB Output is correct
15 Correct 79 ms 12396 KB Output is correct
16 Correct 38 ms 9968 KB Output is correct
17 Correct 75 ms 17484 KB Output is correct
18 Correct 88 ms 17516 KB Output is correct
19 Correct 149 ms 17172 KB Output is correct
20 Correct 49 ms 9924 KB Output is correct
21 Correct 92 ms 17512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 7 ms 5076 KB Output is correct
5 Correct 4 ms 5040 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 4 ms 5076 KB Output is correct
8 Correct 8 ms 5040 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 5 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 5 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 5 ms 5076 KB Output is correct
5 Correct 4 ms 5076 KB Output is correct
6 Correct 3 ms 5044 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 6 ms 5036 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 5 ms 5076 KB Output is correct
11 Correct 3 ms 5044 KB Output is correct
12 Correct 5 ms 5076 KB Output is correct
13 Correct 3 ms 5040 KB Output is correct
14 Correct 8 ms 5076 KB Output is correct
15 Correct 262 ms 5248 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 6 ms 5040 KB Output is correct
18 Correct 40 ms 5132 KB Output is correct
19 Correct 4 ms 5100 KB Output is correct
20 Correct 348 ms 5248 KB Output is correct
21 Correct 37 ms 5204 KB Output is correct
22 Correct 274 ms 5248 KB Output is correct
23 Correct 4 ms 5076 KB Output is correct
24 Correct 300 ms 5260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 5 ms 5076 KB Output is correct
5 Correct 4 ms 5076 KB Output is correct
6 Correct 3 ms 5040 KB Output is correct
7 Correct 4 ms 4948 KB Output is correct
8 Correct 5 ms 5076 KB Output is correct
9 Correct 4 ms 5084 KB Output is correct
10 Correct 7 ms 5040 KB Output is correct
11 Correct 4 ms 5036 KB Output is correct
12 Correct 5 ms 5076 KB Output is correct
13 Correct 3 ms 5040 KB Output is correct
14 Correct 9 ms 5144 KB Output is correct
15 Correct 267 ms 5252 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 6 ms 5040 KB Output is correct
18 Correct 36 ms 5124 KB Output is correct
19 Correct 3 ms 5048 KB Output is correct
20 Correct 341 ms 5256 KB Output is correct
21 Correct 37 ms 5204 KB Output is correct
22 Correct 292 ms 5244 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 281 ms 5252 KB Output is correct
25 Correct 26 ms 10492 KB Output is correct
26 Correct 404 ms 11192 KB Output is correct
27 Execution timed out 2061 ms 23988 KB Time limit exceeded
28 Halted 0 ms 0 KB -