답안 #212586

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
212586 2020-03-23T16:30:22 Z BThero 별자리 3 (JOI20_constellation3) C++17
100 / 100
390 ms 37880 KB
#define DBG 1
// chrono::system_clock::now().time_since_epoch().count()
#include<bits/stdc++.h>
//#include<ext/pb_ds/tree_policy.hpp>
//#include<ext/pb_ds/assoc_container.hpp>

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define debug(x) if (DBG) cerr << #x << " = " << x << endl;

using namespace std;
//using namespace __gnu_pbds;

typedef long long ll;
typedef pair<int, int> pii;

//template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int MAXN = (int)2e5 + 5;

vector<int> pos[MAXN];
vector<pii> stars[MAXN];
ll dp[MAXN], ext[MAXN], ext2[MAXN];
ll t[MAXN << 2];
int par[MAXN], L[MAXN], R[MAXN], sz[MAXN];
int arr[MAXN];
bool active[MAXN];
ll sum;
int n, m;

void update(int v, int tl, int tr, int l, int r, ll x) {
    if (l > r || tl > r || tr < l) {
        return;
    }
    
    if (l <= tl && tr <= r) {
        t[v] += x;
        return;
    }
    
    int mid = (tl + tr) >> 1;
    int c1 = (v << 1), c2 = (c1 | 1);
    update(c1, tl, mid, l, r, x);
    update(c2, mid + 1, tr, l, r, x);
}

ll getVal(int v, int tl, int tr, int p) {
    if (tl == tr) {
        return t[v];
    }
    
    int mid = (tl + tr) >> 1;
    int c1 = (v << 1), c2 = (c1 | 1);
    
    if (p <= mid) {
        return t[v] + getVal(c1, tl, mid, p);
    }
    else {
        return t[v] + getVal(c2, mid + 1, tr, p);
    }
}

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

void uni(int a, int b) {
    a = getPar(a);
    b = getPar(b);
    
    if (a == b) {
        return;
    }
    
    if (sz[a] < sz[b]) {
        swap(a, b);
    }
    
    par[b] = a;
    sz[a] += sz[b];
    dp[a] += ext2[a];
    ext[a] += ext2[a];
    ext2[a] = 0;
    dp[b] += ext2[b];
    ext[b] += ext2[b];
    ext2[b] = 0;
    //if (ext[b]) printf("U %d %d %d\n", L[a], R[a], ext[b]);
    //if (ext[a]) printf("U %d %d %d\n", L[b], R[b], ext[a]);
    update(1, 1, n, L[a], R[a], ext[b]);
    update(1, 1, n, L[b], R[b], ext[a]);
    ext[a] += ext[b];
    dp[a] += dp[b];
    L[a] = min(L[a], L[b]);
    R[a] = max(R[a], R[b]);
}

void solve() {
	scanf("%d", &n);
    
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &arr[i]);
        pos[arr[i]].pb(i);
    }
    
    scanf("%d", &m);
    
    for (int i = 1; i <= m; ++i) {
        int x, y, c;
        scanf("%d %d %d", &x, &y, &c);
        stars[y].pb(mp(x, c));
        sum += c;
    }
    
    for (int i = 1; i <= n; ++i) {
        par[i] = i;
        L[i] = R[i] = i;
        dp[i] = ext[i] = 0;
        active[i] = 0;
    }
    
    for (int y = 1; y <= n; ++y) {
        //printf("%d:\n", y);
        
        //for (int x = 1; x <= n; ++x) {
            //if (active[x] && getPar(x) == x) {
                //printf("%d - (%d %d) - %lld %lld %lld\n", x, L[x], R[x], dp[x], ext[x], ext2[x]);
            //}
        //}
    
        for (auto it : stars[y]) {
            int x, c;
            tie(x, c) = it;
            int p = getPar(x);
            ext2[p] = max(ext2[p], c + getVal(1, 1, n, x) - dp[p]);
        }
        
        //printf("%d:\n", y);
        
        //for (int x = 1; x <= n; ++x) {
            //if (active[x] && getPar(x) == x) {
                //printf("%d - (%d %d) - %lld %lld %lld\n", x, L[x], R[x], dp[x], ext[x], ext2[x]);
            //}
        //}
        
        for (int x : pos[y]) {
            active[x] = 1;
        
            if (active[x - 1]) {
                uni(x - 1, x);
            }
            
            if (active[x + 1]) {
                uni(x, x + 1);
            }
        }
    }
    
    ll ans = 0;
    
    for (int i = 1; i <= n; ++i) {
        if (getPar(i) == i) {
            ans = max(ans, dp[i] + ext2[i]);
        }
    }
    
    printf("%lld\n", sum - ans);
}

int main() {
	int tt = 1;
	
	while (tt--) {
		solve();
	}

	return 0;
}

Compilation message

constellation3.cpp: In function 'void solve()':
constellation3.cpp:106:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
constellation3.cpp:109:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &arr[i]);
         ~~~~~^~~~~~~~~~~~~~~
constellation3.cpp:113:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &m);
     ~~~~~^~~~~~~~~~
constellation3.cpp:117:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d", &x, &y, &c);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9856 KB Output is correct
2 Correct 12 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 9 ms 9856 KB Output is correct
6 Correct 10 ms 9856 KB Output is correct
7 Correct 9 ms 9856 KB Output is correct
8 Correct 9 ms 9728 KB Output is correct
9 Correct 11 ms 9856 KB Output is correct
10 Correct 9 ms 9856 KB Output is correct
11 Correct 9 ms 9856 KB Output is correct
12 Correct 10 ms 9856 KB Output is correct
13 Correct 10 ms 9856 KB Output is correct
14 Correct 10 ms 9856 KB Output is correct
15 Correct 9 ms 9856 KB Output is correct
16 Correct 10 ms 9856 KB Output is correct
17 Correct 10 ms 9856 KB Output is correct
18 Correct 10 ms 9856 KB Output is correct
19 Correct 10 ms 9856 KB Output is correct
20 Correct 10 ms 9856 KB Output is correct
21 Correct 10 ms 9856 KB Output is correct
22 Correct 11 ms 9856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9856 KB Output is correct
2 Correct 12 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 9 ms 9856 KB Output is correct
6 Correct 10 ms 9856 KB Output is correct
7 Correct 9 ms 9856 KB Output is correct
8 Correct 9 ms 9728 KB Output is correct
9 Correct 11 ms 9856 KB Output is correct
10 Correct 9 ms 9856 KB Output is correct
11 Correct 9 ms 9856 KB Output is correct
12 Correct 10 ms 9856 KB Output is correct
13 Correct 10 ms 9856 KB Output is correct
14 Correct 10 ms 9856 KB Output is correct
15 Correct 9 ms 9856 KB Output is correct
16 Correct 10 ms 9856 KB Output is correct
17 Correct 10 ms 9856 KB Output is correct
18 Correct 10 ms 9856 KB Output is correct
19 Correct 10 ms 9856 KB Output is correct
20 Correct 10 ms 9856 KB Output is correct
21 Correct 10 ms 9856 KB Output is correct
22 Correct 11 ms 9856 KB Output is correct
23 Correct 12 ms 9984 KB Output is correct
24 Correct 12 ms 9984 KB Output is correct
25 Correct 16 ms 9984 KB Output is correct
26 Correct 16 ms 10032 KB Output is correct
27 Correct 15 ms 9984 KB Output is correct
28 Correct 12 ms 9984 KB Output is correct
29 Correct 12 ms 9984 KB Output is correct
30 Correct 12 ms 9984 KB Output is correct
31 Correct 12 ms 9984 KB Output is correct
32 Correct 12 ms 9984 KB Output is correct
33 Correct 13 ms 10240 KB Output is correct
34 Correct 14 ms 10112 KB Output is correct
35 Correct 16 ms 10088 KB Output is correct
36 Correct 15 ms 9984 KB Output is correct
37 Correct 14 ms 9984 KB Output is correct
38 Correct 11 ms 9984 KB Output is correct
39 Correct 12 ms 9984 KB Output is correct
40 Correct 11 ms 9984 KB Output is correct
41 Correct 12 ms 9984 KB Output is correct
42 Correct 11 ms 9984 KB Output is correct
43 Correct 12 ms 9984 KB Output is correct
44 Correct 11 ms 9984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9856 KB Output is correct
2 Correct 12 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 9 ms 9856 KB Output is correct
6 Correct 10 ms 9856 KB Output is correct
7 Correct 9 ms 9856 KB Output is correct
8 Correct 9 ms 9728 KB Output is correct
9 Correct 11 ms 9856 KB Output is correct
10 Correct 9 ms 9856 KB Output is correct
11 Correct 9 ms 9856 KB Output is correct
12 Correct 10 ms 9856 KB Output is correct
13 Correct 10 ms 9856 KB Output is correct
14 Correct 10 ms 9856 KB Output is correct
15 Correct 9 ms 9856 KB Output is correct
16 Correct 10 ms 9856 KB Output is correct
17 Correct 10 ms 9856 KB Output is correct
18 Correct 10 ms 9856 KB Output is correct
19 Correct 10 ms 9856 KB Output is correct
20 Correct 10 ms 9856 KB Output is correct
21 Correct 10 ms 9856 KB Output is correct
22 Correct 11 ms 9856 KB Output is correct
23 Correct 12 ms 9984 KB Output is correct
24 Correct 12 ms 9984 KB Output is correct
25 Correct 16 ms 9984 KB Output is correct
26 Correct 16 ms 10032 KB Output is correct
27 Correct 15 ms 9984 KB Output is correct
28 Correct 12 ms 9984 KB Output is correct
29 Correct 12 ms 9984 KB Output is correct
30 Correct 12 ms 9984 KB Output is correct
31 Correct 12 ms 9984 KB Output is correct
32 Correct 12 ms 9984 KB Output is correct
33 Correct 13 ms 10240 KB Output is correct
34 Correct 14 ms 10112 KB Output is correct
35 Correct 16 ms 10088 KB Output is correct
36 Correct 15 ms 9984 KB Output is correct
37 Correct 14 ms 9984 KB Output is correct
38 Correct 11 ms 9984 KB Output is correct
39 Correct 12 ms 9984 KB Output is correct
40 Correct 11 ms 9984 KB Output is correct
41 Correct 12 ms 9984 KB Output is correct
42 Correct 11 ms 9984 KB Output is correct
43 Correct 12 ms 9984 KB Output is correct
44 Correct 11 ms 9984 KB Output is correct
45 Correct 360 ms 35708 KB Output is correct
46 Correct 369 ms 35576 KB Output is correct
47 Correct 390 ms 35576 KB Output is correct
48 Correct 367 ms 35704 KB Output is correct
49 Correct 347 ms 35320 KB Output is correct
50 Correct 370 ms 35160 KB Output is correct
51 Correct 349 ms 35320 KB Output is correct
52 Correct 378 ms 35836 KB Output is correct
53 Correct 359 ms 35576 KB Output is correct
54 Correct 303 ms 37752 KB Output is correct
55 Correct 299 ms 37880 KB Output is correct
56 Correct 284 ms 37496 KB Output is correct
57 Correct 292 ms 37496 KB Output is correct
58 Correct 257 ms 32248 KB Output is correct
59 Correct 282 ms 32632 KB Output is correct
60 Correct 204 ms 35832 KB Output is correct
61 Correct 238 ms 31084 KB Output is correct
62 Correct 289 ms 35192 KB Output is correct
63 Correct 224 ms 30864 KB Output is correct
64 Correct 217 ms 30584 KB Output is correct
65 Correct 286 ms 35248 KB Output is correct
66 Correct 224 ms 30576 KB Output is correct