You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 100010;
int n, q;
struct Node {
int l, r;
LL sum;
int tag;
} tr[N << 2];
void pushup(int u) {
tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum;
}
void bulid(int u, int l, int r) {
tr[u] = {l, r};
if (l == r) {
tr[u].sum = 1;
return;
}
int mid = (l + r) >> 1;
bulid(u << 1, l, mid), bulid(u << 1 | 1, mid + 1, r);
pushup(u);
}
void pushdown(int u) {
Node &root = tr[u], &ls = tr[u << 1], &rs = tr[u << 1 | 1];
if (root.tag) { // 如果懒标记不为零,需要向儿子们传递懒标记
ls.tag = root.tag, ls.sum = (LL)(ls.r - ls.l + 1) * root.tag;
rs.tag = root.tag, rs.sum = (LL)(rs.r - rs.l + 1) * root.tag;
root.tag = 0; // 清空懒标记
}
}
void modify(int u, int l, int r, int v) {
if (l <= tr[u].l && tr[u].r <= r) {
tr[u].sum = (LL)v * (tr[u].r - tr[u].l + 1);
tr[u].tag = v;
return;
}
pushdown(u); // 没有完全覆盖则分裂
int mid = (tr[u].l + tr[u].r) >> 1;
if (l <= mid) modify(u << 1, l, r, v);
if (r > mid) modify(u << 1 | 1, l, r, v);
pushup(u);
}
LL query(int u, int l, int r) {
if (l > tr[u].r || r < tr[u].l) return 0; // 递归出口不在我管辖范围内的情况返回0
if (l <= tr[u].l && tr[u].r <= r) return tr[u].sum; // 完整区间覆盖,返回区间和
// 没有完全命中,开始分裂
pushdown(u);
// 左边+右边
return query(u << 1, l, r) + query(u << 1 | 1, l, r);
}
int main() {
// 加快读入
ios::sync_with_stdio(false), cin.tie(0);
int T;
cin >> T;
int cas = 1;
while (T--) {
cin >> n; // 线段的区间[1~n]
memset(tr, 0, sizeof tr); // 因树状数组多次使用,每次需要清空
bulid(1, 1, n); // 构建树状数组
cin >> q;
while (q--) {
int l, r, d;
cin >> l >> r >> d;
modify(1, l, r, d);
}
printf("Case %d: The total value of the hook is %lld.\n", cas++, query(1, 1, n));
}
return 0;
}