답안 #485433

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
485433 2021-11-07T18:13:27 Z BackNoob Palembang Bridges (APIO15_bridge) C++14
100 / 100
172 ms 13768 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1) 
#define TASK "task"
#define sz(s) (int) (s).size()
 
using namespace std;
const int mxN = 3e5 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
 
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
	if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
	if (a < b) {a = b; return true;} return false;
}
 
struct item{
	int b1 , z1 , b2 , z2;
} a[mxN];
 
vector<int> co;
 
struct fenwicktree{
	vector<pair<ll , ll>> bit;
	int n;
	fenwicktree(){}
	fenwicktree(int _n)
	{
		n = _n;
		bit.resize(n + 7 , {0 , 0});
	}
	void update(int pos , int val)
	{
		int x = co[pos - 1];
		if(val < 0) x = -x;
		for(; pos <= n ; pos += (pos & (-pos))) {
			bit[pos].fi += val;
			bit[pos].se += x;
		}
	}
	int sum(int pos)
	{
		int res = 0;
		for(; pos > 0 ; pos -= (pos & (-pos))) res += bit[pos].fi;
		return res;
	}
	ll sumval(int pos)
	{
		ll res = 0;
		for(; pos > 0 ; pos -= (pos & (-pos))) res += bit[pos].se;
		return res;
	}
	ll median()
	{
		if(n == 0) return 0;
		int lo = 0 , hi = n;
		int le = sum(n);
		while(lo + 1 < hi) {
			int mid = (lo + hi) / 2;
			if(sum(mid) >= (le + 1) / 2) hi = mid;
			else lo = mid;
		}
		ll ans = 0;
		ans += 1LL * co[hi - 1] * sum(hi) - sumval(hi);
		ans += sumval(n) - sumval(hi) - 1LL * (sum(n) - sum(hi)) * co[hi - 1];
		return ans;
	}
} ft1 , ft2;
 
 
int n , k;
pair<int , int> val[mxN];
int cnt[mxN];
 
int compress(int x)
{
	return lower_bound(all(co) , x) - co.begin() + 1;
}
 
ll res1 = infll , res2 = infll;
ll ans1[mxN] , ans2[mxN];
 
void modify(int x , int y)
{
	ft1.update(x , 1);
	ft1.update(y , 1);
	ft2.update(x , -1);
	ft2.update(y , -1);
 
	res1 = ft1.median();
	res2 = ft2.median();
}
 
bool cmp(pair<int , int> &a , pair<int , int> &b)
{
	return co[a.fi - 1] + co[a.se - 1] < co[b.fi - 1] + co[b.se - 1];
}

int CNT[mxN];
 
void solve()
{
	cin >> k >> n;
	for(int i = 1 ; i <= n ; i++) {
		char c1 , c2;
		cin >> c1 >> a[i].b1 >> c2 >>  a[i].b2;
		a[i].z1 = (c1 == 'A' ? 1 : 2);
		a[i].z2 = (c2 == 'A' ? 1 : 2);
	}
 
	for(int i = 1 ; i <= n ; i++) {
		if(a[i].z1 == a[i].z2) continue;
		co.pb(a[i].b1);
		co.pb(a[i].b2);
	}
	sort(all(co));
	co.erase(unique(all(co)) , co.end());
 
	for(int i = 1 ; i <= n ; i++) {
		if(a[i].z1 == a[i].z2) continue;
		a[i].b1 = compress(a[i].b1);
		a[i].b2 = compress(a[i].b2);
	}
 
	ll re = 0;
	int cnt = 0;
	for(int i = 1 ; i <= n ; i++) {
		if(a[i].z1 == a[i].z2) {
			re += abs(a[i].b1 - a[i].b2);
			continue;	
		}
		val[++cnt] = {a[i].b1 , a[i].b2};
	}
 
	sort(val + 1 , val + 1 + cnt , cmp);
 
 
	ft1 = fenwicktree(cnt * 2);
	ft2 = fenwicktree(cnt * 2);
 
 
	for(int i = 1 ; i <= n ; i++) {
		if(a[i].z1 == a[i].z2) continue;
		ft2.update(a[i].b1 , 1);
		ft2.update(a[i].b2 , 1);
	}
 
	res1 = ft1.median();
	res2 = ft2.median();
	ans2[0] = res2;

	if(k == 1) {
		cout << res2 + re + cnt;
	}
 
	for(int i = 1 ; i <= cnt ; i++) {
		pair<int , int> it = val[i];
		modify(it.fi , it.se);
 
		ans1[i] = res1;
		ans2[i] = res2;
	}
 
	if(k == 2) {
		ll ans = ans2[0];
		for(int i = 1 ; i <= cnt ; i++) {
			if(minimize(ans , ans1[i] + ans2[i]) == true) {
			}
		}
		cout << ans + re + cnt << endl;
	}
 
 
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    //freopen(TASK".inp" , "r" , stdin);
    //freopen(TASK".out" , "w" , stdout);
    
    int tc = 1;
    //cin >> tc;
    while(tc--) {
    	solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 2 ms 460 KB Output is correct
10 Correct 1 ms 472 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 472 KB Output is correct
12 Correct 106 ms 11572 KB Output is correct
13 Correct 172 ms 11456 KB Output is correct
14 Correct 116 ms 10360 KB Output is correct
15 Correct 91 ms 7120 KB Output is correct
16 Correct 104 ms 11580 KB Output is correct
17 Correct 126 ms 11564 KB Output is correct
18 Correct 146 ms 11484 KB Output is correct
19 Correct 144 ms 11640 KB Output is correct
20 Correct 97 ms 11552 KB Output is correct
21 Correct 131 ms 11572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 2 ms 460 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 2 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 2 ms 460 KB Output is correct
20 Correct 2 ms 460 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 90 ms 12204 KB Output is correct
26 Correct 94 ms 12408 KB Output is correct
27 Correct 165 ms 13160 KB Output is correct
28 Correct 160 ms 13708 KB Output is correct
29 Correct 163 ms 13716 KB Output is correct
30 Correct 85 ms 7404 KB Output is correct
31 Correct 94 ms 13104 KB Output is correct
32 Correct 114 ms 13768 KB Output is correct
33 Correct 123 ms 13316 KB Output is correct
34 Correct 155 ms 13712 KB Output is correct
35 Correct 99 ms 13248 KB Output is correct
36 Correct 132 ms 13440 KB Output is correct
37 Correct 97 ms 12152 KB Output is correct