Submission #476882

# Submission time Handle Problem Language Result Execution time Memory
476882 2021-09-28T21:02:34 Z nafis_shifat Road Construction (JOI21_road_construction) C++17
100 / 100
2043 ms 743580 KB
#include<bits/stdc++.h>
#define ll long long
#define pii pair<ll,int>
#define f first
#define s second
using namespace std;
const int mxn = 2e6+5;
const ll inf = 1e15;
pii PP = {inf, -1};
struct node
{
    node *left,*right;
    pii val;
    node(pii _v = PP, node *l = NULL, node *r = NULL) : val(_v), left(l), right(r){};
    node *update(int l,int r,int p,ll v) {
        if(l > p || r < p) return this;
        if(l == r) {
            return new node({v, l});
        }


        int mid = l + r >> 1;
        node *ret = new node();
        if(left == NULL) left = new node();
        if(right == NULL) right = new node();
        ret-> left = left-> update(l, mid, p, v);
        ret-> right = right-> update(mid + 1, r, p, v);
        ret-> val = min(ret-> left -> val, ret-> right -> val);
        return ret;
    }

    pii query(int l,int r, int s, int t) {
        if(l > t || r < s) return {inf,-1};
        if(l >= s && r <= t) return val;

        int mid = l + r >> 1;

        if(left == NULL) left = new node();
        if(right == NULL) right = new node();

        return min(left-> query(l, mid, s, t), right-> query(mid + 1, r, s, t));
    }
}*root1[mxn], *root2[mxn];


int pos[mxn], inv[mxn];

int n,k;
vector<pair<int,int>> pnt;

int main() {
    cin >> n >> k;

    root1[0] = new node(PP, new node(), new node());

    root2[0] = new node(PP, new node(), new node());

   



    vector<pair<int,int>> vv;
    for(int i = 1; i <= n; i++) {
        int x,y;

        assert(scanf("%d %d",&x,&y));
        vv.emplace_back(y, x);
        pnt.emplace_back(x, y);
    }

    sort(pnt.begin(), pnt.end());
    pnt.insert(pnt.begin(), {0,0});

    sort(vv.begin(),vv.end());


    priority_queue< tuple<ll,int, int, int> > pq;




    for(int i = 1; i <= n; i++) {
        auto [x,y] = pnt[i];
        pos[i] = lower_bound(vv.begin(), vv.end(), make_pair(y, x)) - vv.begin() + 1;
        inv[pos[i]] = i;




        pii ed1 = root1[i - 1]-> query(1, n, pos[i] + 1, n);

        pii ed2 = root2[i - 1]-> query(1, n, 1, pos[i] - 1);


        pq.push({-(ed1.f + x - y), pos[i], ed1.s, 1});
        pq.push({-(ed2.f + x + y), pos[i], ed2.s, -1});


        root1[i] = root1[i - 1]-> update(1, n, pos[i], y - x);
        root2[i] = root2[i - 1]-> update(1, n, pos[i], - y - x);

        


    }



    while(k--) {
        auto [dist, u, v, tp] = pq.top();
        pq.pop();
        dist = -dist;

        u = inv[u];
        v = inv[v];

        printf("%lld\n", dist);

        if(tp == 1) {
            root1[u] = root1[u]-> update(1, n, pos[v], inf);
            pii ed = root1[u]-> query(1, n, pos[u] + 1, n);

            auto [x,y] = pnt[u];
            pq.push({-(ed.f + x - y), pos[u], ed.s, 1});


        } else {
            root2[u] = root2[u]-> update(1, n, pos[v], inf);
            pii ed = root2[u]-> query(1, n, 1, pos[u] - 1);
            auto [x,y] = pnt[u];
            pq.push({-(ed.f + x + y), pos[u], ed.s, -1});

        }




    }


    
}

Compilation message

road_construction.cpp: In constructor 'node::node(std::pair<long long int, int>, node*, node*)':
road_construction.cpp:13:9: warning: 'node::val' will be initialized after [-Wreorder]
   13 |     pii val;
      |         ^~~
road_construction.cpp:12:11: warning:   'node* node::left' [-Wreorder]
   12 |     node *left,*right;
      |           ^~~~
road_construction.cpp:14:5: warning:   when initialized here [-Wreorder]
   14 |     node(pii _v = PP, node *l = NULL, node *r = NULL) : val(_v), left(l), right(r){};
      |     ^~~~
road_construction.cpp: In member function 'node* node::update(int, int, int, long long int)':
road_construction.cpp:22:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   22 |         int mid = l + r >> 1;
      |                   ~~^~~
road_construction.cpp: In member function 'std::pair<long long int, int> node::query(int, int, int, int)':
road_construction.cpp:36:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   36 |         int mid = l + r >> 1;
      |                   ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 414 ms 133036 KB Output is correct
2 Correct 376 ms 133060 KB Output is correct
3 Correct 342 ms 127684 KB Output is correct
4 Correct 328 ms 127428 KB Output is correct
5 Correct 344 ms 131908 KB Output is correct
6 Correct 5 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1374 ms 737772 KB Output is correct
2 Correct 1437 ms 737712 KB Output is correct
3 Correct 244 ms 127428 KB Output is correct
4 Correct 1139 ms 737512 KB Output is correct
5 Correct 1141 ms 737740 KB Output is correct
6 Correct 1142 ms 737772 KB Output is correct
7 Correct 1210 ms 736932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1202 ms 513796 KB Output is correct
2 Correct 1202 ms 514084 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 773 ms 513844 KB Output is correct
5 Correct 922 ms 513948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1202 ms 513796 KB Output is correct
2 Correct 1202 ms 514084 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 773 ms 513844 KB Output is correct
5 Correct 922 ms 513948 KB Output is correct
6 Correct 1199 ms 513820 KB Output is correct
7 Correct 1223 ms 518972 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 1238 ms 518952 KB Output is correct
11 Correct 759 ms 516828 KB Output is correct
12 Correct 908 ms 519292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 414 ms 133036 KB Output is correct
2 Correct 376 ms 133060 KB Output is correct
3 Correct 342 ms 127684 KB Output is correct
4 Correct 328 ms 127428 KB Output is correct
5 Correct 344 ms 131908 KB Output is correct
6 Correct 5 ms 2636 KB Output is correct
7 Correct 1125 ms 405736 KB Output is correct
8 Correct 1162 ms 405792 KB Output is correct
9 Correct 316 ms 127816 KB Output is correct
10 Correct 951 ms 405108 KB Output is correct
11 Correct 1073 ms 404828 KB Output is correct
12 Correct 817 ms 405624 KB Output is correct
13 Correct 812 ms 404208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 414 ms 133036 KB Output is correct
2 Correct 376 ms 133060 KB Output is correct
3 Correct 342 ms 127684 KB Output is correct
4 Correct 328 ms 127428 KB Output is correct
5 Correct 344 ms 131908 KB Output is correct
6 Correct 5 ms 2636 KB Output is correct
7 Correct 1374 ms 737772 KB Output is correct
8 Correct 1437 ms 737712 KB Output is correct
9 Correct 244 ms 127428 KB Output is correct
10 Correct 1139 ms 737512 KB Output is correct
11 Correct 1141 ms 737740 KB Output is correct
12 Correct 1142 ms 737772 KB Output is correct
13 Correct 1210 ms 736932 KB Output is correct
14 Correct 1202 ms 513796 KB Output is correct
15 Correct 1202 ms 514084 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 773 ms 513844 KB Output is correct
18 Correct 922 ms 513948 KB Output is correct
19 Correct 1199 ms 513820 KB Output is correct
20 Correct 1223 ms 518972 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 1238 ms 518952 KB Output is correct
24 Correct 759 ms 516828 KB Output is correct
25 Correct 908 ms 519292 KB Output is correct
26 Correct 1125 ms 405736 KB Output is correct
27 Correct 1162 ms 405792 KB Output is correct
28 Correct 316 ms 127816 KB Output is correct
29 Correct 951 ms 405108 KB Output is correct
30 Correct 1073 ms 404828 KB Output is correct
31 Correct 817 ms 405624 KB Output is correct
32 Correct 812 ms 404208 KB Output is correct
33 Correct 2043 ms 743448 KB Output is correct
34 Correct 2041 ms 743416 KB Output is correct
35 Correct 1816 ms 742780 KB Output is correct
36 Correct 1499 ms 743580 KB Output is correct
37 Correct 1429 ms 743448 KB Output is correct
38 Correct 1529 ms 742152 KB Output is correct