hdu1698 Just a Hook 线段树之经典
2012-07-22 23:40
260 查看
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8167 Accepted Submission(s): 3970
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
1 10 2 1 5 2 5 9 3
Sample Output
Case 1: The total value of the hook is 24.
题意: 有一个棍子很长 可以分为很多节 每节都有不同的材料 价值为1 2 3 组成 初始化状态认为全部节价值为用价值为1的材料组成
现在输入 第一个数表示case 第二个数是节数 第三个是操作数 每个操作有3个数 x y num 即从x到y 的节材料改成价值为num的材料
注意要用lazy思想 否则必超时
#include<stdio.h>
struct haha
{
int left;
int right;
int sum;//当前节点的总价值
int now;//now是现在的金属类型
}node[100000*4];
void build(int left,int right,int nd)
{
int mid;
node[nd].left=left;
node[nd].right=right;
if(left==right)
{
node[nd].sum=1;node[nd].now=1;
return;
}
mid=(left+right)/2;
build(left,mid,nd*2);
build(mid+1,right,nd*2+1);
node[nd].sum=node[nd*2].sum+node[nd*2+1].sum;
node[nd].now=1;
}
void update(int left,int right,int num,int nd)
{
int mid,l_len,r_len;
if(node[nd].now==num) return;
if(left==node[nd].left&&right==node[nd].right)
{
node[nd].sum=num*(right-left+1);
node[nd].now=num;
return;
}
if(node[nd].now)
{
l_len=node[nd*2].right-node[nd*2].left+1;
r_len=node[nd*2+1].right-node[nd*2+1].left+1;
node[nd*2].sum=l_len*node[nd].now;
node[nd*2+1].sum=r_len*node[nd].now;
node[nd*2].now=node[nd].now;
node[nd*2+1].now=node[nd].now;
node[nd].now=0;
}
mid=(node[nd].left+node[nd].right)/2;
if(right<=mid) update(left,right,num,nd*2);
else if(left>mid) update(left,right,num,nd*2+1);
else if(left<=mid&&right>mid){update(left,mid,num,nd*2);update(mid+1,right,num,nd*2+1);}
else return;
node[nd].sum=node[nd*2].sum+node[nd*2+1].sum;
}
int main()
{
int k,cas,n,oper,left,right,num;
scanf("%d",&cas);
for(k=1;k<=cas;k++)
{
scanf("%d",&n);
build(1,n,1);
scanf("%d",&oper);
while(oper--)
{
scanf("%d %d %d",&left,&right,&num);
update(left,right,num,1);
}
printf("Case %d: The total value of the hook is %d.\n",k,node[1].sum);
}
return 0;
}
[align=center][/align]
【Hot】关于PAT/AC
相关文章推荐
- hdu 1698 Just a Hook(线段树区间更新·经典)
- hdu 1698 Just a Hook(线段树,成段更新,懒惰标记)
- HDU 1698 Just a Hook (线段树区间修改)
- HDU 1698 Just a Hook(线段树区间更新)
- 线段树(区间更新与区间查询)——Just a Hook ( HDU 1698 )
- hdu 1698 Just a Hook(线段树区间覆盖)
- HDU 1698 Just a Hook(线段树区间替换)
- 线段树·Just a Hook HDU - 1698 ·区间替换
- 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(线段树)
- HDU 1698 Just a Hook(线段树)
- hdu 1698 Just a Hook[线段树,updata成段更新]
- 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 线段树