跳到正文

剪贴板

剪贴板 o6t0jave

awapwq233
原文
剪贴板信息

剪贴板信息

发布
收录
更新

ACAC CODECODE

C++
#include <cstdio>
#include <vector>
using namespace std;
typedef pair<int, int> P;
const int N = 2e5 + 9;
int n, m, x, y, c;
long long t[N], ans, v;
vector<int>h[N];
vector<P>s[N];
struct DSU 
{
    int fa[N];
    void init() 
    {
        for(int i = 1; i <= n + 1; i++)
            fa[i] = i;
    }
    int gef(int x) 
    {
        return fa[x] == x ? x : fa[x] = gef(fa[x]);
    }
} l, r;
void add(int k, long long d) 
{
    for (; k <= n + 1; k += k & -k)
        t[k] += d;
}
long long qry(int k) 
{
    long long s = 0;
    for (; k; k -= k & -k)
        s += t[k];
    return s;
}
inline int read() 
{
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int main() 
{
    n = read();
    l.init();
    r.init();
    for(int i = 1; i <= n; i++) 
        m = read(), h[m].push_back(i);
    m = read();
    for(int i = 1; i <= m; i++) 
        x = read(), y = read(), c = read(), s[y].push_back(P(x, c));
    for(int i = 1; i <= n; i++) 
    {
        for (auto b : s[i])
        {
            if ((v = qry(b.first)) >= b.second)
                ans += b.second;
            else 
            {
                ans += v;
                add(l.gef(b.first) + 1, b.second - v);
                add(r.gef(b.first), v - b.second);
            }
        }
        for (auto x : h[i])
            l.fa[x] = x - 1, r.fa[x] = x + 1;
    }
    printf("%lld", ans);
}