动态开节点线段树

动态开节点线段树

题意

题目链接

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;
}
05-08 15:29