您的位置:首页 > 其它

hdu 1698 Just a Hook (线段树)

2013-08-01 16:17 295 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1698

Just a Hook

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 12534 Accepted Submission(s): 6242

[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

1

10

2

1 5 2

5 9 3

[align=left]Sample Output[/align]

Case 1: The total value of the hook is 24.

【题解】: 区间的更新,线段树
【code】:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#define N 100010
using namespace std;

struct Nod
{
int l,r;  //左右
int flag;  //标记是不是同一颜色
int c,v;   //c是颜色,v区间长度
}node[N<<2];

void building(int l,int r,int p)
{
node[p].l = l;
node[p].r = r;
node[p].flag = 1;
node[p].c = 1;
node[p].v = r-l+1;
if(l==r)    return;
int mid = (l+r)>>1;
building(l,mid,p<<1);
building(mid+1,r,p<<1|1);
}

void update(int l,int r,int p,int c)
{
if(node[p].l==l&&node[p].r==r)
{
node[p].c = c;
node[p].flag = 1;
return;
}
if(node[p].c!=c&&node[p].flag)
{
node[p].flag = 0;
node[p<<1|1].flag=node[p<<1].flag = 1;
node[p<<1|1].c=node[p<<1].c = node[p].c;
}
int mid = (node[p].l+node[p].r)>>1;
if(r<=mid)  update(l,r,p<<1,c);
else if(l>mid)  update(l,r,p<<1|1,c);
else
{
update(l,mid,p<<1,c);
update(mid+1,r,p<<1|1,c);
}
//合并:颜色相同
if(node[p<<1].c==node[p<<1|1].c&&node[p<<1].flag&&node[p<<1|1].flag)
{
node[p].flag = 1;
node[p].c = node[p<<1].c;
}
}

int cal_dfs(int p)  //最后的查询
{
if(node[p].flag==1)
{
return node[p].c*node[p].v;
}
return cal_dfs(p<<1)+cal_dfs(p<<1|1);
}

int main()
{
int t,cas;
scanf("%d",&t);
for(cas=1;cas<=t;cas++)
{
int n,q;
scanf("%d%d",&n,&q);
building(1,n,1);
while(q--)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
update(x,y,1,z);
}
printf("Case %d: The total value of the hook is %d.\n",cas,cal_dfs(1));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: