이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
int k, n;
ll ans = 0;
array<ll, 2> a[N];
pair<ll, ll> pref[N], suf[N];
array<ll, 2> TL[N], TR[N];
vector<pair<ll, int>> v;
void build(int m){
for(int i = 0; i <= 4*m; ++i) TL[i] = {0, 0}, TR[i] = {0, 0};
}
void updateL(int l, int r, int p, int k, ll val){
if(l == r){
TL[k] = {1, val};
return;
}
int m = l+r>>1;
if(p <= m) updateL(l, m, p, k<<1, val);
else updateL(m+1, r, p, k<<1|1, val);
TL[k][0] = TL[k<<1][0] + TL[k<<1|1][0];
TL[k][1] = TL[k<<1][1] + TL[k<<1|1][1];
}
void updateR(int l, int r, int p, int k, ll val){
if(l == r){
TR[k] = {1, val};
return;
}
int m = l+r>>1;
if(p <= m) updateR(l, m, p, k<<1, val);
else updateR(m+1, r, p, k<<1|1, val);
TR[k][0] = TR[k<<1][0] + TR[k<<1|1][0];
TR[k][1] = TR[k<<1][1] + TR[k<<1|1][1];
}
array<ll, 2> getL(int l, int r, int ql, int qr, int k){
if(ql > r || l > qr) return {0, 0};
if(ql <= l && r <= qr) return TL[k];
int m = l+r>>1;
auto p = getL(l, m, ql, qr, k<<1);
auto v = getL(m+1, r, ql, qr, k<<1);
return {p[0] + v[0], p[1] + v[1]};
}
array<ll, 2> getR(int l, int r, int ql, int qr, int k){
if(ql > r || l > qr) return {0, 0};
if(ql <= l && r <= qr) return TR[k];
int m = l+r>>1;
auto p = getR(l, m, ql, qr, k<<1);
auto v = getR(m+1, r, ql, qr, k<<1);
return {p[0] + v[0], p[1] + v[1]};
}
ll get(int l, int r, int p, int k, int tot, ll sum){
if(l == r){
tot += TR[k][1];
sum -= TR[k][0];
return sum + tot * v[l - 1].first;
}
int m = l+r>>1;
if(TR[k<<1][1] + TL[k<<1][1] >= p){
return get(l, m, p, k<<1, tot - TL[k<<1|1][1], sum + TL[k<<1|1][0]);
}
return get(m+1, r, p - (TR[k<<1][1] + TL[k<<1][1]), k<<1|1, tot + TR[k<<1][1], sum - TR[k<<1][0]);
}
void solve(){
cin >> k >> n;
int c = 0;
for(int i = 1; i <= n; ++i){
char c1, c2; ll x1, x2; cin >> c1 >> x1 >> c2 >> x2;
if(c1 == c2){
ans += abs(x1-x2);
}else{
++c;
a[c][0] = min(x1, x2);
a[c][1] = max(x1, x2);
ans += abs(x1-x2);
}
}
n = c;
if(n==0){
cout << ans;
return;
}
sort(a+1, a+1+n);
if(k == 1){
vector<pair<ll, ll>> v;
for(int i = 1; i <= n; ++i) v.pb({a[i][0], 0}), v.pb({a[i][1], 1});
sort(all(v));
pref[0] = {0, 0};
for(int i = 1; i < v.size(); ++i){
pref[i] = pref[i - 1];
if(v[i].second == 1) pref[i].first += v[i].first, pref[i].second++;
}
suf[v.size() - 1] = {0, 0};
for(int i = v.size() - 2; i >= 0; --i){
suf[i] = suf[i + 1];
if(v[i].second == 0) suf[i].first += v[i].first, suf[i].second++;
}
ll best = 1e18;
for(int i = 0; i < v.size(); ++i){
if(abs(suf[i].second - pref[i].second) <= 1)
best = min(best, suf[i].first - pref[i].first + (v[i].first * pref[i].second) - (v[i].first * suf[i].second));
}
cout << ans + best*2 + n;
}else{
ll best = 1e18;
sort(a+1, a+1+n, [&](const array<ll, 2> &x, const array<ll, 2> &y){
return x[0] + x[1] < y[0] + y[1];
});
for(int i = 1; i <= n; ++i) v.pb({a[i][0], i*2}), v.pb({a[i][1], i*2+1});
sort(all(v));
vector<array<int, 2>> pos(2*n+5);
for(int i = 0; i < n*2; ++i){
pos[v[i].second / 2][v[i].second % 2] = i + 1;
}
vector<ll> PREF(n+1, 0);
build(2*n);
for(int i = 1; i <= n; ++i){
updateL(1, 2*n, pos[i][0], 1, a[i][0]);
updateR(1, 2*n, pos[i][1], 1, a[i][1]);
PREF[i] = get(1, 2*n, i, 1, 0, 0);
}
vector<ll> SUF(n+1, 0);
build(2*n);
for(int i = n; i >= 1; --i){
updateL(1, 2*n, pos[i][0], 1, a[i][0]);
updateR(1, 2*n, pos[i][1], 1, a[i][1]);
SUF[i] = get(1, 2*n, n-i+1, 1, 0, 0);
}
for(int i = 0; i < n; ++i) best=min(best,PREF[i]+SUF[i+1]);
cout << ans + 2*best + n;
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
bridge.cpp: In function 'void updateL(int, int, int, int, long long int)':
bridge.cpp:28:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
28 | int m = l+r>>1;
| ~^~
bridge.cpp: In function 'void updateR(int, int, int, int, long long int)':
bridge.cpp:39:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
39 | int m = l+r>>1;
| ~^~
bridge.cpp: In function 'std::array<long long int, 2> getL(int, int, int, int, int)':
bridge.cpp:49:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
49 | int m = l+r>>1;
| ~^~
bridge.cpp: In function 'std::array<long long int, 2> getR(int, int, int, int, int)':
bridge.cpp:58:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
58 | int m = l+r>>1;
| ~^~
bridge.cpp: In function 'long long int get(int, int, int, int, int, long long int)':
bridge.cpp:69:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
69 | int m = l+r>>1;
| ~^~
bridge.cpp: In function 'void solve()':
bridge.cpp:102:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int i = 1; i < v.size(); ++i){
| ~~^~~~~~~~~~
bridge.cpp:113:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for(int i = 0; i < v.size(); ++i){
| ~~^~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:156:15: warning: unused variable 'aa' [-Wunused-variable]
156 | int tt = 1, aa;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |