Submission #874248

# Submission time Handle Problem Language Result Execution time Memory
874248 2023-11-16T14:28:05 Z andrei_boaca Sky Walking (IOI19_walk) C++17
57 / 100
2045 ms 193600 KB
#include "walk.h"
#include <bits/stdc++.h>
//#include "grader.cpp"
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("fast-math")
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
const ll INF=1e18;
int n,m;
int ind;
vector<pii> muchii[1000005];
vector<pii> nodes[1000005];
vector<int> where[100005];
int arb[4*100005],toprop[4*100005];
ll dp[100005];
struct skywalk
{
    ll l,r,h,ind;
} v[100005];
ll dist[1000005];
bool comp(pii a,pii b)
{
    return a.second<b.second;
}
bool cresc(skywalk a, skywalk b)
{
    if(a.h!=b.h)
        return a.h<b.h;
    return a.l>b.l;
}
bool byr(skywalk a, skywalk b)
{
    return a.r<b.r;
}
bool descresc(skywalk a, skywalk b)
{
    if(a.h!=b.h)
        return a.h>b.h;
    return a.l>b.l;
}
void addnode(int x,int y)
{
    ind++;
    nodes[x].push_back({y,ind});
}
bool use[2000005];
void bfs()
{
    for(int i=1;i<=ind;i++)
        dist[i]=INF;
    dist[1]=0;
    priority_queue<pll,vector<pll>, greater<pll>> pq;
    pq.push({0,1});
    while(!pq.empty())
    {
        int nod=pq.top().second;
        if(nod==2)
            return;
        pq.pop();
        if(use[nod])
            continue;
        use[nod]=1;
        for(auto i:muchii[nod])
            if(dist[i.first]>dist[nod]+i.second)
            {
                dist[i.first]=dist[nod]+i.second;
                pq.push({dist[i.first],i.first});
            }
    }
}
void prop(int nod)
{
    int val=toprop[nod];
    arb[nod*2]=val;
    arb[nod*2+1]=val;
    toprop[nod*2]=val;
    toprop[nod*2+1]=val;
}
void build(int nod,int st,int dr)
{
    arb[nod]=-1;
    toprop[nod]=-1;
    if(st==dr)
        return;
    int mij=(st+dr)/2;
    build(nod*2,st,mij);
    build(nod*2+1,mij+1,dr);
}
void update(int nod,int st,int dr,int a,int b,int val)
{
    if(toprop[nod]!=-1&&st!=dr)
        prop(nod);
    toprop[nod]=-1;
    if(st>=a&&dr<=b)
    {
        arb[nod]=val;
        toprop[nod]=val;
        return;
    }
    int mij=(st+dr)/2;
    if(a<=mij)
        update(nod*2,st,mij,a,b,val);
    if(b>mij)
        update(nod*2+1,mij+1,dr,a,b,val);
}
int query(int nod,int st,int dr,int poz)
{
    if(toprop[nod]!=-1&&st!=dr)
        prop(nod);
    toprop[nod]=-1;
    if(st==dr)
        return arb[nod];
    int mij=(st+dr)/2;
    if(poz<=mij)
        return query(nod*2,st,mij,poz);
    else
        return query(nod*2+1,mij+1,dr,poz);
}
long long min_distance(std::vector<int> x, std::vector<int> h, std::vector<int> l, std::vector<int> r, std::vector<int> y, int s, int g)
{
    n=x.size();
    m=l.size();
    if(s!=0||g!=n-1)
    {
        addnode(s,0);
        addnode(g,0);
        for(int i=0;i<l.size();i++)
        {
            int prv=-1;
            for(int j=l[i];j<=r[i];j++)
                if(y[i]<=h[j])
                {
                    addnode(j,y[i]);
                    if(prv!=-1)
                    {
                        int a=ind-1;
                        int b=ind;
                        muchii[a].push_back({b,x[j]-x[prv]});
                        muchii[b].push_back({a,x[j]-x[prv]});
                    }
                    prv=j;
                }
        }
        for(int i=0;i<n;i++)
            if(!nodes[i].empty())
            {
                sort(nodes[i].begin(),nodes[i].end());
                for(int j=1;j<nodes[i].size();j++)
                {
                    ll d=nodes[i][j].first-nodes[i][j-1].first;
                    ll a=nodes[i][j].second;
                    ll b=nodes[i][j-1].second;
                    muchii[a].push_back({b,d});
                    muchii[b].push_back({a,d});
                }
            }
        bfs();
        if(dist[2]==INF)
            return -1;
        return dist[2];
    }
    for(int i=0;i<m;i++)
        v[i]={l[i],r[i],y[i],i};
    sort(v,v+m,cresc);
    build(1,0,n-1);
    for(int i=0;i<m;i++)
    {
        int rgt=v[i].r;
        int val=query(1,0,n-1,rgt);
        if(val!=-1)
            where[v[i].ind].push_back(val);
        update(1,0,n-1,v[i].l,v[i].r,v[i].ind);
    }
    sort(v,v+m,descresc);
    build(1,0,n-1);
    for(int i=0;i<m;i++)
    {
        int rgt=v[i].r;
        int val=query(1,0,n-1,rgt);
        if(val!=-1)
            where[v[i].ind].push_back(val);
        update(1,0,n-1,v[i].l,v[i].r,v[i].ind);
    }
    ll ans=INF;
    queue<int> coada;
    for(int i=0;i<m;i++)
    {
        dp[i]=INF;
        if(l[i]==0)
        {
            dp[i]=y[i];
            coada.push(i);
        }
    }
    while(!coada.empty())
    {
        int nod=coada.front();
        coada.pop();
        if(r[nod]==n-1)
            ans=min(ans,dp[nod]+y[nod]+x[n-1]-x[0]);
        for(int i:where[nod])
            if(dp[i]>dp[nod]+abs(y[i]-y[nod]))
            {
                dp[i]=dp[nod]+abs(y[i]-y[nod]);
                coada.push(i);
            }
        }
    if(ans==INF)
        return -1;
    return ans;
}

Compilation message

walk.cpp: In function 'long long int min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:130:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  130 |         for(int i=0;i<l.size();i++)
      |                     ~^~~~~~~~~
walk.cpp:151:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  151 |                 for(int j=1;j<nodes[i].size();j++)
      |                             ~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 51800 KB Output is correct
2 Correct 11 ms 56156 KB Output is correct
3 Correct 14 ms 52060 KB Output is correct
4 Correct 11 ms 52000 KB Output is correct
5 Correct 11 ms 52056 KB Output is correct
6 Correct 11 ms 52060 KB Output is correct
7 Correct 14 ms 52060 KB Output is correct
8 Correct 11 ms 52056 KB Output is correct
9 Correct 15 ms 52060 KB Output is correct
10 Correct 13 ms 52056 KB Output is correct
11 Correct 11 ms 52056 KB Output is correct
12 Correct 13 ms 52060 KB Output is correct
13 Correct 12 ms 51804 KB Output is correct
14 Correct 11 ms 52060 KB Output is correct
15 Correct 14 ms 52060 KB Output is correct
16 Correct 15 ms 56152 KB Output is correct
17 Correct 14 ms 56156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 52012 KB Output is correct
2 Correct 12 ms 56156 KB Output is correct
3 Correct 253 ms 104304 KB Output is correct
4 Correct 361 ms 110156 KB Output is correct
5 Correct 225 ms 102532 KB Output is correct
6 Correct 605 ms 95596 KB Output is correct
7 Correct 213 ms 102660 KB Output is correct
8 Correct 341 ms 120548 KB Output is correct
9 Correct 253 ms 103232 KB Output is correct
10 Correct 532 ms 130640 KB Output is correct
11 Correct 159 ms 84128 KB Output is correct
12 Correct 128 ms 69100 KB Output is correct
13 Correct 1010 ms 69456 KB Output is correct
14 Correct 2045 ms 71740 KB Output is correct
15 Correct 1090 ms 70752 KB Output is correct
16 Correct 147 ms 72356 KB Output is correct
17 Correct 138 ms 72020 KB Output is correct
18 Correct 86 ms 68172 KB Output is correct
19 Correct 23 ms 52816 KB Output is correct
20 Correct 930 ms 62268 KB Output is correct
21 Correct 106 ms 67408 KB Output is correct
22 Correct 103 ms 68176 KB Output is correct
23 Correct 104 ms 67904 KB Output is correct
24 Correct 113 ms 68072 KB Output is correct
25 Correct 113 ms 67408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 61008 KB Output is correct
2 Correct 98 ms 63692 KB Output is correct
3 Correct 115 ms 63828 KB Output is correct
4 Correct 147 ms 65108 KB Output is correct
5 Correct 153 ms 65256 KB Output is correct
6 Correct 148 ms 65404 KB Output is correct
7 Correct 78 ms 62424 KB Output is correct
8 Correct 118 ms 65148 KB Output is correct
9 Correct 135 ms 65104 KB Output is correct
10 Correct 107 ms 65132 KB Output is correct
11 Correct 19 ms 56808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 61008 KB Output is correct
2 Correct 98 ms 63692 KB Output is correct
3 Correct 115 ms 63828 KB Output is correct
4 Correct 147 ms 65108 KB Output is correct
5 Correct 153 ms 65256 KB Output is correct
6 Correct 148 ms 65404 KB Output is correct
7 Correct 78 ms 62424 KB Output is correct
8 Correct 118 ms 65148 KB Output is correct
9 Correct 135 ms 65104 KB Output is correct
10 Correct 107 ms 65132 KB Output is correct
11 Correct 19 ms 56808 KB Output is correct
12 Correct 126 ms 63828 KB Output is correct
13 Correct 151 ms 65104 KB Output is correct
14 Correct 151 ms 65104 KB Output is correct
15 Correct 130 ms 65008 KB Output is correct
16 Correct 127 ms 65032 KB Output is correct
17 Correct 126 ms 65052 KB Output is correct
18 Correct 128 ms 65024 KB Output is correct
19 Correct 126 ms 65108 KB Output is correct
20 Correct 85 ms 62368 KB Output is correct
21 Correct 28 ms 57704 KB Output is correct
22 Correct 114 ms 64464 KB Output is correct
23 Correct 115 ms 64592 KB Output is correct
24 Correct 110 ms 64592 KB Output is correct
25 Correct 110 ms 64308 KB Output is correct
26 Correct 110 ms 64596 KB Output is correct
27 Correct 153 ms 65028 KB Output is correct
28 Correct 137 ms 65020 KB Output is correct
29 Correct 142 ms 65012 KB Output is correct
30 Correct 78 ms 62316 KB Output is correct
31 Correct 138 ms 65196 KB Output is correct
32 Correct 106 ms 65108 KB Output is correct
33 Correct 105 ms 65108 KB Output is correct
34 Correct 113 ms 65228 KB Output is correct
35 Correct 116 ms 65104 KB Output is correct
36 Correct 147 ms 64852 KB Output is correct
37 Correct 105 ms 64836 KB Output is correct
38 Correct 109 ms 65232 KB Output is correct
39 Correct 99 ms 65096 KB Output is correct
40 Correct 111 ms 65104 KB Output is correct
41 Correct 112 ms 64852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 51800 KB Output is correct
2 Correct 11 ms 56156 KB Output is correct
3 Correct 14 ms 52060 KB Output is correct
4 Correct 11 ms 52000 KB Output is correct
5 Correct 11 ms 52056 KB Output is correct
6 Correct 11 ms 52060 KB Output is correct
7 Correct 14 ms 52060 KB Output is correct
8 Correct 11 ms 52056 KB Output is correct
9 Correct 15 ms 52060 KB Output is correct
10 Correct 13 ms 52056 KB Output is correct
11 Correct 11 ms 52056 KB Output is correct
12 Correct 13 ms 52060 KB Output is correct
13 Correct 12 ms 51804 KB Output is correct
14 Correct 11 ms 52060 KB Output is correct
15 Correct 14 ms 52060 KB Output is correct
16 Correct 15 ms 56152 KB Output is correct
17 Correct 14 ms 56156 KB Output is correct
18 Correct 14 ms 52012 KB Output is correct
19 Correct 12 ms 56156 KB Output is correct
20 Correct 253 ms 104304 KB Output is correct
21 Correct 361 ms 110156 KB Output is correct
22 Correct 225 ms 102532 KB Output is correct
23 Correct 605 ms 95596 KB Output is correct
24 Correct 213 ms 102660 KB Output is correct
25 Correct 341 ms 120548 KB Output is correct
26 Correct 253 ms 103232 KB Output is correct
27 Correct 532 ms 130640 KB Output is correct
28 Correct 159 ms 84128 KB Output is correct
29 Correct 128 ms 69100 KB Output is correct
30 Correct 1010 ms 69456 KB Output is correct
31 Correct 2045 ms 71740 KB Output is correct
32 Correct 1090 ms 70752 KB Output is correct
33 Correct 147 ms 72356 KB Output is correct
34 Correct 138 ms 72020 KB Output is correct
35 Correct 86 ms 68172 KB Output is correct
36 Correct 23 ms 52816 KB Output is correct
37 Correct 930 ms 62268 KB Output is correct
38 Correct 106 ms 67408 KB Output is correct
39 Correct 103 ms 68176 KB Output is correct
40 Correct 104 ms 67904 KB Output is correct
41 Correct 113 ms 68072 KB Output is correct
42 Correct 113 ms 67408 KB Output is correct
43 Correct 50 ms 61008 KB Output is correct
44 Correct 98 ms 63692 KB Output is correct
45 Correct 115 ms 63828 KB Output is correct
46 Correct 147 ms 65108 KB Output is correct
47 Correct 153 ms 65256 KB Output is correct
48 Correct 148 ms 65404 KB Output is correct
49 Correct 78 ms 62424 KB Output is correct
50 Correct 118 ms 65148 KB Output is correct
51 Correct 135 ms 65104 KB Output is correct
52 Correct 107 ms 65132 KB Output is correct
53 Correct 19 ms 56808 KB Output is correct
54 Correct 126 ms 63828 KB Output is correct
55 Correct 151 ms 65104 KB Output is correct
56 Correct 151 ms 65104 KB Output is correct
57 Correct 130 ms 65008 KB Output is correct
58 Correct 127 ms 65032 KB Output is correct
59 Correct 126 ms 65052 KB Output is correct
60 Correct 128 ms 65024 KB Output is correct
61 Correct 126 ms 65108 KB Output is correct
62 Correct 85 ms 62368 KB Output is correct
63 Correct 28 ms 57704 KB Output is correct
64 Correct 114 ms 64464 KB Output is correct
65 Correct 115 ms 64592 KB Output is correct
66 Correct 110 ms 64592 KB Output is correct
67 Correct 110 ms 64308 KB Output is correct
68 Correct 110 ms 64596 KB Output is correct
69 Correct 153 ms 65028 KB Output is correct
70 Correct 137 ms 65020 KB Output is correct
71 Correct 142 ms 65012 KB Output is correct
72 Correct 78 ms 62316 KB Output is correct
73 Correct 138 ms 65196 KB Output is correct
74 Correct 106 ms 65108 KB Output is correct
75 Correct 105 ms 65108 KB Output is correct
76 Correct 113 ms 65228 KB Output is correct
77 Correct 116 ms 65104 KB Output is correct
78 Correct 147 ms 64852 KB Output is correct
79 Correct 105 ms 64836 KB Output is correct
80 Correct 109 ms 65232 KB Output is correct
81 Correct 99 ms 65096 KB Output is correct
82 Correct 111 ms 65104 KB Output is correct
83 Correct 112 ms 64852 KB Output is correct
84 Correct 54 ms 61536 KB Output is correct
85 Runtime error 194 ms 193600 KB Execution killed with signal 11
86 Halted 0 ms 0 KB -