思路:
线段树区查区改模板题,就是忘记开流加速在hdu上超时了n发,记住输入数据大概大于等于1e5时就要用scanf了
代码:
#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <set>
#define fastio ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
using namespace std;
typedef long long ll;
typedef pair<double, int> PII;
const int N = 1e5 + 10;
const int INF = 2e9;
const double eps = 1e-6;
const int mod = 998244353;
ll n, m;
ll lazy[N << 2];
struct node
{
ll l, r, col; //l记录左子树索引,r记录右子树索引,sum记录该节点的子树+自己总和,lz记录修改值
} tree[N << 2];
inline void PushUp(ll idx)
{
tree[idx].col = tree[idx << 1].col + tree[idx << 1 | 1].col; //递归建完左右子树后计算该节点的和
}
inline void PushDown(ll idx) //idx为此时tree的索引
{
if (lazy[idx] != 0)
{
lazy[idx << 1] = lazy[idx];
lazy[idx << 1 | 1] = lazy[idx];
ll mid = (tree[idx].l + tree[idx].r) >> 1;
tree[idx << 1].col = lazy[idx] * (mid - tree[idx << 1].l + 1);
tree[idx << 1 | 1].col = lazy[idx] * (tree[idx << 1 | 1].r - mid);
lazy[idx] = 0;
}
}
void Build(ll idx, ll l, ll r) //i为此时tree的索引
{
lazy[idx] = 0;
tree[idx].l = l;
tree[idx].r = r;
if (l == r) //当左右节点相等则此为叶子节点
{
tree[idx].col = 1;
return;
}
ll mid = (l + r) / 2;
Build(idx << 1, l, mid); //建左子树
Build(idx << 1 | 1, mid + 1, r); //建右子树
PushUp(idx);
}
inline void Update(ll idx, ll l, ll r, ll k)
{
if (tree[idx].l >= l && tree[idx].r <= r) //此时节点区间若在l~r之内
{
tree[idx].col = k * (tree[idx].r - tree[idx].l + 1);
lazy[idx] = k;
return;
}
PushDown(idx);
if (tree[idx << 1].r >= l)
Update(idx << 1, l, r, k);
if (tree[idx << 1 | 1].l <= r)
Update(idx << 1 | 1, l, r, k);
PushUp(idx);
}
inline ll Query(ll idx, ll l, ll r)
{
if (tree[idx].l >= l && tree[idx].r <= r)
return tree[idx].col;
PushDown(idx);
ll num = 0;
if (tree[idx << 1].r >= l)
num += Query(idx << 1, l, r); //若与左子树有交集加上左子树上的
if (tree[idx << 1 | 1].l <= r)
num += Query(idx << 1 | 1, l, r);
return num;
}
int main()
{
fastio;
int T;
cin >> T;
for (int j = 1; j <= T; j++)
{
memset(tree, 0, sizeof tree);
cin >> n >> m;
Build(1, 1, n);
for (int i = 1; i <= m; i++)
{
int l, r, k;
// scanf("%d%d%d", &l, &r, &k);
cin >> l >> r >> k;
Update(1, l, r, k);
}
ll ans = Query(1, 1, n);
cout << "Case " << j << ": The total value of the hook is " << ans << '.' << endl;
}
return 0;
}