nbut线段树专题E - Just a Hook
来源:互联网 发布:完美告白知乎 编辑:程序博客网 时间:2024/06/05 17:45
Description
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
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
。。。简单的线段树区间更新
#include<stdio.h>
#include<string.h>
#define maxn 100010
struct node
{
int l,r,add,sum;
}tree[maxn<<2];
void build(int p,int l,int r)
{
tree[p].l=l;
tree[p].r=r;
tree[p].add=0;
tree[p].sum=1;
if(l==r)
return ;
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
}
void pushdown(int p)
{
int m=(tree[p].r-tree[p].l+1);
if(tree[p].add)
{
tree[p<<1].add=tree[p].add;
tree[p<<1|1].add=tree[p].add;
tree[p<<1].sum=(m-(m>>1))*tree[p].add;
tree[p<<1|1].sum=(m>>1)*tree[p].add;
tree[p].add=0;
}
}
void update(int p,int l,int r,int val)
{
if(l<=tree[p].l && r>=tree[p].r)
{
tree[p].add=val;
tree[p].sum=(tree[p].r-tree[p].l+1)*val;
return ;
}
pushdown(p);
int mid=(tree[p].l+tree[p].r)>>1;
if(r<=mid)
update(p<<1,l,r,val);
else if(l>mid)
update(p<<1|1,l,r,val);
else
{
update(p<<1,l,mid,val);
update(p<<1|1,mid+1,r,val);
}
tree[p].sum=tree[p<<1].sum+tree[p<<1|1].sum;
}
int main()
{
int n,t,m;
while(~scanf("%d",&t))
{
int p=1;
while(t--)
{
int x,y,z;
scanf("%d",&n);
build(1,1,n);
scanf("%d",&m);
while(m--)
{
scanf("%d%d%d",&x,&y,&z);
update(1,x,y,z);
}
printf("Case %d: The total value of the hook is %d.\n",p,tree[1].sum);
p++;
}
}
return 0;
}
- nbut线段树专题E - Just a Hook
- kuangbin——线段树专题 E - Just a Hook
- 线段树专题—HDU1698 Just a Hook
- Just a Hook(线段树)
- 线段树 Just a Hook
- 线段树 Just a hook
- HDU 1698 Just a Hook 线段树
- [hdu]1698 Just a Hook -- 线段树
- hdu 1698 Just a Hook 线段树
- HDU 1698 JUST A HOOK(线段树)
- HDU 1698 Just a Hook 线段树
- 【线段树】 hdu1698 Just a Hook
- hdu Just a Hook (线段树)
- hdoj 1698 Just a Hook 【线段树】
- Just a Hook+线段树更新
- HDU 1698 Just a Hook(线段树)
- HDU 1698 just a hook 线段树
- HDU 1698 Just a Hook(线段树)
- NBUT-2014暑期集训专题练习1 -> 二分法 J - J
- NBUT-2014暑期集训专题练习1 -> 二分法 H - H
- NBUT-2014暑期集训专题练习1 -> 二分法B - B
- NBUT-2014暑期集训专题练习1 -> 二分法L - L
- NBUT-2014暑期集训专题练习1 -> 二分法F - F
- nbut线段树专题E - Just a Hook
- nbut线段树专题F - A Simple Problem with Integers
- nbut线段树专题I - Balanced Lineup
- <vim>VIM各种tips
- nbut线段树专题C - Mayor\'s posters
- nbut线段树专题W - Frequent values
- nbut线段树专题M - Buy Tickets
- nbut线段树专题 J - Atlantis
- 我对离散化的一些感悟