题目:
题意:
给一串初始值全为1的数字串,X Y Z表示把下标X—Y的值全改为Z,求所有操作之后的所有数字之和
代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include "iostream"
#include "cstring"
#include "algorithm"
#include "cmath"
#include "cstdio"
#include "sstream"
#include "queue"
#include "vector"
#include "string"
#include "stack"
#include "cstdlib"
#include "deque"
#include "fstream"
#include "map"
using namespace std;
typedef long long LL;
const int INF = 522133279;
const int MAXN = 100000+100;
#define eps 1e-14
const int mod = 1000000007;
struct tt
{
int l,r;
int v; //当前区间总和
int type; //当前区间的元素类型(1,2,3)
void ca(int tmp)
{
type = tmp;
v = (r-l+1)*tmp;
}
}tree[MAXN*3];
void build(int root , int l , int r)
{
tree[root].l = l;
tree[root].r = r;
tree[root].ca(1);
if(l == r)
return ;
int mid = (l+r)/2;
build(root*2,l,mid);
build(root*2+1,mid+1,r);
}
void up(int root , int l, int r , int type)
{
if(r >= tree[root].r && l <= tree[root].l) //假如当前覆盖区间被包含在目的更新区间内,直接改变此覆盖区间的type类型和v值
{
tree[root].ca(type);
return;
}
//否则
if(tree[root].l != tree[root].r && tree[root].type) //很重要的延迟处理,当前区间的type值不为0,表示其子区间与其有相同的状态,可以刷新子区间的v值,
//否则表示其子区间必定已被修改,type肯定不同,不能更新,需进行后续处理
{
tree[2*root].ca(tree[root].type);
tree[2*root+1].ca(tree[root].type);
tree[root].type=0;
}
int mid = (tree[root].l + tree[root].r)/2;
if(l <= mid) //如果目地区间与子区间有交集,则更新子区间
up(root*2,l,r,type);
if(r > mid)
up(root*2+1,l,r,type);
tree[root].v = tree[2*root].v + tree[2*root+1].v;
}
int main()
{
//freopen("in","r",stdin);
//freopen("out","w",stdout);
int t;
scanf("%d",&t);
for(int ka = 1 ; ka <= t ; ka++)
{
int n;
scanf("%d",&n);
memset(tree,0,sizeof(tree));
build(1,1,n);
int q;
scanf("%d",&q);
for(int i = 0 ; i < q ; i++)
{
int a,x,y;
scanf("%d%d%d",&x,&y,&a);
up(1,x,y,a);
}
printf("Case %d: The total value of the hook is %d.\n",ka,tree[1].v);
}
return 0;
}