Submission #212586

#TimeUsernameProblemLanguageResultExecution timeMemory
212586BTheroConstellation 3 (JOI20_constellation3)C++17
100 / 100
390 ms37880 KiB
#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 (stderr)

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);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...