@Chilling
2017-02-16T17:58:49.000000Z
字数 3944
阅读 951
线段树
Description
In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.
Now Pudge wants to do some operations on the hook.
Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:
For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.
Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.
Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer
Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
Output
For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
Sample Input
1
10
2
1 5 2
5 9 3
Sample Output
Case 1: The total value of the hook is 24.
题意:最初钩子每单位长度价值为1,进行q次操作,每次输入x,y和z,表示区间范围和替换的价值。每组测试数据输出一个总价值。
大致思路:每次找到对应节点后标记,并按照lazy标记修改当前节点的sum值。由于是用价值z直接替换区间内的值,所以lazy不用累加,直接替换。
void build(int id,int l,int r)
{
s[id].l=l;
s[id].r=r;
s[id].lazy=0; //初始为0,表示没有进行替换
if(s[id].l==s[id].r)
s[id].sum=1;
else
{
int mid=(l+r)/2;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
s[id].sum=s[id*2].sum+s[id*2+1].sum;
}
}
void pushdown(int id)
{
s[id*2].lazy=s[id].lazy; //直接替换
s[id*2+1].lazy=s[id].lazy;
s[id*2].sum=s[id].lazy*(s[id*2].r-s[id*2].l+1);
s[id*2+1].sum=s[id].lazy*(s[id*2+1].r-s[id*2+1].l+1);
s[id].lazy=0;
}
void rep(int id,int l,int r,int val)
{
if(l<=s[id].l&&s[id].r<=r) //完全覆盖
{
s[id].lazy=val;
s[id].sum=val*(s[id].r-s[id].l+1); //修改当前节点的sum值
return;
}
if(s[id].lazy!=0) //不完全覆盖,且标记不为0,向下传递
pushdown(id);
int mid=(s[id].l+s[id].r)/2;
if(r<=mid)
rep(id*2,l,r,val);
else if(l>mid)
rep(id*2+1,l,r,val);
else
{
rep(id*2,l,r,val);
rep(id*2+1,l,r,val);
}
s[id].sum=s[id*2].sum+s[id*2+1].sum; //回溯
}
long long sum(int id,int l,int r)
{
if(l<=s[id].l&&s[id].r<=r)
return s[id].sum;
if(s[id].lazy!=0)
pushdown(id);
int mid=(s[id].l+s[id].r)/2;
if(r<=mid)
return sum(id*2,l,r);
else if(l>mid)
return sum(id*2+1,l,r);
else
return sum(id*2,l,r)+sum(id*2+1,l,r);
}
完整代码:
#include<stdio.h>
struct node
{
int l,r;
long long sum,lazy;
}s[100005*4];
void build(int id,int l,int r)
{
s[id].l=l;
s[id].r=r;
s[id].lazy=0;
if(s[id].l==s[id].r)
s[id].sum=1;
else
{
int mid=(l+r)/2;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
s[id].sum=s[id*2].sum+s[id*2+1].sum;
}
}
void pushdown(int id)
{
s[id*2].lazy=s[id].lazy; //直接替换
s[id*2+1].lazy=s[id].lazy;
s[id*2].sum=s[id].lazy*(s[id*2].r-s[id*2].l+1);
s[id*2+1].sum=s[id].lazy*(s[id*2+1].r-s[id*2+1].l+1);
s[id].lazy=0;
}
void rep(int id,int l,int r,int val)
{
if(l<=s[id].l&&s[id].r<=r)
{
s[id].lazy=val;
s[id].sum=val*(s[id].r-s[id].l+1);
return;
}
if(s[id].lazy!=0)
pushdown(id);
int mid=(s[id].l+s[id].r)/2;
if(r<=mid)
rep(id*2,l,r,val);
else if(l>mid)
rep(id*2+1,l,r,val);
else
{
rep(id*2,l,r,val);
rep(id*2+1,l,r,val);
}
s[id].sum=s[id*2].sum+s[id*2+1].sum;
}
long long sum(int id,int l,int r)
{
if(l<=s[id].l&&s[id].r<=r)
return s[id].sum;
if(s[id].lazy!=0)
pushdown(id);
int mid=(s[id].l+s[id].r)/2;
if(r<=mid)
return sum(id*2,l,r);
else if(l>mid)
return sum(id*2+1,l,r);
else
return sum(id*2,l,r)+sum(id*2+1,l,r);
}
int main()
{
int t,n,q,x,y,z,tt=0;
scanf("%d",&t);
while(t--)
{
tt++;
scanf("%d%d",&n,&q);
build(1,1,n);
while(q--)
{
scanf("%d%d%d",&x,&y,&z);
rep(1,x,y,z);
}
printf("Case %d: The total value of the hook is %lld.\n",tt,sum(1,1,n));
}
return 0;
}