제출 #621458

#제출 시각아이디문제언어결과실행 시간메모리
621458arayiPalembang Bridges (APIO15_bridge)C++17
100 / 100
649 ms24312 KiB
// Arayi #include <bits/stdc++.h> #define fr first #define sc second #define MP make_pair #define ad push_back #define PB push_back #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define lli long long int #define y1 arayikhalatyan #define j1 jigglypuff #define ld long double #define itn int #define pir pair<int, int> #define all(x) (x).begin(), (x).end() #define str string #define enl endl #define en endl #define cd complex<long double> #define vcd vector<cd> #define vii vector<int> #define vlli vector<lli> using namespace std; lli gcd(lli a, lli b) { return (b == 0LL ? a : gcd(b, a % b)); } ld dist(ld x, ld y1, ld x2, ld y2) { return sqrt((x - x2) * (x - x2) + (y1 - y2) * (y1 - y2)); } lli S(lli a) { return (a * (a + 1LL)) / 2; } mt19937 rnd(363542); char vow[] = {'a', 'e', 'i', 'o', 'u'}; int dx[] = {0, -1, 0, 1, -1, -1, 1, 1, 0}; int dy[] = {-1, 0, 1, 0, -1, 1, -1, 1, 0}; char dc[] = {'R', 'D', 'L', 'U'}; const int N = 1e6 + 20; const lli mod = 998244353; const ld pi = acos(-1); const ld e = 1e-13; const int T = 128; lli bp(lli a, lli b = mod - 2LL) { lli ret = 1; while (b) { if (b & 1) ret *= a, ret %= mod; a *= a; a %= mod; b >>= 1; } return ret; } ostream &operator<<(ostream &c, pir a) { c << a.fr << " " << a.sc; return c; } template <class T> void maxi(T &a, T b) { a = max(a, b); } template <class T> void mini(T &a, T b) { a = min(a, b); } int n, k; lli ans; vector<pair<int, pir>> fp; lli pr[N], sf[N]; lli t[N]; void upd(int q, lli v, int nl = 0, int nr = 2 * n, int nd = 1) { if (q > nr || q < nl) return; if (nl == nr) { t[nd] += v; return; } int md = (nl + nr) / 2; upd(q, v, nl, md, nd * 2); upd(q, v, md + 1, nr, nd * 2 + 1); t[nd] = t[nd * 2] + t[nd * 2 + 1]; } lli qry(int l, int r, int nl = 0, int nr = 2 * n, int nd = 1) { if (l > nr || r < nl) return 0; if (l == nl && r == nr) return t[nd]; int md = (nl + nr) / 2; return qry(l, min(md, r), nl, md, nd * 2) + qry(max(md + 1, l), r, md + 1, nr, nd * 2 + 1); } int main() { fastio; cin >> k >> n; vector<pir> s; for (int i = 0; i < n; i++) { int l, r; char A, B; cin >> A >> l >> B >> r; if (l > r) swap(l, r); if (A == B) ans += r - l; else { ans++; fp.ad(MP(l + r, MP(l, i))); s.ad(MP(l, 2 * i)); s.ad(MP(r, 2 * i + 1)); } } sort(all(s)); sort(all(fp)); multiset<int> a, b; for (int i = 0; i < (int)fp.size(); i++) { int l = fp[i].sc.fr, r = fp[i].fr - fp[i].sc.fr; int l1 = upper_bound(all(s), MP(l, 2 * fp[i].sc.sc)) - s.begin() - 1; int r1 = upper_bound(all(s), MP(r, 2 * fp[i].sc.sc + 1)) - s.begin() - 1; // cout << l << " " << r << "-"; upd(l1, l), upd(r1, r); if (i == 0) a.insert(l1), b.insert(r1); else { if (r1 < *b.begin()) a.insert(r1); else b.insert(r1); if (l1 < *b.begin()) a.insert(l1); else b.insert(l1); } if (a.size() < b.size()) { a.insert(*b.begin()); b.erase(b.begin()); } else if (a.size() > b.size()) { b.insert(*(--a.end())); a.erase(--a.end()); } int i1 = *b.begin(); // cout << i1 << "-"; pr[i] = qry(i1, 2 * n) - qry(0, i1 - 1); // cout << pr[i] << endl; } memset(t, 0, sizeof(t)); a.clear(), b.clear(); for (int i = (int)fp.size() - 1; i >= 0; i--) { int l = fp[i].sc.fr, r = fp[i].fr - fp[i].sc.fr; int l1 = upper_bound(all(s), MP(l, 2 * fp[i].sc.sc)) - s.begin() - 1; int r1 = upper_bound(all(s), MP(r, 2 * fp[i].sc.sc + 1)) - s.begin() - 1; // cout << l << " " << r << "-"; upd(l1, l), upd(r1, r); if (i == 0) a.insert(l1), b.insert(r1); else { if (r1 < *b.begin()) a.insert(r1); else b.insert(r1); if (l1 < *b.begin()) a.insert(l1); else b.insert(l1); } if (a.size() < b.size()) { a.insert(*b.begin()); b.erase(b.begin()); } else if (a.size() > b.size()) { b.insert(*(--a.end())); a.erase(--a.end()); } int i1 = *b.begin(); // cout << i1 << "-"; sf[i] = qry(i1, 2 * n) - qry(0, i1 - 1); } lli sm = pr[(int)fp.size() - 1]; if (k == 2) { for (int i = 0; i < (int)fp.size() - 1; i++) mini(sm, pr[i] + sf[i + 1]); } ans += sm; cout << ans << endl; return 0; } /* __ *(><)* \/ / ||/ --|| || /\ / \ / \ */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...