Submission #578500

#TimeUsernameProblemLanguageResultExecution timeMemory
578500alireza_kavianiConstellation 3 (JOI20_constellation3)C++17
100 / 100
250 ms37404 KiB
#include <bits/stdc++.h>
using namespace std;

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

#define all(x)      (x).begin(),(x).end()
#define X           first
#define Y           second
#define sep         ' '
#define endl        '\n'
#define SZ(x)       ll(x.size())

const ll MAXN = 2e5 + 10;
const ll LOG = 22;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; //998244353; //1e9 + 9;

ll n , m , S , cnt , A[MAXN] , sum[MAXN] , sz[MAXN] , tot[MAXN] , par[MAXN] , dp[MAXN] , L[MAXN] , R[MAXN];
vector<int> building[MAXN];
vector<pii> star[MAXN];

int Find(int v){
	return (par[v] == -1 ? v : par[v] = Find(par[v]));
}

void merge(int v , int u){
	v = Find(v) , u = Find(u);
	if(u == v)	return;
	if(sz[v] < sz[u])	swap(v , u);
	sz[v] += sz[u];
	par[u] = v;
	sum[v] += tot[u];
	for(int i = L[u] ; i <= R[u] ; i++){
		dp[i] += sum[u] + tot[v] - sum[v];
	}
	tot[v] += tot[u];
	L[v] = min(L[v] , L[u]);
	R[v] = max(R[v] , R[u]);
}

int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	fill(par , par + MAXN , -1);
	fill(sz , sz + MAXN , 1);

	cin >> n;
	for(int i = 1 ; i <= n ; i++){
		cin >> A[i];
		building[A[i] + 1].push_back(i);
		L[i] = R[i] = i;
		dp[i] = 0;
	}
	cin >> m;
	for(int i = 1 ; i <= m ; i++){
		int x , y , c;
		cin >> x >> y >> c;
		star[y].push_back({x , c});
		S += c;
	}
	for(int i = 1 ; i <= n + 1 ; i++){
		for(int j : building[i]){
			if(j > 1 && A[j - 1] < i)	merge(j - 1 , j);
			if(j < n && A[j + 1] < i)	merge(j , j + 1);
		}
		for(pii j : star[i]){
			int pos = j.X , val = j.Y , comp = Find(pos);
			tot[comp] = max(tot[comp] , dp[pos] + sum[comp] + val);
		}
	}
	cout << S - tot[Find(1)] << endl;

    return 0;
}
/*

*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...