Submission #895160

# Submission time Handle Problem Language Result Execution time Memory
895160 2023-12-29T13:52:51 Z alexander707070 Sightseeing in Kyoto (JOI22_kyoto) C++14
100 / 100
367 ms 21332 KB
#include<bits/stdc++.h>
#define MAXN 100007
using namespace std;

struct event{
    long double diff;
    int from,to;

    inline friend bool operator < (event fr,event sc){
        if(fr.diff!=sc.diff)return fr.diff<sc.diff;
        return fr.from<sc.from;
    }
}xs,ys,curr;

struct node{
    int l,r;
};

int n,m;
long long a[MAXN],b[MAXN],ans;
bool isx,isy;
int xx,yy;

set<event> x,y;
node as[MAXN],bs[MAXN];

void erasea(int x){
    as[as[x].l].r=as[x].r;
    as[as[x].r].l=as[x].l;
}

void eraseb(int x){
    bs[bs[x].l].r=bs[x].r;
    bs[bs[x].r].l=bs[x].l;
}

int main(){

    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        as[i].l=i-1; as[i].r=i+1;

        if(i>1){
            xs={a[i]-a[i-1],i-1,i};
            x.insert(xs);
        }
    }
    as[0].r=1; as[n+1].l=n;

    for(int i=1;i<=m;i++){
        cin>>b[i];
        bs[i].l=i-1; bs[i].r=i+1;

        if(i>1){
            ys={b[i]-b[i-1],i-1,i};
            y.insert(ys);
        }
    }
    bs[0].r=1; bs[m+1].l=m;

    isx=isy=true;
    xx=n; yy=m;

    while(!x.empty() and !y.empty()){
        auto it=x.rbegin(),it2=y.rbegin();
        xs=*it; ys=*it2;

        if(xs.diff>ys.diff){
            x.erase(xs);
            if(as[xs.to].r!=n+1){
                curr={1.0*(a[as[xs.to].r]-a[xs.to])/(as[xs.to].r-xs.to),xs.to,as[xs.to].r};
                x.erase(curr);

                x.insert({1.0*(a[as[xs.to].r]-a[xs.from])/(as[xs.to].r-xs.from),xs.from,as[xs.to].r});
            }else{
                if(isx and isy)isx=false;
                else{
                    isy=true; isx=false;
                    ans+=(yy-bs[m+1].l)*a[xs.to];
                    yy=bs[m+1].l;
                }
            }
            erasea(xs.to);
        }else{
            y.erase(ys);
            if(bs[ys.to].r!=m+1){
                curr={1.0*(b[bs[ys.to].r]-b[ys.to])/(bs[ys.to].r-ys.to),ys.to,bs[ys.to].r};
                y.erase(curr);

                y.insert({1.0*(b[bs[ys.to].r]-b[ys.from])/(bs[ys.to].r-ys.from),ys.from,bs[ys.to].r});
            }else{
                if(isy and isx)isy=false;
                else{
                    isx=true; isy=false;
                    ans+=(xx-as[n+1].l)*b[ys.to];
                    xx=as[n+1].l;
                }
            }
            eraseb(ys.to);
        }
    }

    ans+=(xx-1)*b[bs[m+1].l];
    ans+=(yy-1)*a[as[n+1].l];

    cout<<ans<<"\n";


    return 0;
}

Compilation message

kyoto.cpp: In function 'int main()':
kyoto.cpp:45:21: warning: narrowing conversion of '(a[i] - a[(i - 1)])' from 'long long int' to 'long double' [-Wnarrowing]
   45 |             xs={a[i]-a[i-1],i-1,i};
      |                 ~~~~^~~~~~~
kyoto.cpp:56:21: warning: narrowing conversion of '(b[i] - b[(i - 1)])' from 'long long int' to 'long double' [-Wnarrowing]
   56 |             ys={b[i]-b[i-1],i-1,i};
      |                 ~~~~^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2504 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 2 ms 2652 KB Output is correct
10 Correct 2 ms 2652 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 2 ms 2652 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 2 ms 2512 KB Output is correct
16 Correct 2 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 0 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 0 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2392 KB Output is correct
25 Correct 1 ms 2392 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 102 ms 9640 KB Output is correct
5 Correct 53 ms 8868 KB Output is correct
6 Correct 34 ms 5420 KB Output is correct
7 Correct 346 ms 19792 KB Output is correct
8 Correct 330 ms 19796 KB Output is correct
9 Correct 332 ms 19792 KB Output is correct
10 Correct 330 ms 19792 KB Output is correct
11 Correct 118 ms 19796 KB Output is correct
12 Correct 341 ms 20000 KB Output is correct
13 Correct 326 ms 19796 KB Output is correct
14 Correct 164 ms 19924 KB Output is correct
15 Correct 86 ms 20048 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 0 ms 2392 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2504 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 2 ms 2652 KB Output is correct
10 Correct 2 ms 2652 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 2 ms 2652 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 2 ms 2512 KB Output is correct
16 Correct 2 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 0 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 0 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2392 KB Output is correct
25 Correct 1 ms 2392 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
29 Correct 1 ms 2396 KB Output is correct
30 Correct 1 ms 2392 KB Output is correct
31 Correct 1 ms 2396 KB Output is correct
32 Correct 102 ms 9640 KB Output is correct
33 Correct 53 ms 8868 KB Output is correct
34 Correct 34 ms 5420 KB Output is correct
35 Correct 346 ms 19792 KB Output is correct
36 Correct 330 ms 19796 KB Output is correct
37 Correct 332 ms 19792 KB Output is correct
38 Correct 330 ms 19792 KB Output is correct
39 Correct 118 ms 19796 KB Output is correct
40 Correct 341 ms 20000 KB Output is correct
41 Correct 326 ms 19796 KB Output is correct
42 Correct 164 ms 19924 KB Output is correct
43 Correct 86 ms 20048 KB Output is correct
44 Correct 1 ms 2396 KB Output is correct
45 Correct 0 ms 2392 KB Output is correct
46 Correct 1 ms 2396 KB Output is correct
47 Correct 1 ms 2396 KB Output is correct
48 Correct 1 ms 2392 KB Output is correct
49 Correct 1 ms 2396 KB Output is correct
50 Correct 1 ms 2396 KB Output is correct
51 Correct 1 ms 2396 KB Output is correct
52 Correct 1 ms 2396 KB Output is correct
53 Correct 1 ms 2396 KB Output is correct
54 Correct 1 ms 2392 KB Output is correct
55 Correct 116 ms 10932 KB Output is correct
56 Correct 2 ms 2396 KB Output is correct
57 Correct 9 ms 3356 KB Output is correct
58 Correct 29 ms 4816 KB Output is correct
59 Correct 350 ms 21128 KB Output is correct
60 Correct 359 ms 21196 KB Output is correct
61 Correct 349 ms 21076 KB Output is correct
62 Correct 367 ms 21036 KB Output is correct
63 Correct 132 ms 20816 KB Output is correct
64 Correct 367 ms 21332 KB Output is correct
65 Correct 344 ms 21072 KB Output is correct
66 Correct 148 ms 20976 KB Output is correct
67 Correct 143 ms 21332 KB Output is correct