#include <bits/stdc++.h>
#define bit(i, x) (x >> i & 1)
#define ll long long
#define int long long
const int N = 3e5 + 5;
const int mod = 998244353;
using namespace std;
int k, n;
vector<int> candidate;
vector<int> X;
vector<pair<int, int>> a;
long long bonus;
struct BIT{
vector<ll> bit;
int n;
void init(int _n) {
n = _n;
bit.assign(n + 3, 0);
}
void update(int u, ll x) {
for(int i = u; i <= n; i += i & -i) bit[i] += x;
}
void rangeupdate(int u, int v, ll x) {
update(u, x);
update(v + 1, -x);
}
ll query(int u) {
ll s = 0;
for(int i = u; i > 0; i -= i & -i) s += bit[i];
return s;
}
ll query(int u, int v) {
if(u > v) return 0;
return query(v) - query(u - 1);
}
} t1, t2, t3, t4, t5;
signed main(int argc, char* argv[]) {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> k >> n;
assert(k == 1);
for(int i = 1; i <= n; i++) {
char p, q; int s, t;
cin >> p >> s >> q >> t;
if(p == q) {
bonus += abs(s - t);
continue;
}
X.push_back(s);
X.push_back(t);
a.push_back({min(s, t), max(s, t)});
}
sort(X.begin(), X.end());
X.resize(unique(X.begin(), X.end()) - X.begin());
candidate = X;
iota(candidate.begin(), candidate.end(), 0);
for(auto &[l, r] : a) {
l = lower_bound(X.begin(), X.end(), l) - X.begin();
r = lower_bound(X.begin(), X.end(), r) - X.begin();
}
t1.init(2 * n); // trai
t2.init(2 * n); // phai
t3.init(2 * n); // count trai
t4.init(2 * n); // count phai
t5.init(2 * n); // nam trong
for(auto [l, r] : a) {
// trai
t1.update(r + 1, -(X[l] + X[r]) + 1);
t3.update(r + 1, 1);
// phai
t2.update(l + 1, X[l] + X[r] + 1);
t4.update(l + 1, 1);
// nam trong
t5.rangeupdate(l + 1, r + 1, X[r] - X[l] + 1);
}
// for(int i = 0; i <= n - 1; i++) {
// cout << i << " " << t3.query(1, i) << "\n";
// }
ll res = 2e18;
// for(auto x : candidate) cout << x << " ";
// cout << "\n";
for(auto x : candidate) {
// trai
pair<ll, ll> l = make_pair(t1.query(1, x), t3.query(1, x));
// phai
pair<ll, ll> r = make_pair(t2.query(x + 2, 2 * n), t4.query(x + 2, 2 * n));
// nam trong
ll mid = t5.query(x + 1);
// cout << (l.first + 2LL * X[x] * l.second) << " ";
long long val = (mid) + (l.first + 2LL * X[x] * l.second) + (r.first - 2LL * X[x] * r.second);
res = min(res, val);
}
cout << res + bonus;
return 0 ^ 0;
}
컴파일 시 표준 에러 (stderr) 메시지
bridge.cpp:45:8: warning: first argument of 'int main(long long int, char**)' should be 'int' [-Wmain]
45 | signed main(int argc, char* argv[]) {
| ^~~~
# | 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... |