题意:
屠夫的钩子有三种连起来,他可以改变某一段的钩子种类来改变钩子的长度m次。问m次后改变链子的长度。
思路:
这个算是区间赋值吧,在最后在输出整个区间的和,lazy标记和处理完孩子后更新当前节点一个不能少。
因为是赋值,所以lazy标记不要去累加。
Code:
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn=1e5+7;
inline ll read() {
ll s = 0, w = 1;
char ch = getchar();
while (ch < 48 || ch > 57) {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= 48 && ch <= 57)
s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
return s * w;
}
int tree[maxn<<2],rz[maxn<<2];
inline pushdown(int rt,int len) {
if(rz[rt]) {
tree[rt<<1]=rz[rt]*(len-(len>>1));
tree[rt<<1|1]=rz[rt]*(len>>1);
rz[rt<<1]=rz[rt<<1|1]=rz[rt];
rz[rt]=0;
}
}
inline void build(int l,int r,int rt) {
rz[rt]=0;
if(l==r) {
tree[rt]=1;
return;
}
int mid=l+r>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
inline void update(int x,int y,int val,int l,int r,int rt) {
if(x<=l&&y>=r) {
tree[rt]=val*(r-l+1);
rz[rt]=val;
return;
}
int mid=l+r>>1;
pushdown(rt,r-l+1);
if(x<=mid) update(x,y,val,l,mid,rt<<1);
if(y>mid) update(x,y,val,mid+1,r,rt<<1|1);
tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
int main() {
int t=read(),n,m,x,y,z,cas=0;
while(t--) {
n=read(),m=read();
build(1,n,1);
while(m--) {
x=read(),y=read(),z=read();
update(x,y,z,1,n,1);
}
printf("Case %d: The total value of the hook is %d.\n",++cas,tree[1]);
}
return 0;
} 
京公网安备 11010502036488号