kuangbin_SegTree E (HDU 1698)

时间:2023-01-28 04:14:40

POJ服务器炸了 还没好呢

然后就只能跳掉一些题目了

这题也是成段更新模板题 本来lazy标记不是很明白 后来学长上课讲了一下就知道原理了 回去看看代码很容易就理解了

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#define INF 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1 | 1
using namespace std;
typedef long long LL; const int MAXN = 1e5 + ;
LL col[MAXN<<], sum[MAXN<<]; inline void pushup(int rt)
{
sum[rt] = sum[rt<<] + sum[rt<<|];
}
inline void pushdown(int rt, int len)
{
if(col[rt]){
col[rt<<] = col[rt];
col[rt<<|] = col[rt];
sum[rt<<] = (len - (len>>)) * col[rt];
sum[rt<<|] = (len>>) * col[rt];
col[rt] = ;
}
} void build(int l, int r, int rt)
{
col[rt] = ;
if(l == r){
sum[rt] = ;
return;
}
int m = (l + r) >> ;
build(lson);
build(rson);
pushup(rt);
} void update(int L, int R, LL c, int l, int r, int rt)
{
if(L <= l && R >= r){
//printf("Update %d - %d\n", l, r);
col[rt] = c;
sum[rt] = (r - l + ) * c;
return;
}
pushdown(rt, r - l + );
int m = (l + r) >> ;
if(L <= m) update(L, R, c, lson);
if(R > m) update(L, R, c, rson);
pushup(rt);
} LL query(int L, int R, int l, int r, int rt)
{
if(L <= l && R >= r){
return sum[rt];
}
pushdown(rt, r - l + );
int m = (l + r) >> ;
LL res = ;
if(L <= m) res += query(L, R, lson);
if(R > m) res += query(L, R, rson);
return res;
} int main()
{
int t, n, q;
scanf("%d", &t);
for(int kase = ; kase <= t; kase++){
scanf("%d%d", &n, &q);
build(, n, );
while(q--){
int l, r;
LL c;
scanf("%d%d%I64d", &l, &r, &c);
update(l, r, c, , n, );
}
printf("Case %d: The total value of the hook is %I64d.\n",
kase, query(, n, , n, ));
}
return ;
}