Submission #14914

# Submission time Handle Problem Language Result Execution time Memory
14914 2015-07-06T00:30:27 Z ainta Palembang Bridges (APIO15_bridge) C++
100 / 100
316 ms 22228 KB
#include<stdio.h>
#include<algorithm>
#define SZ 262144
using namespace std;
int K, n;
long long Res, SS, D1[101000], D2[101000];
struct A{
    int b, e, num;
    bool operator<(const A &p)const{
        return b+e<p.b+p.e;
    }
}w[101000];
int Num[101000];
struct AA{
    int x, num;
    bool operator<(const AA &p)const{
        return x<p.x;
    }
}ord[201000];
long long IT[SZ+SZ+1][4];
void Ins(int pv, int a, int b){
    a+=SZ;
    while(a){
        IT[a][pv]+=b;
        a>>=1;
    }
}
int Findx(int node, int g){
    if(node >= SZ)return node-SZ;
    if(IT[node*2][0] + IT[node*2][1] < g)return Findx(node*2+1,g-IT[node*2][0] - IT[node*2][1]);
    return Findx(node*2,g);
}
long long Sum(int pv, int b, int e){
    b+=SZ, e+=SZ;
    long long r = 0;
    while(b<=e){
        if(b&1)r+=IT[b][pv];
        if(!(e&1))r += IT[e][pv];
        b=(b+1)>>1, e=(e-1)>>1;
    }
    return r;
}
long long Gap(int a){
    long long t1 = Sum(0, 1, a-1), t2 = Sum(1, a+1, n+n);
    return t1*ord[a].x - Sum(2, 1, a-1) + Sum(3,a+1,n+n) - t2*ord[a].x;
}
int main(){
    int i, a, b, c = 0, e, mid, r, pv = 1;
    scanf("%d%d",&K,&n);
    char p1[3], p2[3];
    for(i=1;i<=n;i++){
        scanf("%s",p1);
        scanf("%d",&a);
        scanf("%s",p2);
        scanf("%d",&b);
        SS += max(a,b)-min(a,b);
        if(p1[0] != p2[0]){
            SS++;
            c++;
            w[c].b = min(a,b);
            w[c].e = max(a,b);
            w[c].num =c;
        }
    }
    n=c;
    sort(w+1,w+n+1);
    for(i=1;i<=n;i++){
        ord[i].x=w[i].b, ord[i].num = i;
        ord[i + n].x = w[i].e, ord[i+n].num = i+n;
    }
    sort(ord+1,ord+n+n+1);
    for(i=1;i<=n+n;i++){
        a = ord[i].num;
        if(a <= n)w[a].b = i;
        else w[a-n].e = i;
    }
    for(i=1;i<=n;i++){
        Ins(0, w[i].e, 1);
        Ins(1, w[i].b, 1);
        Ins(2, w[i].e, ord[w[i].e].x);
        Ins(3, w[i].b, ord[w[i].b].x);
        a = Findx(1, i);
        D1[i] = Gap(a);
    }
    for(i=1;i<=n;i++){
        Ins(0, w[i].e, -1);
        Ins(1, w[i].b, -1);
        Ins(2, w[i].e, -ord[w[i].e].x);
        Ins(3, w[i].b, -ord[w[i].b].x);
    }
    for(i=n;i>=1;i--){
        Ins(0, w[i].e, 1);
        Ins(1, w[i].b, 1);
        Ins(2, w[i].e, ord[w[i].e].x);
        Ins(3, w[i].b, ord[w[i].b].x);
        a = Findx(1, n+1-i);
        D2[i] = Gap(a);
    }
    Res = SS + D1[n]*2;
    if(K==1){
        printf("%lld\n",Res);
        return 0;
    }
    for(i=1;i<n;i++){
        Res = min(Res, SS + (D1[i] + D2[i+1])*2);
    }
    printf("%lld\n",Res);
    return 0;
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:48:25: warning: unused variable 'e' [-Wunused-variable]
     int i, a, b, c = 0, e, mid, r, pv = 1;
                         ^
bridge.cpp:48:28: warning: unused variable 'mid' [-Wunused-variable]
     int i, a, b, c = 0, e, mid, r, pv = 1;
                            ^
bridge.cpp:48:33: warning: unused variable 'r' [-Wunused-variable]
     int i, a, b, c = 0, e, mid, r, pv = 1;
                                 ^
bridge.cpp:48:36: warning: unused variable 'pv' [-Wunused-variable]
     int i, a, b, c = 0, e, mid, r, pv = 1;
                                    ^
bridge.cpp:49:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&K,&n);
                        ^
bridge.cpp:52:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%s",p1);
                       ^
bridge.cpp:53:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&a);
                       ^
bridge.cpp:54:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%s",p2);
                       ^
bridge.cpp:55:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&b);
                       ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 22228 KB Output is correct
2 Correct 0 ms 22228 KB Output is correct
3 Correct 0 ms 22228 KB Output is correct
4 Correct 0 ms 22228 KB Output is correct
5 Correct 0 ms 22228 KB Output is correct
6 Correct 0 ms 22228 KB Output is correct
7 Correct 0 ms 22228 KB Output is correct
8 Correct 0 ms 22228 KB Output is correct
9 Correct 0 ms 22228 KB Output is correct
10 Correct 0 ms 22228 KB Output is correct
11 Correct 0 ms 22228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 22228 KB Output is correct
2 Correct 0 ms 22228 KB Output is correct
3 Correct 0 ms 22228 KB Output is correct
4 Correct 0 ms 22228 KB Output is correct
5 Correct 3 ms 22228 KB Output is correct
6 Correct 0 ms 22228 KB Output is correct
7 Correct 0 ms 22228 KB Output is correct
8 Correct 0 ms 22228 KB Output is correct
9 Correct 3 ms 22228 KB Output is correct
10 Correct 0 ms 22228 KB Output is correct
11 Correct 0 ms 22228 KB Output is correct
12 Correct 189 ms 22228 KB Output is correct
13 Correct 286 ms 22228 KB Output is correct
14 Correct 233 ms 22228 KB Output is correct
15 Correct 96 ms 22228 KB Output is correct
16 Correct 136 ms 22228 KB Output is correct
17 Correct 176 ms 22228 KB Output is correct
18 Correct 133 ms 22228 KB Output is correct
19 Correct 206 ms 22228 KB Output is correct
20 Correct 183 ms 22228 KB Output is correct
21 Correct 203 ms 22228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 22228 KB Output is correct
2 Correct 0 ms 22228 KB Output is correct
3 Correct 0 ms 22228 KB Output is correct
4 Correct 0 ms 22228 KB Output is correct
5 Correct 0 ms 22228 KB Output is correct
6 Correct 0 ms 22228 KB Output is correct
7 Correct 0 ms 22228 KB Output is correct
8 Correct 0 ms 22228 KB Output is correct
9 Correct 0 ms 22228 KB Output is correct
10 Correct 0 ms 22228 KB Output is correct
11 Correct 0 ms 22228 KB Output is correct
12 Correct 0 ms 22228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 22228 KB Output is correct
2 Correct 0 ms 22228 KB Output is correct
3 Correct 0 ms 22228 KB Output is correct
4 Correct 0 ms 22228 KB Output is correct
5 Correct 0 ms 22228 KB Output is correct
6 Correct 0 ms 22228 KB Output is correct
7 Correct 0 ms 22228 KB Output is correct
8 Correct 0 ms 22228 KB Output is correct
9 Correct 0 ms 22228 KB Output is correct
10 Correct 0 ms 22228 KB Output is correct
11 Correct 0 ms 22228 KB Output is correct
12 Correct 0 ms 22228 KB Output is correct
13 Correct 0 ms 22228 KB Output is correct
14 Correct 0 ms 22228 KB Output is correct
15 Correct 0 ms 22228 KB Output is correct
16 Correct 0 ms 22228 KB Output is correct
17 Correct 0 ms 22228 KB Output is correct
18 Correct 0 ms 22228 KB Output is correct
19 Correct 0 ms 22228 KB Output is correct
20 Correct 3 ms 22228 KB Output is correct
21 Correct 0 ms 22228 KB Output is correct
22 Correct 0 ms 22228 KB Output is correct
23 Correct 0 ms 22228 KB Output is correct
24 Correct 0 ms 22228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 22228 KB Output is correct
2 Correct 0 ms 22228 KB Output is correct
3 Correct 0 ms 22228 KB Output is correct
4 Correct 0 ms 22228 KB Output is correct
5 Correct 0 ms 22228 KB Output is correct
6 Correct 0 ms 22228 KB Output is correct
7 Correct 0 ms 22228 KB Output is correct
8 Correct 0 ms 22228 KB Output is correct
9 Correct 0 ms 22228 KB Output is correct
10 Correct 0 ms 22228 KB Output is correct
11 Correct 0 ms 22228 KB Output is correct
12 Correct 0 ms 22228 KB Output is correct
13 Correct 0 ms 22228 KB Output is correct
14 Correct 0 ms 22228 KB Output is correct
15 Correct 0 ms 22228 KB Output is correct
16 Correct 0 ms 22228 KB Output is correct
17 Correct 0 ms 22228 KB Output is correct
18 Correct 0 ms 22228 KB Output is correct
19 Correct 0 ms 22228 KB Output is correct
20 Correct 0 ms 22228 KB Output is correct
21 Correct 0 ms 22228 KB Output is correct
22 Correct 0 ms 22228 KB Output is correct
23 Correct 0 ms 22228 KB Output is correct
24 Correct 0 ms 22228 KB Output is correct
25 Correct 166 ms 22228 KB Output is correct
26 Correct 229 ms 22228 KB Output is correct
27 Correct 263 ms 22228 KB Output is correct
28 Correct 316 ms 22228 KB Output is correct
29 Correct 279 ms 22228 KB Output is correct
30 Correct 93 ms 22228 KB Output is correct
31 Correct 143 ms 22228 KB Output is correct
32 Correct 203 ms 22228 KB Output is correct
33 Correct 159 ms 22228 KB Output is correct
34 Correct 176 ms 22228 KB Output is correct
35 Correct 183 ms 22228 KB Output is correct
36 Correct 216 ms 22228 KB Output is correct
37 Correct 123 ms 22228 KB Output is correct