Submission #887939

# Submission time Handle Problem Language Result Execution time Memory
887939 2023-12-15T14:35:40 Z Kutan Palembang Bridges (APIO15_bridge) C++14
100 / 100
141 ms 12924 KB
// Cao Quang Hung
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
 
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pii pair<int,int>
#define pll pair<long long , long long>
#define vi vector<int>
#define vpii vector<pii>
#define SZ(x) ((int)(x.size()))
#define fi first
#define se second
#define IN(x,y) ((y).find((x))!=(y).end())
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define FOR(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define dem(x) __builtin_popcount(x)
#define Mask(x) (1LL << (x))
#define BIT(x, i) ((x) >> (i) & 1)
#define ln '\n'
#define io_faster ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
///mt19937 rnd(time(0));
 
const int INF = 1e9 , mod = 1e9 + 7;
 
template <class T1, class T2>
inline T1 mul(T1& x, const T2 &k){ return x = (1LL * x * k) % mod; }
 
template <class T1 , class T2>
inline T1 pw(T1 x, T2 k){T1 res = 1; for (; k ; k >>= 1){ if (k & 1) mul(res, x); mul(x, x); } return res;}
 
template <class T>
inline bool minimize(T &x, const T &y){ if (x > y){x = y; return 1;} return 0; }
 
template <class T>
inline bool maximize(T &x, const T &y){ if (x < y){x = y; return 1;} return 0; }
 
template <class T>
inline void add(T &x , const T &y){ if ((x += y) >= mod) x -= mod; }
 
template <class T>
inline T product (const T &x , const T &y) { return 1LL * x * y % mod; }
 
#define PROB "a"
void file(){
    if(fopen(PROB".inp", "r")){
        freopen(PROB".inp","r",stdin);
        freopen(PROB".out","w",stdout);
    }
}
void sinh_(){
//    srand(time(0));
//    freopen(PROB".inp" , "w" , stdout);
//    int n;
}
 
typedef long long ll;
typedef double db;
const int N = 2e5 + 5;
int k, n;
ll Ans = 0;
pii segment[N];
int numseg = 0, numpoint;
vector<int> compress;
 
void readip(){
    cin >> k >> n;
    REP(i, 1, n) {
        char a, b; int l, r;
        cin >> a >> l >> b >> r;
        Ans += abs(r - l);
        if (a == b) continue;
        ++Ans;
        if (l > r) swap(l, r);
        segment[++numseg] = {l, r};
        compress.eb(l);
        compress.eb(r);
    }
    sort(ALL(compress));
    compress.erase(unique(ALL(compress)), compress.end());
    numpoint = compress.size();
 
    if (numpoint == 0) {
        cout << Ans << ln;
        exit(false);
    }
 
    sort(segment + 1, segment + numseg + 1);
    REP(i, 1, numseg) {
        segment[i].first = lower_bound(ALL(compress), segment[i].first) - compress.begin() + 1;
        segment[i].second = lower_bound(ALL(compress), segment[i].second) - compress.begin() + 1;
    }
 
}
 
namespace sub1 {
    bool check() {
        return k == 1;
    }
 
    vector<pair<ll, int>> pref, suf;
    void solve() {
        pref.assign(numpoint + 2, {0, 0});
        suf.assign(numpoint + 2, {0, 0});
        REP(i, 1, numseg) {
            pref[segment[i].second].first += compress[segment[i].second - 1];
            pref[segment[i].second].second++;
 
            suf[segment[i].first].first += compress[segment[i].first - 1];
            suf[segment[i].first].second++;
        }
 
        REP(i, 1, numpoint)  {
            pref[i].first += pref[i - 1].first;
            pref[i].second += pref[i - 1].second;
        }
 
        REPD(i, numpoint - 1, 1) {
            suf[i].first += suf[i + 1].first;
            suf[i].second += suf[i + 1].second;
        }
 
 
        ll res = 1e18;
        REP(i, 1, numpoint) {
            int x = compress[i - 1];
            ll tmp = 1LL * x * pref[i].second - pref[i].first
                    + suf[i].first - 1LL * x * suf[i].second;
            tmp *= 2;
            minimize(res, tmp);
        }
        cout << Ans + res << ln;
    }
};
 

namespace subfull {
    vector<int> lef_median, rig_median;
    vector<ll> totLef, totRig;

    struct segtree {
        vector<int> it;
        segtree(int sz = 0) {
            if (sz > 0)
                it.assign(4 * sz + 7, 0);
        }

        void upd(int i, int inc, int id = 1, int l = 1, int r= numpoint) {
            while(1) {
                it[id] += inc;
                if (l == r) return;
                int mid = (l + r) >> 1;
                if (i <= mid) id = id << 1, r = mid;
                else id = id << 1 | 1, l = mid + 1;
            }
        }

        int walk(int val, int id = 1, int l = 1, int r = numpoint) {
            while(1) {
                if (l == r) return l;
                int mid = (l + r) >> 1;
                if (val > it[id << 1]) {
                    val -= it[id << 1];
                    id = id << 1 | 1;
                    l = mid + 1;
                }
                else {
                    id = id << 1;
                    r = mid;
                }
            }
        }
    } ST;


    struct fenwick{
        vector<pair<ll, int>> fen;
        fenwick(int sz = 0) {
            if (sz > 0)
                fen.assign(sz + 7, {0LL, 0});
        }

        void upd(int i, pair<int, int> val) {
            for (; i <= numpoint; i += i & -i)
                fen[i].fi += val.fi , fen[i].se += val.se;
        }

        pair<ll, int> get(int i) {
            pair<ll, int> res(0LL, 0);
            for (; i > 0; i -= i & -i) {
                res.fi += fen[i].fi;
                res.se += fen[i].se;
            }
            return res;
        }
    } Fen;

    void solve() {
        lef_median.assign(numseg + 2, 0);
        rig_median.assign(numseg + 2, 0);
        totLef.assign(numseg + 2, 0LL);
        totRig.assign(numseg + 2, 0LL);

        ST = segtree(numpoint);
        Fen = fenwick(numpoint);    
        
        REP(i, 1, numseg) {
            Fen.upd(segment[i].fi, {compress[segment[i].fi - 1], +1});
            Fen.upd(segment[i].se, {compress[segment[i].se - 1], +1});
            
            ST.upd(segment[i].fi, +1);
            ST.upd(segment[i].se, +1);
            
            lef_median[i] = ST.walk(i);
            pair<ll, int> tmp = Fen.get(lef_median[i]);
            totLef[i] += 1LL * (compress[lef_median[i] - 1]) * tmp.se - tmp.fi;

            pair<ll, int> temp = Fen.get(numpoint);
            temp.fi -= tmp.fi;
            temp.se -= tmp.se;

            totLef[i] += temp.fi - 1LL * (compress[lef_median[i] - 1]) * temp.se;
        }

        REP(i, 1, numseg) {
            ST.upd(segment[i].fi, -1);
            ST.upd(segment[i].se, -1);
            
            Fen.upd(segment[i].fi, {-compress[segment[i].fi - 1], -1});
            Fen.upd(segment[i].se, {-compress[segment[i].se - 1], -1});
        }

        REPD(i, numseg, 1) {
            Fen.upd(segment[i].fi, {compress[segment[i].fi - 1], +1});
            Fen.upd(segment[i].se, {compress[segment[i].se - 1], +1});
            ST.upd(segment[i].fi, +1);
            ST.upd(segment[i].se, +1);
            rig_median[i] = ST.walk(numseg - i + 1);

            pair<ll, int> tmp = Fen.get(rig_median[i] - 1);
            totRig[i] += 1LL * compress[rig_median[i] - 1] * tmp.se - tmp.fi;
            pair<ll, int> temp = Fen.get(numpoint);
            temp.fi -= tmp.fi;
            temp.se -= tmp.se;
            totRig[i] += temp.fi - 1LL * (compress[rig_median[i] - 1]) * temp.se;
        }

        ll res = 1e18;
        REP(i, 1, numseg - 1) 
            minimize(res, totLef[i] + totRig[i + 1]);
        cout << Ans + res << ln;
    }   
};

void solve(){       
    if (sub1::check()) {
        sub1::solve();
        return;
    }

    if (numpoint <= 2) {
        cout << Ans << ln;
        return;
    }
    
    REP(i, 1, numseg) Ans -= compress[segment[i].se - 1] - compress[segment[i].fi - 1];
    sort(segment + 1, segment + numseg + 1, [&] (const pii &x, const pii &y) {
        return compress[x.fi - 1] + compress[x.se - 1] < compress[y.fi - 1] + compress[y.se - 1];
    });

    subfull::solve();

}
 
int main(){
    sinh_();
    io_faster
    file();
    int t = 1;
//    cin >> t;
    while (t--){
        readip();
        solve();
    }
}

Compilation message

bridge.cpp: In function 'void readip()':
bridge.cpp:92:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
bridge.cpp:146:5: note: in expansion of macro 'REP'
  146 |     REP(i, 1, n) {
      |     ^~~
bridge.cpp:92:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
bridge.cpp:167:5: note: in expansion of macro 'REP'
  167 |     REP(i, 1, numseg) {
      |     ^~~
bridge.cpp: In function 'void sub1::solve()':
bridge.cpp:92:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
bridge.cpp:183:9: note: in expansion of macro 'REP'
  183 |         REP(i, 1, numseg) {
      |         ^~~
bridge.cpp:92:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
bridge.cpp:191:9: note: in expansion of macro 'REP'
  191 |         REP(i, 1, numpoint)  {
      |         ^~~
bridge.cpp:93:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   93 | #define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
      |                             ^
bridge.cpp:196:9: note: in expansion of macro 'REPD'
  196 |         REPD(i, numpoint - 1, 1) {
      |         ^~~~
bridge.cpp:92:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
bridge.cpp:203:9: note: in expansion of macro 'REP'
  203 |         REP(i, 1, numpoint) {
      |         ^~~
bridge.cpp: In function 'void subfull::solve()':
bridge.cpp:92:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
bridge.cpp:285:9: note: in expansion of macro 'REP'
  285 |         REP(i, 1, numseg) {
      |         ^~~
bridge.cpp:92:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
bridge.cpp:303:9: note: in expansion of macro 'REP'
  303 |         REP(i, 1, numseg) {
      |         ^~~
bridge.cpp:93:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   93 | #define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
      |                             ^
bridge.cpp:311:9: note: in expansion of macro 'REPD'
  311 |         REPD(i, numseg, 1) {
      |         ^~~~
bridge.cpp:92:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
bridge.cpp:327:9: note: in expansion of macro 'REP'
  327 |         REP(i, 1, numseg - 1)
      |         ^~~
bridge.cpp: In function 'void solve()':
bridge.cpp:92:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
bridge.cpp:344:5: note: in expansion of macro 'REP'
  344 |     REP(i, 1, numseg) Ans -= compress[segment[i].se - 1] - compress[segment[i].fi - 1];
      |     ^~~
bridge.cpp: In function 'void file()':
bridge.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |         freopen(PROB".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:126:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  126 |         freopen(PROB".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 372 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 23 ms 1952 KB Output is correct
13 Correct 57 ms 8400 KB Output is correct
14 Correct 39 ms 2008 KB Output is correct
15 Correct 32 ms 5084 KB Output is correct
16 Correct 19 ms 2156 KB Output is correct
17 Correct 35 ms 8400 KB Output is correct
18 Correct 37 ms 8408 KB Output is correct
19 Correct 48 ms 7884 KB Output is correct
20 Correct 30 ms 2140 KB Output is correct
21 Correct 45 ms 8396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 2 ms 500 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 2 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 436 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 18 ms 2780 KB Output is correct
26 Correct 45 ms 5320 KB Output is correct
27 Correct 140 ms 11648 KB Output is correct
28 Correct 138 ms 12752 KB Output is correct
29 Correct 141 ms 12924 KB Output is correct
30 Correct 71 ms 7080 KB Output is correct
31 Correct 19 ms 3688 KB Output is correct
32 Correct 90 ms 12752 KB Output is correct
33 Correct 95 ms 12580 KB Output is correct
34 Correct 108 ms 12676 KB Output is correct
35 Correct 30 ms 6100 KB Output is correct
36 Correct 113 ms 12488 KB Output is correct
37 Correct 24 ms 5076 KB Output is correct