线段树区间更新
菜鸟之伤 __int64!!!!
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 100050;
struct Tree{
int l, r, setv, sum;
}tree[maxn<<2]; // ?
int n;
__int64 sumn;
void pushup(int id){
tree[id].sum = tree[id << 1].sum + tree[id << 1 | 1].sum;
}
void pushdown(int id){
if(tree[id].l >= tree[id].r)
return;
if(tree[id].setv){
tree[id<<1].setv = tree[id<<1|1].setv = tree[id].setv;
tree[id<<1].sum = (tree[id<<1].r-tree[id<<1].l+1)*tree[id].setv;
tree[id<<1|1].sum = (tree[id<<1|1].r-tree[id<<1|1].l+1)*tree[id].setv;
tree[id].setv = 0;
}
}
void build(int id, int l, int r){
tree[id].l = l;
tree[id].r = r;
tree[id].setv = 0;
if(tree[id].l == tree[id].r){
tree[id].sum = 1;
return ;
}
int mid = (l + r) >>1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
pushup(id);
}
void query(int id, int l, int r){
if(tree[id].l >= l && tree[id].r <= r){
sumn += tree[id].sum;
return ;
}
pushdown(id);
int mid = ( tree[id].l + tree[id].r) >> 1;
if(l <= mid){
query(id<<1, l, r);
}
if(r > mid){
query(id<<1|1, l, r);
}
pushup(id);
}
void updateSet(int id,int l,int r,int val){
if(tree[id].l >= l && tree[id].r <= r){
tree[id].setv = val;
tree[id].sum = (tree[id].r-tree[id].l+1)*val;
return;
}
pushdown(id);
int mid = (tree[id].l+tree[id].r)>>1;
if(l <= mid)
updateSet(id<<1,l,r,val);
if(mid < r)
updateSet((id<<1)+1,l,r,val);
pushup(id);
}
int main(){
int t , q, x, y, z, num = 1;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
build(1,1,n);
scanf("%d", &q);
while(q--){
scanf("%d%d%d", &x, &y, &z);
updateSet(1,x,y,z);
}
sumn = 0;
query(1,1,n);
printf("Case %d: The total value of the hook is %I64d.\n", num++, sumn);
}
return 0;
}