Submission #99608

# Submission time Handle Problem Language Result Execution time Memory
99608 2019-03-05T15:33:28 Z ae04071 Palembang Bridges (APIO15_bridge) C++11
100 / 100
311 ms 11580 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define sz(x) ((int)(x).size())
using namespace std;
using lli = long long;
using pii = pair<int,int>;

int k,n;
vector<pii> arr;
lli ta[100001];

const int MAX=1<<18;
struct seg_tr{
    int cnt[MAX<<1];
    lli val[MAX<<1];
    
    void init() {for(int i=0;i<MAX+MAX;i++) val[i]=cnt[i]=0;}
    void upd(int cur,int s,int f,int idx,int v) {
        if(f<idx || s>idx) return;
        else if(s==f) {
            cnt[cur]++; val[cur]+=v;
        }
        else {
            int nx=cur<<1,md=(s+f)>>1;
            upd(nx,s,md,idx,v); upd(nx+1,md+1,f,idx,v);
            cnt[cur] = cnt[nx] + cnt[nx+1];
            val[cur] = val[nx] + val[nx+1];
        }
    }
    int kth(int cur,int s,int f,int k) {
        if(s==f) return s;
        else {
            int nx=cur<<1,md=(s+f)>>1;
            if(cnt[nx] >= k) return kth(nx,s,md,k);
            else return kth(nx+1,md+1,f,k-cnt[nx]);
        }
    }
    lli get(int cur,int s,int f,int idx,int v) {
        if(s==f) return 0;
        else {
            int nx=cur<<1,md=(s+f)>>1;
            if(idx<=md) return val[nx+1] - 1ll*cnt[nx+1]*v + get(nx,s,md,idx,v);
            else return 1ll*cnt[nx]*v - val[nx] + get(nx+1,md+1,f,idx,v);
        }
    }
}seg_tr;

lli solve() {
    vector<int> pa;
    n = sz(arr);
    if(!n) return 0;
    for(int i=0;i<n;i++) pa.push_back(arr[i].fi), pa.push_back(arr[i].se);

    sort(arr.begin(),arr.end(),[](const pii &a,const pii &b) {
        return a.fi+a.se < b.fi+b.se;
    });

    sort(pa.begin(),pa.end());
    pa.erase(unique(pa.begin(),pa.end()),pa.end());

    lli ret = 1000000000000000000ll;
    seg_tr.init();
    for(int i=0;i<n;i++) {
        int i1 = lower_bound(pa.begin(),pa.end(),arr[i].fi) - pa.begin(),
            i2 = lower_bound(pa.begin(),pa.end(),arr[i].se) - pa.begin();
        seg_tr.upd(1,0,MAX-1,i1,arr[i].fi);
        seg_tr.upd(1,0,MAX-1,i2,arr[i].se);
       
        int mi = seg_tr.kth(1,0,MAX-1,i+1);
        ta[i] = seg_tr.get(1,0,MAX-1,mi,pa[mi]);
    }
    if(k==1) return ta[n-1];
    seg_tr.init();
    for(int i=n-1;i>=0;i--) {
        int mi = seg_tr.kth(1,0,MAX-1,n-i-1);
        if(i==n-1) mi=0;
        ret = min(ret, ta[i] + seg_tr.get(1,0,MAX-1,mi,pa[mi]));

        int i1 = lower_bound(pa.begin(),pa.end(),arr[i].fi) - pa.begin(),
            i2 = lower_bound(pa.begin(),pa.end(),arr[i].se) - pa.begin();
        seg_tr.upd(1,0,MAX-1,i1,arr[i].fi);
        seg_tr.upd(1,0,MAX-1,i2,arr[i].se);
    }
    return ret;
}
int main() {
    lli ans=0;
    scanf("%d%d",&k,&n);
    for(int i=0;i<n;i++) {
        int a,b;
        char ca,cb;
        scanf(" %c %d %c %d",&ca,&a,&cb,&b);
        if(a>b) swap(a,b);
        if(ca==cb) ans += b-a;
        else arr.push_back(pii(a,b));
    }
    ans += solve();
    ans += n;
    printf("%lld\n",ans);
    return 0;
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:89: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:93:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf(" %c %d %c %d",&ca,&a,&cb,&b);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 9 ms 6528 KB Output is correct
4 Correct 9 ms 6528 KB Output is correct
5 Correct 10 ms 6528 KB Output is correct
6 Correct 9 ms 6656 KB Output is correct
7 Correct 10 ms 6528 KB Output is correct
8 Correct 8 ms 6528 KB Output is correct
9 Correct 10 ms 6572 KB Output is correct
10 Correct 8 ms 6528 KB Output is correct
11 Correct 10 ms 6528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 11 ms 6528 KB Output is correct
4 Correct 9 ms 6528 KB Output is correct
5 Correct 10 ms 6528 KB Output is correct
6 Correct 8 ms 6528 KB Output is correct
7 Correct 10 ms 6508 KB Output is correct
8 Correct 8 ms 6528 KB Output is correct
9 Correct 10 ms 6528 KB Output is correct
10 Correct 9 ms 6528 KB Output is correct
11 Correct 10 ms 6528 KB Output is correct
12 Correct 86 ms 9200 KB Output is correct
13 Correct 174 ms 9364 KB Output is correct
14 Correct 108 ms 8936 KB Output is correct
15 Correct 113 ms 8232 KB Output is correct
16 Correct 90 ms 9168 KB Output is correct
17 Correct 107 ms 9064 KB Output is correct
18 Correct 98 ms 9192 KB Output is correct
19 Correct 125 ms 9324 KB Output is correct
20 Correct 95 ms 9192 KB Output is correct
21 Correct 114 ms 9048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 12 ms 6528 KB Output is correct
4 Correct 9 ms 6528 KB Output is correct
5 Correct 10 ms 6528 KB Output is correct
6 Correct 11 ms 6528 KB Output is correct
7 Correct 12 ms 6528 KB Output is correct
8 Correct 11 ms 6528 KB Output is correct
9 Correct 11 ms 6528 KB Output is correct
10 Correct 10 ms 6528 KB Output is correct
11 Correct 9 ms 6548 KB Output is correct
12 Correct 9 ms 6528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 9 ms 6476 KB Output is correct
4 Correct 11 ms 6528 KB Output is correct
5 Correct 9 ms 6528 KB Output is correct
6 Correct 10 ms 6528 KB Output is correct
7 Correct 9 ms 6528 KB Output is correct
8 Correct 10 ms 6500 KB Output is correct
9 Correct 10 ms 6528 KB Output is correct
10 Correct 10 ms 6528 KB Output is correct
11 Correct 11 ms 6528 KB Output is correct
12 Correct 10 ms 6528 KB Output is correct
13 Correct 11 ms 6528 KB Output is correct
14 Correct 11 ms 6528 KB Output is correct
15 Correct 13 ms 6528 KB Output is correct
16 Correct 9 ms 6528 KB Output is correct
17 Correct 13 ms 6664 KB Output is correct
18 Correct 10 ms 6528 KB Output is correct
19 Correct 13 ms 6556 KB Output is correct
20 Correct 10 ms 6528 KB Output is correct
21 Correct 10 ms 6528 KB Output is correct
22 Correct 11 ms 6528 KB Output is correct
23 Correct 11 ms 6560 KB Output is correct
24 Correct 11 ms 6528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 356 KB Output is correct
2 Correct 3 ms 540 KB Output is correct
3 Correct 11 ms 6648 KB Output is correct
4 Correct 9 ms 6528 KB Output is correct
5 Correct 9 ms 6528 KB Output is correct
6 Correct 11 ms 6528 KB Output is correct
7 Correct 10 ms 6528 KB Output is correct
8 Correct 11 ms 6528 KB Output is correct
9 Correct 9 ms 6528 KB Output is correct
10 Correct 10 ms 6528 KB Output is correct
11 Correct 29 ms 6528 KB Output is correct
12 Correct 9 ms 6528 KB Output is correct
13 Correct 11 ms 6528 KB Output is correct
14 Correct 10 ms 6528 KB Output is correct
15 Correct 11 ms 6528 KB Output is correct
16 Correct 11 ms 6532 KB Output is correct
17 Correct 10 ms 6528 KB Output is correct
18 Correct 10 ms 6528 KB Output is correct
19 Correct 11 ms 6528 KB Output is correct
20 Correct 11 ms 6572 KB Output is correct
21 Correct 10 ms 6560 KB Output is correct
22 Correct 11 ms 6528 KB Output is correct
23 Correct 10 ms 6528 KB Output is correct
24 Correct 12 ms 6528 KB Output is correct
25 Correct 122 ms 9832 KB Output is correct
26 Correct 110 ms 9932 KB Output is correct
27 Correct 311 ms 10732 KB Output is correct
28 Correct 275 ms 11420 KB Output is correct
29 Correct 270 ms 11580 KB Output is correct
30 Correct 135 ms 9072 KB Output is correct
31 Correct 128 ms 10648 KB Output is correct
32 Correct 188 ms 11364 KB Output is correct
33 Correct 131 ms 10860 KB Output is correct
34 Correct 177 ms 11324 KB Output is correct
35 Correct 123 ms 10968 KB Output is correct
36 Correct 168 ms 11076 KB Output is correct
37 Correct 117 ms 9804 KB Output is correct