BZOJ4636: 蒟蒻的数列(动态开节点线段树)
题意
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;
}
作者:自为风月马前卒
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。