问题等价于
有一个数列,初始值均为 \(0\),他进行 \(n\) 次操作,每次将数列 \([a_i,b_i)\) 这个区间中所有比 \(h_i\) 小的数改为 \(h_i\),他想知道 \(n\) 次操作后数列中所有元素的和。
按 \(h_i\)递增排序,这样就是裸的区间覆盖问题了
#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
//#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(x) freopen(#x".in", "r", stdin),freopen(#x".out", "w", stdout)
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef queue <pii> qii ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 100010 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 1000000007 ;
const double eps = 1e-7 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf\n", x + 0.0000000001) ; }
template <class T> void chmin(T &a, T b) { if (a > b) a = b ; }
template <class T> void chmax(T &a, T b) { if (a < b) a = b ; }
int n, t ;
ll ans;
struct node {
int left, right;
int c;
} tree[4 * 40005 * 2];
struct edge {
int left, right, h;
} a[40005];
int p[2 * 40005];
bool cmp(edge e1, edge e2) { return e1.h < e2.h; }
int erfen(int l, int r, int x){
while (l <= r) {
int mid = (l + r) / 2;
if (p[mid] == x) return mid;
else if (p[mid] > x) r = mid - 1;
else l = mid + 1;
}
return 0;
}
void change(int now, int l, int r, int x) {
if (tree[now].right < l || tree[now].left > r) return;
if (tree[now].left >= l && tree[now].right <= r) {
tree[now].c = x;
return;
}
int mid = (tree[now].left + tree[now].right) / 2;
if (tree[now].c) {
tree[now * 2].c = tree[now].c;
tree[now * 2 + 1].c = tree[now].c;
tree[now].c = 0;
}
if (mid >= r) change(now * 2, l, r, x);
else if (mid <= l) change(now * 2 + 1, l, r, x);
else {
change(now * 2, l, r, x);
change(now * 2 + 1, l, r, x);
}
}
void built(int now, int l, int r) {
tree[now].left = l;
tree[now].right = r;
tree[now].c = 0;
if (l == r - 1) return;
built(now * 2, l, (l + r) / 2);
built(now * 2 + 1, (l + r) / 2, r);
}
void quest(int now) {
if (tree[now].c) {
ans += (p[tree[now].right] - p[tree[now].left]) * (long long)tree[now].c;
return;
}
if (tree[now].right == tree[now].left + 1)
return;
quest(now * 2);
quest(now * 2 + 1);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d%d", &a[i].left, &a[i].right, &a[i].h);
p[++t] = a[i].left;
p[++t] = a[i].right;
}
sort(p + 1, p + 1 + 2 * n);
sort(a + 1, a + n + 1, cmp);
built(1, 1, n * 2);
for (int i = 1; i <= n; i++) {
int l = erfen(1, 2 * n, a[i].left);
int r = erfen(1, 2 * n, a[i].right);
change(1, l, r, a[i].h);
}
quest(1);
printf("%lld", ans);
}