Submission #219051

# Submission time Handle Problem Language Result Execution time Memory
219051 2020-04-03T13:53:35 Z Akashi Constellation 3 (JOI20_constellation3) C++14
100 / 100
510 ms 45328 KB
#include <bits/stdc++.h>
using namespace std;

struct stea{
    int x, y, c;
};

int n, m;
stea a[200005];
bool active[200005];
set <pair <int, int> > ev[200005];

struct dsu{
    int par[200005], sz[200005], st[200005], dr[200005];
    long long val[200005];

    void init(int n){
        for(int i = 1; i <= n ; ++i){
            par[i] = i; sz[i] = 1; val[i] = 0;
            st[i] = dr[i] = i;
        }
    }

    int find(int x){
        if(x != par[x]) return (par[x] = find(par[x]));
        return x;
    }

    void merge(int x, int y){
        x = find(x); y = find(y);
        if(x == y) return ;

        if(sz[x] < sz[y]) swap(x, y);
        par[y] = x;

        sz[x] += sz[y];
        val[x] += val[y];

        st[x] = min(st[x], st[y]);
        dr[x] = max(dr[x], dr[y]);
    }

    void update(long long cand, int x){
        x = find(x);
        val[x] = max(val[x], cand);
    }

    bool inside(int point, int wh){
        wh = find(wh);
        if(st[wh] <= point && point <= dr[wh]) return true;
        return false;
    }
};

dsu comp;

struct segTree{
    long long lazy[800005];

    void propag(int nod){
        for(int i = nod * 2; i <= nod * 2 + 1 ; ++i) lazy[i] += lazy[nod];
        lazy[nod] = 0;
    }

    void update(int x, int y, long long val, int st = 1, int dr = n, int nod = 1){
        if(x <= st && dr <= y){
            lazy[nod] += val;
            return ;
        }
        if(lazy[nod]) propag(nod);

        int mij = (st + dr) / 2;
        if(x <= mij) update(x, y, val, st, mij, nod * 2);
        if(mij + 1 <= y) update(x, y, val, mij + 1, dr, nod * 2 + 1);
    }

    long long query(int x, int st = 1, int dr = n, int nod = 1){
        if(st == dr) return lazy[nod];
        if(lazy[nod]) propag(nod);

        int mij = (st + dr) / 2;
        if(x <= mij) return query(x, st, mij, nod * 2);
        return query(x, mij + 1, dr, nod * 2 + 1);
    }

};

segTree A;

int main()
{
//    freopen("1.in", "r", stdin);

    scanf("%d", &n);
    int x;
    for(int i = 1; i <= n ; ++i){
        scanf("%d", &x);
        ev[x + 1].insert({0, i});
    }

    scanf("%d", &m);
    long long Sum = 0;
    for(int i = 1; i <= m ; ++i){
        scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].c);
        ev[a[i].y].insert({1, i});
        Sum = Sum + a[i].c;
    }

    comp.init(n);

    for(int i = 1; i <= n ; ++i){
        int Last = -1;
        long long Max = 0;
        for(auto it : ev[i]){
            int tip = it.first, pos = it.second;
            if(tip == 0){
                active[pos] = 1;
                if(active[pos - 1]){
                    A.update(pos, pos, -comp.val[comp.find(pos - 1)]);
                    comp.merge(pos, pos - 1);
                }
                if(active[pos + 1]){
                    A.update(comp.st[comp.find(pos)], comp.dr[comp.find(pos)], -comp.val[comp.find(pos + 1)]);
                    A.update(comp.st[comp.find(pos + 1)], comp.dr[comp.find(pos + 1)], -comp.val[comp.find(pos)]);
                    comp.merge(pos, pos + 1);
                }
            }
            else{
                if(Last == -1) ;
                else if(!comp.inside(a[pos].x, Last)){
                    int x = comp.find(Last);
                    comp.update(Max, Last);

                    Max = 0;
                }

                Last = a[pos].x;
                Max = max(Max, a[pos].c - A.query(a[pos].x));
            }
        }

        if(Last != -1){
            int x = comp.find(Last);
            comp.update(Max, Last);
        }
    }

    for(int i = 1; i <= n ; ++i)
        if(comp.dr[comp.find(i)] == i) Sum = Sum - comp.val[comp.find(i)];

    printf("%lld", Sum);
    return 0;
}












Compilation message

constellation3.cpp: In function 'int main()':
constellation3.cpp:131:25: warning: unused variable 'x' [-Wunused-variable]
                     int x = comp.find(Last);
                         ^
constellation3.cpp:143:17: warning: unused variable 'x' [-Wunused-variable]
             int x = comp.find(Last);
                 ^
constellation3.cpp:94:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
constellation3.cpp:97:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &x);
         ~~~~~^~~~~~~~~~
constellation3.cpp:101:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &m);
     ~~~~~^~~~~~~~~~
constellation3.cpp:104:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].c);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9856 KB Output is correct
2 Correct 10 ms 9856 KB Output is correct
3 Correct 10 ms 9856 KB Output is correct
4 Correct 10 ms 9856 KB Output is correct
5 Correct 10 ms 9856 KB Output is correct
6 Correct 10 ms 9856 KB Output is correct
7 Correct 10 ms 9856 KB Output is correct
8 Correct 10 ms 9856 KB Output is correct
9 Correct 10 ms 9856 KB Output is correct
10 Correct 10 ms 9856 KB Output is correct
11 Correct 10 ms 9856 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 10 ms 9856 KB Output is correct
14 Correct 11 ms 9856 KB Output is correct
15 Correct 10 ms 9856 KB Output is correct
16 Correct 10 ms 9856 KB Output is correct
17 Correct 11 ms 9856 KB Output is correct
18 Correct 10 ms 9856 KB Output is correct
19 Correct 12 ms 9856 KB Output is correct
20 Correct 11 ms 9856 KB Output is correct
21 Correct 12 ms 9856 KB Output is correct
22 Correct 10 ms 9856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9856 KB Output is correct
2 Correct 10 ms 9856 KB Output is correct
3 Correct 10 ms 9856 KB Output is correct
4 Correct 10 ms 9856 KB Output is correct
5 Correct 10 ms 9856 KB Output is correct
6 Correct 10 ms 9856 KB Output is correct
7 Correct 10 ms 9856 KB Output is correct
8 Correct 10 ms 9856 KB Output is correct
9 Correct 10 ms 9856 KB Output is correct
10 Correct 10 ms 9856 KB Output is correct
11 Correct 10 ms 9856 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 10 ms 9856 KB Output is correct
14 Correct 11 ms 9856 KB Output is correct
15 Correct 10 ms 9856 KB Output is correct
16 Correct 10 ms 9856 KB Output is correct
17 Correct 11 ms 9856 KB Output is correct
18 Correct 10 ms 9856 KB Output is correct
19 Correct 12 ms 9856 KB Output is correct
20 Correct 11 ms 9856 KB Output is correct
21 Correct 12 ms 9856 KB Output is correct
22 Correct 10 ms 9856 KB Output is correct
23 Correct 12 ms 10112 KB Output is correct
24 Correct 12 ms 10112 KB Output is correct
25 Correct 13 ms 10112 KB Output is correct
26 Correct 12 ms 10104 KB Output is correct
27 Correct 12 ms 10112 KB Output is correct
28 Correct 13 ms 10112 KB Output is correct
29 Correct 12 ms 10240 KB Output is correct
30 Correct 14 ms 10112 KB Output is correct
31 Correct 15 ms 10112 KB Output is correct
32 Correct 12 ms 10112 KB Output is correct
33 Correct 12 ms 10112 KB Output is correct
34 Correct 12 ms 10112 KB Output is correct
35 Correct 12 ms 10112 KB Output is correct
36 Correct 12 ms 10112 KB Output is correct
37 Correct 12 ms 10112 KB Output is correct
38 Correct 10 ms 10112 KB Output is correct
39 Correct 12 ms 10112 KB Output is correct
40 Correct 12 ms 10112 KB Output is correct
41 Correct 25 ms 10112 KB Output is correct
42 Correct 14 ms 10112 KB Output is correct
43 Correct 12 ms 10112 KB Output is correct
44 Correct 12 ms 10112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9856 KB Output is correct
2 Correct 10 ms 9856 KB Output is correct
3 Correct 10 ms 9856 KB Output is correct
4 Correct 10 ms 9856 KB Output is correct
5 Correct 10 ms 9856 KB Output is correct
6 Correct 10 ms 9856 KB Output is correct
7 Correct 10 ms 9856 KB Output is correct
8 Correct 10 ms 9856 KB Output is correct
9 Correct 10 ms 9856 KB Output is correct
10 Correct 10 ms 9856 KB Output is correct
11 Correct 10 ms 9856 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 10 ms 9856 KB Output is correct
14 Correct 11 ms 9856 KB Output is correct
15 Correct 10 ms 9856 KB Output is correct
16 Correct 10 ms 9856 KB Output is correct
17 Correct 11 ms 9856 KB Output is correct
18 Correct 10 ms 9856 KB Output is correct
19 Correct 12 ms 9856 KB Output is correct
20 Correct 11 ms 9856 KB Output is correct
21 Correct 12 ms 9856 KB Output is correct
22 Correct 10 ms 9856 KB Output is correct
23 Correct 12 ms 10112 KB Output is correct
24 Correct 12 ms 10112 KB Output is correct
25 Correct 13 ms 10112 KB Output is correct
26 Correct 12 ms 10104 KB Output is correct
27 Correct 12 ms 10112 KB Output is correct
28 Correct 13 ms 10112 KB Output is correct
29 Correct 12 ms 10240 KB Output is correct
30 Correct 14 ms 10112 KB Output is correct
31 Correct 15 ms 10112 KB Output is correct
32 Correct 12 ms 10112 KB Output is correct
33 Correct 12 ms 10112 KB Output is correct
34 Correct 12 ms 10112 KB Output is correct
35 Correct 12 ms 10112 KB Output is correct
36 Correct 12 ms 10112 KB Output is correct
37 Correct 12 ms 10112 KB Output is correct
38 Correct 10 ms 10112 KB Output is correct
39 Correct 12 ms 10112 KB Output is correct
40 Correct 12 ms 10112 KB Output is correct
41 Correct 25 ms 10112 KB Output is correct
42 Correct 14 ms 10112 KB Output is correct
43 Correct 12 ms 10112 KB Output is correct
44 Correct 12 ms 10112 KB Output is correct
45 Correct 503 ms 45176 KB Output is correct
46 Correct 478 ms 44664 KB Output is correct
47 Correct 505 ms 45052 KB Output is correct
48 Correct 510 ms 44820 KB Output is correct
49 Correct 481 ms 44536 KB Output is correct
50 Correct 483 ms 44408 KB Output is correct
51 Correct 473 ms 44536 KB Output is correct
52 Correct 508 ms 45328 KB Output is correct
53 Correct 490 ms 44792 KB Output is correct
54 Correct 389 ms 45176 KB Output is correct
55 Correct 388 ms 45048 KB Output is correct
56 Correct 369 ms 44536 KB Output is correct
57 Correct 381 ms 44664 KB Output is correct
58 Correct 312 ms 44280 KB Output is correct
59 Correct 323 ms 44408 KB Output is correct
60 Correct 181 ms 43384 KB Output is correct
61 Correct 376 ms 44536 KB Output is correct
62 Correct 381 ms 44664 KB Output is correct
63 Correct 364 ms 44536 KB Output is correct
64 Correct 378 ms 43896 KB Output is correct
65 Correct 364 ms 44408 KB Output is correct
66 Correct 366 ms 44792 KB Output is correct