답안 #205470

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
205470 2020-02-29T01:24:46 Z anonymous Palembang Bridges (APIO15_bridge) C++14
100 / 100
1115 ms 204152 KB
#include<iostream>
#include<utility>
#include<algorithm>
#define MAXN 100005
#define MAXD ((int) 1e9+5)
#define LL long long
#define fi first
#define se second
using namespace std;
int N, K, P, xi, yi;
LL ans, best=1LL<<60;
char s1, s2;
pair <LL, LL> Pt[MAXN];
class LazyCreate {
    LL Sum[MAXN * 140];
    int ST[MAXN*140][3], P=2; //sum, num, l, r
public:
    void upd(int ind, int val, bool b, int l, int r, int cur) {
         if (l > ind || ind > r) {return;}
         if (l == r) {
            if (b) { //true means add
                Sum[cur]+=val;
                ST[cur][0]++;
            } else {
                Sum[cur]-=val;
                ST[cur][0]--;
            }
         } else {
            int mid=(l+r)>>1;
            if (!ST[cur][1]) {
                ST[cur][1]=P, ST[cur][2]=P+1, P+=2;
            }
            upd(ind, val, b, l, mid, ST[cur][1]);
            upd(ind, val, b, mid+1, r, ST[cur][2]);
            Sum[cur]=Sum[ST[cur][1]] + Sum[ST[cur][2]];
            ST[cur][0]=ST[ST[cur][1]][0] + ST[ST[cur][2]][0];
         }
    }
    pair<LL,LL> ask(int lo, int hi, int l, int r, int cur) {
         if (hi < l || lo > r || cur == 0) {
            return(pair<LL,LL> {0,0});
         } else if (lo <= l && hi >= r) {
            return(pair<LL,LL> {Sum[cur], ST[cur][0]});
         } else {
            int mid = (l+r)>>1;
            pair <LL,LL> res1 = ask(lo, hi, l, mid, ST[cur][1]);
            pair <LL,LL> res2 = ask(lo, hi, mid+1, r, ST[cur][2]);
            return(pair<LL,LL> {res1.fi + res2.fi, res1.se + res2.se});
         }
    }
    LL kth(int k, int l, int r, int cur) { //zero indexed
        if (l == r) {return(l);}
        int mid = (l + r)>>1;
        if (ST[ST[cur][1]][0] > k) {
            return(kth(k, l, mid, ST[cur][1]));
        } else {
            return(kth(k - ST[ST[cur][1]][0], mid+1, r, ST[cur][2]));
        }
    }
    LL Cost() {
        if (!ST[1][0]) {return(0);}
        LL med = kth(ST[1][0]/2, 0, MAXD, 1);
        pair<LL, LL> res1 = ask(0, med, 0, MAXD, 1);
        pair<LL, LL> res2 = ask(med+1, MAXD, 0, MAXD, 1);
        return(res1.se*med - res1.fi + res2.fi - res2.se*med);
    }
} Tl, Tr;

int main() {
    //freopen("palin.txt","r",stdin);
    scanf("%d %d",&K,&N);
    for (int i=0; i<N; i++) {
        scanf("\n%c %d %c %d", &s1, &xi, &s2, &yi);
        if (s1 == s2) {
            ans += abs(xi-yi);
        } else {
            ans+=1;
            Pt[P] = {xi,yi};
            P++;
        }
    }
    sort(Pt, Pt+P, [](pair<LL,LL> a, pair<LL,LL> b) {return(a.fi + a.se < b.fi + b.se);});
    for (int i=0; i<P; i++) {
            Tr.upd(Pt[i].fi, Pt[i].fi, true, 0, MAXD, 1);
            Tr.upd(Pt[i].se, Pt[i].se, true, 0, MAXD, 1);
    }
    if (K == 1) {
        printf("%lld", Tr.Cost() + ans);
    } else {
        best = Tr.Cost();
        for (int i=0; i<P; i++) {
            Tr.upd(Pt[i].fi, Pt[i].fi, false, 0, MAXD, 1);
            Tr.upd(Pt[i].se, Pt[i].se, false, 0, MAXD, 1);
            Tl.upd(Pt[i].fi, Pt[i].fi, true, 0, MAXD, 1);
            Tl.upd(Pt[i].se, Pt[i].se, true, 0, MAXD, 1);
            best = min(best, Tr.Cost() + Tl.Cost());
        }
        printf("%lld", best + ans);
    }
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:71:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&K,&N);
     ~~~~~^~~~~~~~~~~~~~~
bridge.cpp:73:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("\n%c %d %c %d", &s1, &xi, &s2, &yi);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 6 ms 376 KB Output is correct
4 Correct 7 ms 1016 KB Output is correct
5 Correct 7 ms 1784 KB Output is correct
6 Correct 6 ms 376 KB Output is correct
7 Correct 8 ms 1912 KB Output is correct
8 Correct 6 ms 376 KB Output is correct
9 Correct 7 ms 1272 KB Output is correct
10 Correct 6 ms 376 KB Output is correct
11 Correct 6 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 280 KB Output is correct
3 Correct 6 ms 376 KB Output is correct
4 Correct 7 ms 1144 KB Output is correct
5 Correct 7 ms 1784 KB Output is correct
6 Correct 6 ms 376 KB Output is correct
7 Correct 8 ms 1912 KB Output is correct
8 Correct 6 ms 376 KB Output is correct
9 Correct 6 ms 1272 KB Output is correct
10 Correct 6 ms 376 KB Output is correct
11 Correct 6 ms 504 KB Output is correct
12 Correct 101 ms 1912 KB Output is correct
13 Correct 338 ms 99296 KB Output is correct
14 Correct 110 ms 2168 KB Output is correct
15 Correct 199 ms 62180 KB Output is correct
16 Correct 105 ms 2040 KB Output is correct
17 Correct 204 ms 101900 KB Output is correct
18 Correct 115 ms 9720 KB Output is correct
19 Correct 179 ms 65656 KB Output is correct
20 Correct 105 ms 1912 KB Output is correct
21 Correct 125 ms 11128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 6 ms 632 KB Output is correct
5 Correct 5 ms 504 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 760 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 504 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 6 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 632 KB Output is correct
5 Correct 5 ms 504 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 6 ms 760 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 6 ms 632 KB Output is correct
11 Correct 6 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 8 ms 376 KB Output is correct
14 Correct 9 ms 376 KB Output is correct
15 Correct 12 ms 3320 KB Output is correct
16 Correct 6 ms 376 KB Output is correct
17 Correct 7 ms 376 KB Output is correct
18 Correct 7 ms 1016 KB Output is correct
19 Correct 8 ms 376 KB Output is correct
20 Correct 12 ms 3448 KB Output is correct
21 Correct 8 ms 504 KB Output is correct
22 Correct 12 ms 2168 KB Output is correct
23 Correct 8 ms 376 KB Output is correct
24 Correct 8 ms 632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 6 ms 632 KB Output is correct
5 Correct 5 ms 504 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 6 ms 376 KB Output is correct
8 Correct 6 ms 632 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 632 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 376 KB Output is correct
13 Correct 9 ms 376 KB Output is correct
14 Correct 9 ms 504 KB Output is correct
15 Correct 12 ms 3320 KB Output is correct
16 Correct 6 ms 376 KB Output is correct
17 Correct 7 ms 376 KB Output is correct
18 Correct 7 ms 1016 KB Output is correct
19 Correct 8 ms 376 KB Output is correct
20 Correct 12 ms 3576 KB Output is correct
21 Correct 8 ms 504 KB Output is correct
22 Correct 14 ms 2168 KB Output is correct
23 Correct 8 ms 376 KB Output is correct
24 Correct 9 ms 632 KB Output is correct
25 Correct 347 ms 2040 KB Output is correct
26 Correct 345 ms 1912 KB Output is correct
27 Correct 758 ms 43280 KB Output is correct
28 Correct 1114 ms 198904 KB Output is correct
29 Correct 1115 ms 198904 KB Output is correct
30 Correct 550 ms 112976 KB Output is correct
31 Correct 328 ms 3704 KB Output is correct
32 Correct 589 ms 204152 KB Output is correct
33 Correct 359 ms 19576 KB Output is correct
34 Correct 516 ms 131704 KB Output is correct
35 Correct 330 ms 3960 KB Output is correct
36 Correct 383 ms 22456 KB Output is correct
37 Correct 324 ms 2680 KB Output is correct