欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

BZOJ4636: 蒟蒻的数列(动态开节点线段树)

程序员文章站 2022-05-25 19:49:20
题意 "题目链接" Sol 直接上动态开节点线段树 因为只有一次询问,所以中途不需要下传标记 cpp include define LL long long using namespace std; const int MAXN = 8e6 + 10, INF = 1e9 + 10; templat ......

题意

题目链接

sol

直接上动态开节点线段树

因为只有一次询问,所以中途不需要下传标记

#include<bits/stdc++.h> 
#define ll long long 
using namespace std;
const int maxn = 8e6 + 10, inf = 1e9 + 10;
template <typename a, typename b> inline bool chmin(a &a, b b){if(a > b) {a = b; return 1;} return 0;}
template <typename a, typename b> inline bool chmax(a &a, b b){if(a < b) {a = b; return 1;} return 0;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int n, rt, ls[maxn], rs[maxn], mx[maxn], tot;
void intmax(int &k, int l, int r, int ll, int rr, int val) {
    if(!k) k = ++tot;
    if(ll <= l && r <= rr) {chmax(mx[k], val); return ;}
    int mid = l + r >> 1;
    if(ll <= mid) intmax(ls[k], l, mid, ll, rr, val);
    if(rr  > mid) intmax(rs[k], mid + 1, r, ll, rr, val);
}
ll query(int k, int l, int r, int val) {
    chmax(mx[k], val); chmax(val, mx[k]);
    if(l == r) return mx[k];
    int mid = l + r >> 1;ll ans = 0;
    if(ls[k]) ans += query(ls[k], l, mid, val);
    else ans += (mid - l + 1) * mx[k];
    if(rs[k]) ans += query(rs[k], mid + 1, r, val);
    else ans += (r - mid) * mx[k];
    return ans;
}
signed main() {
    n = read();
    for(int i = 1; i <= n; i++) {
        int l = read(), r = read() - 1, k = read();
        intmax(rt, 1, inf, l, r, k);
    }
    printf("%lld\n", query(rt, 1, inf, 0));
    return 0;
}