Submission #409781

# Submission time Handle Problem Language Result Execution time Memory
409781 2021-05-21T14:05:37 Z MKopchev Sky Walking (IOI19_walk) C++14
100 / 100
3377 ms 239788 KB
#include "walk.h"
#include<bits/stdc++.h>

using namespace std;

const int nmax=1e5+42;

set<int> there[nmax];
map<int,int> id[nmax];

vector<long long> dist;

vector< vector< pair<int,int> > > adj;

vector<int> inp;

void add_edge(int x1,int y1,int x2,int y2)
{
    int p=id[x1][y1];
    int q=id[x2][y2];

    int d=abs(inp[x1]-inp[x2])+abs(y1-y2);

    //cout<<"x1= "<<x1<<" y1= "<<y1<<" x2= "<<x2<<" y2= "<<y2<<" d= "<<d<<" p= "<<p<<" q= "<<q<<endl;

    while(adj.size()<=p)adj.push_back({});
    while(adj.size()<=q)adj.push_back({});

    adj[p].push_back({q,d});
    adj[q].push_back({p,d});
}

priority_queue< pair<long long,int> > pq;

int tree[20][nmax];
int depth[nmax];

vector<int> mem_h,mem_l,mem_r,mem_y;

void build(int node,int l,int r)
{
    for(int step=0;(1<<step)<=r-l+1;step++)
    {
        for(int i=l;i+(1<<step)-1<=r;i++)
        {
            if(step==0)tree[step][i]=mem_h[i];
            else tree[step][i]=max(tree[step-1][i],tree[step-1][i+(1<<(step-1))]);
        }
    }
    depth[1]=0;
    for(int i=2;i<=r-l+1;i++)
        depth[i]=depth[i/2]+1;
}

void push(int i,int j)
{
    //cout<<"push "<<i<<" "<<j<<endl;

    if(mem_l[i]<=j&&j<=mem_r[i])
    {
        if(mem_h[j]>=mem_y[i])
        {
            //cout<<"in"<<endl;
            there[j].insert(mem_y[i]);
        }
    }
}
int query(int node,int l,int r,int lq,int rq)
{
    int d=depth[rq-lq+1];

    //cout<<d<<" "<<rq-lq+1<<endl;

    assert(lq+(1<<d)-1+1>=rq-(1<<d)+1);

    return max(tree[d][lq],tree[d][rq-(1<<d)+1]);
}

int get_first(int l,int r,int h)
{
    if(l>r)return -1;

    if(query(1,0,mem_h.size()-1,l,r)<h)return -1;

    int ok=r,not_ok=l-1;

    while(ok-not_ok>1)
    {
        int av=(ok+not_ok)/2;

        if(query(1,0,mem_h.size()-1,l,av)<h)not_ok=av;
        else ok=av;
    }
    return ok;
}

int get_last(int l,int r,int h)
{
    if(l>r)return -1;

    if(query(1,0,mem_h.size()-1,l,r)<h)return -1;

    int ok=l,not_ok=r+1;

    while(not_ok-ok>1)
    {
        int av=(ok+not_ok)/2;

        if(query(1,0,mem_h.size()-1,av,r)<h)not_ok=av;
        else ok=av;
    }
    return ok;
}

vector< pair<int,int> > updates[nmax];

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) {

    mem_l=l;
    mem_r=r;
    mem_y=y;

    if(s>g)swap(s,g);

    inp=x;
    mem_h=h;

    build(1,0,h.size()-1);

	for(int i=0;i<x.size();i++)
        there[i].insert(0);

	for(int i=0;i<y.size();i++)
    {
        updates[l[i]].push_back({y[i],1});
        updates[r[i]+1].push_back({y[i],-1});

        //cout<<"line: "<<l[i]<<" "<<r[i]<<" "<<y[i]<<endl;

        /*
        for(int j=l[i];j<=r[i];j++)
            if(h[j]>=y[i])
            {
                //cout<<"push "<<j<<" h= "<<y[i]<<endl;

                there[j].insert(y[i]);
                noted[i].insert(j);
            }
        */

        push(i,l[i]);

        push(i,get_last(l[i],s,y[i]));

        push(i,get_first(s,g,y[i]));

        push(i,get_last(s,g,y[i]));

        push(i,get_first(g,r[i],y[i]));

        push(i,r[i]);
    }

    set<int> active={};
    map<int,int> cnt_active={};

    for(int i=0;i<x.size();i++)
    {
        for(auto w:updates[i])
        {
            cnt_active[w.first]+=w.second;

            active.erase(w.first);

            if(cnt_active[w.first])active.insert(w.first);
        }

        set<int> help=there[i];

        for(auto w:help)
        {
            set<int>::iterator it=active.lower_bound(w);

            if(it!=active.begin())
            {
                it--;

                int val=*it;

                if(0<=val&&val<=h[i])there[i].insert(val);

                //cout<<"low "<<*it<<endl;
            }

            it=active.upper_bound(w);

            if(it!=active.end())
            {
                int val=*it;

                if(0<=val&&val<=h[i])there[i].insert(val);

                //cout<<"high "<<*it<<endl;
            }
        }
    }

    int pointer=0;

    for(int i=0;i<x.size();i++)
    {
        for(auto w:there[i])
        {
            id[i][w]=pointer;

            //cout<<"id "<<i<<" "<<w<<" -> "<<pointer<<endl;

            pointer++;
        }
    }

    /*
    cout<<"towers: "<<endl;

    for(int i=0;i<x.size();i++)
    {
        cout<<"i= "<<i<<" : ";

        for(auto w:there[i])
            cout<<w<<" ";

        cout<<endl;
    }
    */

    for(int i=0;i<x.size();i++)
    {
        int prv=0;

        for(auto w:there[i])
        {
            if(w)
            {
                add_edge(i,prv,i,w);
            }

            prv=w;
        }
    }

    //cout<<"-------------"<<endl;

    map<int, vector<int> > seen_by_y={};

    for(int i=0;i<x.size();i++)
    {
        for(auto w:there[i])
            seen_by_y[w].push_back(i);
    }

    map<int, vector< pair<int,int> > > seen_updates_y={};

    for(int i=0;i<y.size();i++)
        seen_updates_y[y[i]].push_back({l[i],r[i]});

    for(auto w:seen_by_y)
    {
        //cout<<l[i]<<" "<<r[i]<<" "<<y[i]<<" :"<<endl;

        vector<int> me=w.second;

        vector<int> pref=me;

        for(auto &t:pref)
            t=0;

        int prv=-1;

        for(auto u:seen_updates_y[w.first])
        {
            int p=lower_bound(me.begin(),me.end(),u.first)-me.begin();
            int q=lower_bound(me.begin(),me.end(),u.second)-me.begin();

            pref[p]++;
            pref[q]--;
        }

        for(int i=1;i<pref.size();i++)
            pref[i]+=pref[i-1];

        for(int i=0;i+1<pref.size();i++)
        {
            if(pref[i])
                add_edge(me[i],w.first,me[i+1],w.first);
        }
    }

    /*
    cout<<"there: "<<endl;
    for(int i=0;i<x.size();i++)
    {
        cout<<i<<" -> ";for(auto j:there[i])cout<<j<<" ";cout<<endl;
    }
    */

    for(int i=0;i<=pointer;i++)
        dist.push_back(-1);

    pq.push({0,id[s][0]});

    while(pq.size())
    {
        pair<long long,int> tp=pq.top();
        pq.pop();

        tp.first=-tp.first;

        if(dist[tp.second]!=-1)continue;

        dist[tp.second]=tp.first;

        //cout<<"dist: "<<tp.second<<" -> "<<tp.first<<endl;

        for(auto w:adj[tp.second])
            pq.push({-(tp.first+w.second),w.first});
    }

    return dist[id[g][0]];
}
/*
int main() {
    int n, m;
	assert(2 == scanf("%d%d", &n, &m));
	vector<int> x(n), h(n);
	for (int i = 0; i < n; i++)
		assert(2 == scanf("%d%d", &x[i], &h[i]));
	vector<int> l(m), r(m), y(m);
	for (int i = 0; i < m; i++)
		assert(3 == scanf("%d%d%d", &l[i], &r[i], &y[i]));
	int s, g;
	assert(2 == scanf("%d%d", &s, &g));
	fclose(stdin);

	long long result = min_distance(x, h, l, r, y, s, g);

	printf("%lld\n", result);
	fclose(stdout);
	return 0;
}
*/

Compilation message

walk.cpp: In function 'void add_edge(int, int, int, int)':
walk.cpp:26:21: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<std::pair<int, int> > >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   26 |     while(adj.size()<=p)adj.push_back({});
      |           ~~~~~~~~~~^~~
walk.cpp:27:21: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<std::pair<int, int> > >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   27 |     while(adj.size()<=q)adj.push_back({});
      |           ~~~~~~~~~~^~~
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:15: 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<x.size();i++)
      |              ~^~~~~~~~~
walk.cpp:133:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |  for(int i=0;i<y.size();i++)
      |              ~^~~~~~~~~
walk.cpp:167:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  167 |     for(int i=0;i<x.size();i++)
      |                 ~^~~~~~~~~
walk.cpp:210:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  210 |     for(int i=0;i<x.size();i++)
      |                 ~^~~~~~~~~
walk.cpp:236:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  236 |     for(int i=0;i<x.size();i++)
      |                 ~^~~~~~~~~
walk.cpp:255:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  255 |     for(int i=0;i<x.size();i++)
      |                 ~^~~~~~~~~
walk.cpp:263:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  263 |     for(int i=0;i<y.size();i++)
      |                 ~^~~~~~~~~
walk.cpp:288:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  288 |         for(int i=1;i<pref.size();i++)
      |                     ~^~~~~~~~~~~~
walk.cpp:291:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  291 |         for(int i=0;i+1<pref.size();i++)
      |                     ~~~^~~~~~~~~~~~
walk.cpp:277:13: warning: unused variable 'prv' [-Wunused-variable]
  277 |         int prv=-1;
      |             ^~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 11980 KB Output is correct
2 Correct 8 ms 11980 KB Output is correct
3 Correct 8 ms 12100 KB Output is correct
4 Correct 8 ms 11980 KB Output is correct
5 Correct 8 ms 12108 KB Output is correct
6 Correct 8 ms 12160 KB Output is correct
7 Correct 9 ms 12128 KB Output is correct
8 Correct 8 ms 12108 KB Output is correct
9 Correct 8 ms 11980 KB Output is correct
10 Correct 8 ms 12108 KB Output is correct
11 Correct 8 ms 12108 KB Output is correct
12 Correct 8 ms 12108 KB Output is correct
13 Correct 8 ms 12032 KB Output is correct
14 Correct 8 ms 12108 KB Output is correct
15 Correct 7 ms 11980 KB Output is correct
16 Correct 7 ms 12108 KB Output is correct
17 Correct 8 ms 12100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11972 KB Output is correct
2 Correct 7 ms 11980 KB Output is correct
3 Correct 1451 ms 128208 KB Output is correct
4 Correct 1500 ms 169232 KB Output is correct
5 Correct 933 ms 138160 KB Output is correct
6 Correct 901 ms 130556 KB Output is correct
7 Correct 962 ms 138456 KB Output is correct
8 Correct 1561 ms 135992 KB Output is correct
9 Correct 1249 ms 149148 KB Output is correct
10 Correct 1599 ms 174788 KB Output is correct
11 Correct 1169 ms 117256 KB Output is correct
12 Correct 836 ms 110100 KB Output is correct
13 Correct 1521 ms 178312 KB Output is correct
14 Correct 749 ms 104408 KB Output is correct
15 Correct 648 ms 92576 KB Output is correct
16 Correct 548 ms 86624 KB Output is correct
17 Correct 519 ms 83548 KB Output is correct
18 Correct 939 ms 108812 KB Output is correct
19 Correct 28 ms 16492 KB Output is correct
20 Correct 267 ms 58080 KB Output is correct
21 Correct 424 ms 78228 KB Output is correct
22 Correct 436 ms 85672 KB Output is correct
23 Correct 866 ms 111560 KB Output is correct
24 Correct 473 ms 85860 KB Output is correct
25 Correct 484 ms 82180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 142 ms 30652 KB Output is correct
2 Correct 1843 ms 154088 KB Output is correct
3 Correct 2005 ms 163020 KB Output is correct
4 Correct 2351 ms 205932 KB Output is correct
5 Correct 2523 ms 205056 KB Output is correct
6 Correct 2293 ms 197740 KB Output is correct
7 Correct 1122 ms 135384 KB Output is correct
8 Correct 824 ms 110276 KB Output is correct
9 Correct 2206 ms 189820 KB Output is correct
10 Correct 777 ms 118716 KB Output is correct
11 Correct 40 ms 24528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 142 ms 30652 KB Output is correct
2 Correct 1843 ms 154088 KB Output is correct
3 Correct 2005 ms 163020 KB Output is correct
4 Correct 2351 ms 205932 KB Output is correct
5 Correct 2523 ms 205056 KB Output is correct
6 Correct 2293 ms 197740 KB Output is correct
7 Correct 1122 ms 135384 KB Output is correct
8 Correct 824 ms 110276 KB Output is correct
9 Correct 2206 ms 189820 KB Output is correct
10 Correct 777 ms 118716 KB Output is correct
11 Correct 40 ms 24528 KB Output is correct
12 Correct 1971 ms 162564 KB Output is correct
13 Correct 1789 ms 205720 KB Output is correct
14 Correct 2502 ms 204596 KB Output is correct
15 Correct 1368 ms 152756 KB Output is correct
16 Correct 1450 ms 166832 KB Output is correct
17 Correct 1696 ms 190628 KB Output is correct
18 Correct 1351 ms 152616 KB Output is correct
19 Correct 1446 ms 166756 KB Output is correct
20 Correct 1102 ms 131556 KB Output is correct
21 Correct 163 ms 61164 KB Output is correct
22 Correct 1200 ms 171276 KB Output is correct
23 Correct 1104 ms 162848 KB Output is correct
24 Correct 878 ms 121688 KB Output is correct
25 Correct 1073 ms 156732 KB Output is correct
26 Correct 732 ms 104152 KB Output is correct
27 Correct 2485 ms 204776 KB Output is correct
28 Correct 1666 ms 206696 KB Output is correct
29 Correct 2249 ms 197524 KB Output is correct
30 Correct 1072 ms 134612 KB Output is correct
31 Correct 2102 ms 189632 KB Output is correct
32 Correct 606 ms 97568 KB Output is correct
33 Correct 604 ms 101456 KB Output is correct
34 Correct 789 ms 111956 KB Output is correct
35 Correct 809 ms 114152 KB Output is correct
36 Correct 626 ms 97048 KB Output is correct
37 Correct 424 ms 78356 KB Output is correct
38 Correct 432 ms 85720 KB Output is correct
39 Correct 864 ms 111700 KB Output is correct
40 Correct 470 ms 86056 KB Output is correct
41 Correct 479 ms 82256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 11980 KB Output is correct
2 Correct 8 ms 11980 KB Output is correct
3 Correct 8 ms 12100 KB Output is correct
4 Correct 8 ms 11980 KB Output is correct
5 Correct 8 ms 12108 KB Output is correct
6 Correct 8 ms 12160 KB Output is correct
7 Correct 9 ms 12128 KB Output is correct
8 Correct 8 ms 12108 KB Output is correct
9 Correct 8 ms 11980 KB Output is correct
10 Correct 8 ms 12108 KB Output is correct
11 Correct 8 ms 12108 KB Output is correct
12 Correct 8 ms 12108 KB Output is correct
13 Correct 8 ms 12032 KB Output is correct
14 Correct 8 ms 12108 KB Output is correct
15 Correct 7 ms 11980 KB Output is correct
16 Correct 7 ms 12108 KB Output is correct
17 Correct 8 ms 12100 KB Output is correct
18 Correct 7 ms 11972 KB Output is correct
19 Correct 7 ms 11980 KB Output is correct
20 Correct 1451 ms 128208 KB Output is correct
21 Correct 1500 ms 169232 KB Output is correct
22 Correct 933 ms 138160 KB Output is correct
23 Correct 901 ms 130556 KB Output is correct
24 Correct 962 ms 138456 KB Output is correct
25 Correct 1561 ms 135992 KB Output is correct
26 Correct 1249 ms 149148 KB Output is correct
27 Correct 1599 ms 174788 KB Output is correct
28 Correct 1169 ms 117256 KB Output is correct
29 Correct 836 ms 110100 KB Output is correct
30 Correct 1521 ms 178312 KB Output is correct
31 Correct 749 ms 104408 KB Output is correct
32 Correct 648 ms 92576 KB Output is correct
33 Correct 548 ms 86624 KB Output is correct
34 Correct 519 ms 83548 KB Output is correct
35 Correct 939 ms 108812 KB Output is correct
36 Correct 28 ms 16492 KB Output is correct
37 Correct 267 ms 58080 KB Output is correct
38 Correct 424 ms 78228 KB Output is correct
39 Correct 436 ms 85672 KB Output is correct
40 Correct 866 ms 111560 KB Output is correct
41 Correct 473 ms 85860 KB Output is correct
42 Correct 484 ms 82180 KB Output is correct
43 Correct 142 ms 30652 KB Output is correct
44 Correct 1843 ms 154088 KB Output is correct
45 Correct 2005 ms 163020 KB Output is correct
46 Correct 2351 ms 205932 KB Output is correct
47 Correct 2523 ms 205056 KB Output is correct
48 Correct 2293 ms 197740 KB Output is correct
49 Correct 1122 ms 135384 KB Output is correct
50 Correct 824 ms 110276 KB Output is correct
51 Correct 2206 ms 189820 KB Output is correct
52 Correct 777 ms 118716 KB Output is correct
53 Correct 40 ms 24528 KB Output is correct
54 Correct 1971 ms 162564 KB Output is correct
55 Correct 1789 ms 205720 KB Output is correct
56 Correct 2502 ms 204596 KB Output is correct
57 Correct 1368 ms 152756 KB Output is correct
58 Correct 1450 ms 166832 KB Output is correct
59 Correct 1696 ms 190628 KB Output is correct
60 Correct 1351 ms 152616 KB Output is correct
61 Correct 1446 ms 166756 KB Output is correct
62 Correct 1102 ms 131556 KB Output is correct
63 Correct 163 ms 61164 KB Output is correct
64 Correct 1200 ms 171276 KB Output is correct
65 Correct 1104 ms 162848 KB Output is correct
66 Correct 878 ms 121688 KB Output is correct
67 Correct 1073 ms 156732 KB Output is correct
68 Correct 732 ms 104152 KB Output is correct
69 Correct 2485 ms 204776 KB Output is correct
70 Correct 1666 ms 206696 KB Output is correct
71 Correct 2249 ms 197524 KB Output is correct
72 Correct 1072 ms 134612 KB Output is correct
73 Correct 2102 ms 189632 KB Output is correct
74 Correct 606 ms 97568 KB Output is correct
75 Correct 604 ms 101456 KB Output is correct
76 Correct 789 ms 111956 KB Output is correct
77 Correct 809 ms 114152 KB Output is correct
78 Correct 626 ms 97048 KB Output is correct
79 Correct 424 ms 78356 KB Output is correct
80 Correct 432 ms 85720 KB Output is correct
81 Correct 864 ms 111700 KB Output is correct
82 Correct 470 ms 86056 KB Output is correct
83 Correct 479 ms 82256 KB Output is correct
84 Correct 123 ms 27432 KB Output is correct
85 Correct 2070 ms 164552 KB Output is correct
86 Correct 2870 ms 219552 KB Output is correct
87 Correct 230 ms 66612 KB Output is correct
88 Correct 250 ms 68400 KB Output is correct
89 Correct 226 ms 66604 KB Output is correct
90 Correct 55 ms 21128 KB Output is correct
91 Correct 11 ms 12748 KB Output is correct
92 Correct 64 ms 22008 KB Output is correct
93 Correct 806 ms 109384 KB Output is correct
94 Correct 171 ms 61384 KB Output is correct
95 Correct 1336 ms 177132 KB Output is correct
96 Correct 1133 ms 163464 KB Output is correct
97 Correct 896 ms 121560 KB Output is correct
98 Correct 1090 ms 156572 KB Output is correct
99 Correct 3377 ms 239788 KB Output is correct
100 Correct 2286 ms 211204 KB Output is correct
101 Correct 2568 ms 213480 KB Output is correct
102 Correct 1117 ms 137596 KB Output is correct
103 Correct 585 ms 100524 KB Output is correct
104 Correct 590 ms 104408 KB Output is correct
105 Correct 821 ms 113764 KB Output is correct
106 Correct 715 ms 111456 KB Output is correct
107 Correct 787 ms 105820 KB Output is correct
108 Correct 120 ms 27384 KB Output is correct
109 Correct 1821 ms 172656 KB Output is correct
110 Correct 1718 ms 209396 KB Output is correct
111 Correct 1671 ms 206356 KB Output is correct
112 Correct 809 ms 114244 KB Output is correct
113 Correct 739 ms 108808 KB Output is correct