Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.
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.
1 10 2 1 5 2 5 9 3
Case 1: The total value of the hook is 24.
题意:有 t 组测试数据,n 为钩子长度(1<=n<=100000),m 为操作的次数。初始时,每个钩子的价值为1,操作由三个数字组成x,y,z表示把区间[x,y]的钩子变成的价值变成z(1代表铜,2银,3金)。
使用 lazy 懒惰标记来节省时间
注意:建树部分
{
tree[id].lazy=0; // 因为要多次建树,所以初始化时不能仅仅更新叶节点的lazy 要将lazy全部清零
if (l>=r)
{
tree[id].val=1;
return;
}
int mid=(l+r)/2;
build_tree(lson,l,mid);
build_tree(rson,mid+1,r);
push_up(id);
return ;
}
#include<algorithm>
#include<stdio.h>
#define lson (id*2)
#define rson (id*2+1)
using namespace std;
struct edge{
int val;
int lazy;
}tree[800005];
int n,ans=0,m,t;
void push_up(int id)
{
tree[id].val=tree[lson].val+tree[rson].val;
return ;
}
void push_down(int id,int l,int r)
{
int mid=(l+r)/2;
if (tree[id].lazy==0) return ;
tree[lson].val=tree[id].lazy*(mid-l+1);
tree[rson].val=tree[id].lazy*(r-mid);
tree[lson].lazy=tree[id].lazy;
tree[rson].lazy=tree[id].lazy;
tree[id].lazy=0;
return ;
}
void build_tree(int id,int l,int r)
{
tree[id].lazy=0;
if (l>=r)
{
tree[id].val=1;
return;
}
int mid=(l+r)/2;
build_tree(lson,l,mid);
build_tree(rson,mid+1,r);
return ;
}
void add_tree(int id,int l,int r,int L,int R,int v)
{
if (l>=L&&r<=R)
{
tree[id].val=(r-l+1)*v;
tree[id].lazy=v;
return ;
}
int mid=(l+r)/2;
push_down(id,l,r);
if (mid>=L)
add_tree(lson,l,mid,L,R,v);
if (mid+1<=R)
add_tree(rson,mid+1,r,L,R,v);
push_up(id);
return ;
}
void query_tree(int id,int l,int r,int L,int R)
{
if (l>=L&&r<=R)
{
ans+=tree[id].val;
return ;
}
int mid=(l+r)/2;
push_down(id,l,r);
if (mid>=L) query_tree(lson,l,mid,L,R);
if (mid+1<=R) query_tree(rson,mid+1,r,L,R);
push_up(id);
return ;
}
int main()
{
cin>>t;
int num=0;
while(num<t)
{
num++;
scanf("%d",&n);
build_tree(1,1,n);
scanf("%d",&m);
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add_tree(1,1,n,a,b,c);
}
cout<<"Case "<<num<<": The total value of the hook is "<<tree[1].val<<"."<<endl;
}
}
4736




被折叠的 条评论
为什么被折叠?



