1 条题解

  • 0
    @ 2026-1-16 15:27:14

    #include <bits/stdc++.h>
    #define EB emplace_back
    using std::vector;
    
    typedef long long ll;
    typedef std::pair <int, int> pr;
    const int N = 200054;
    
    int n, m;
    int a[N];
    vector <int> ia[N];
    vector <pr> star[N];
    
    struct dsu {
    	int p[N];
    
    	void init(int n) {std::iota(p, p + (n + 1), 0);}
    	int ancestor(int x) {return p[x] == x ? x : (p[x] = ancestor(p[x]));}
    	void link(int x, int y) {p[ancestor(x)] = ancestor(y);} // directed
    } L, R;
    
    namespace BIT {
    	ll x[N];
    
    	ll sum(int h) {ll s = 0; for (; h; h &= h - 1) s += x[h]; return s;}
    	void add(int h, ll v) {assert(h); for (; h <= n; h |= h - 1, ++h) x[h] += v;}
    	inline void add(int l, int r, ll v) {add(l, v), add(r + 1, -v);}
    }
    
    int main() {
    	int i, x, y, w; ll W, ans = 0;
    	scanf("%d", &n), L.init(n + 1), R.init(n + 1);
    	for (i = 1; i <= n; ++i) scanf("%d", a + i), a[i] = n - a[i], ia[a[i]].EB(i);
    	scanf("%d", &m);
    	for (i = 0; i < m; ++i) scanf("%d%d%d", &x, &y, &w), star[n - y + 1].EB(x, w);
    	for (i = n; i; --i) {
    		for (int x : ia[i]) L.link(x, x - 1), R.link(x, x + 1);
    		for (const pr &s : star[i]) {
    			std::tie(x, w) = s, W = BIT::sum(x);
    			if (W < w) ans += W, BIT::add(L.ancestor(x) + 1, R.ancestor(x) - 1, w - W);
    			else ans += w;
    		}
    	}
    	printf("%lld\n", ans);
    	return 0;
    }
    
    
    • 1

    信息

    ID
    8400
    时间
    1000ms
    内存
    512MiB
    难度
    10
    标签
    递交数
    1
    已通过
    1
    上传者