Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24314 Accepted Submission(s): 12127
Problem 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
Sample Output
Case 1: The total value of the hook is 24.
题意:有T组测试数据
n
为钩子长度
m为操作的次数。
初始时,每个钩子的价值为1,
操作由三个数字组成x,y,z表示把区间[x,y]的钩子变成的价值变成z(1铜,2银,3金)
SL
1.区间更新: Lazy +=变=,注意要加push_down的条件(lazy!=0)。
2.多数据: build——tree还要清空lazy部分。
#include<iostream>
#include<stdio.h>
#include<string.h>
#define lson (id*2)
#define rson (id*2+1)
using namespace std;
int T;
int n;
struct nod{
int val,lazy;
}tree[800000];
void push_up(int id)
{
tree[id].val=tree[lson].val+tree[rson].val;
}
void build(int id,int l,int r)
{
tree[id].lazy=0; 多样例!!
if(l==r)
{
tree[id].val=1;
tree[id].lazy=0;
return ;
}
int mid=(l+r)>>1;
build(lson,l,mid);
build(rson,mid+1,r);
push_up(id);
}
void push_down(int id,int l,int mid,int r)
{
if(tree[id].lazy==0) return ; 覆盖更新条件!!
tree[lson].lazy=tree[rson].lazy=tree[id].lazy;
tree[lson].val=(mid-l+1)*tree[id].lazy;
tree[rson].val=(r-mid)*tree[id].lazy;
tree[id].lazy=0;
return ;
}
void add(int id,int l,int r,int L,int R,int V)
{
if(r<L||l>R) return ;
if(r<=R&&l>=L)
{
tree[id].lazy=V;
tree[id].val=(r-l+1)*V;
return ;
}
int mid=(l+r)>>1;
push_down(id,l,mid,r);
add(lson,l,mid,L,R,V);
add(rson,mid+1,r,L,R,V);
push_up(id);
return ;
}
int main()
{
scanf("%d",&T);
for(int i=1;i<=T;i++)
{
scanf("%d",&n);
build(1,1,n);
int Q;
scanf("%d",&Q);
for(int j=1;j<=Q;j++)
{
int aa,bb,cc;
scanf("%d%d%d",&aa,&bb,&cc);
add(1,1,n,aa,bb,cc);
}
printf("Case %d: The total value of the hook is %d.\n",i,tree[1].val);
}
}