HDU 1698 Just a Hook
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 46698 Accepted Submission(s): 22167
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.
区间替换+区间求和
#include <cstdio>
using namespace std;
const int maxn=100002;
struct Node{
int value;
int set;
int l,r;
}t[maxn*4+2];
void build (int p,int l,int r)//递归构建 线段树
{
t[p].set=0;
t[p].l=l,t[p].r=r;
int mid=(l+r)/2;
if (l==r)
{
t[p].value=1;
return ;
}
build (p*2,l,mid);
build (p*2+1,mid+1,r);
t[p].value=t[p*2].value+t[p*2+1].value;
}
void pushdown (int p) // 懒惰标记的下传
{
int l=t[p].l,r=t[p].r;
int mid=(l+r)/2;
if (t[p].set)
{
t[p*2].set=t[p*2+1].set=t[p].set;//标记下传
t[p*2].value=(mid-l+1)*t[p].set;
t[p*2+1].value=(r-mid)*t[p].set;
t[p].set=0;//标记清零
}
}
void change (int p,int x,int y,int z)//把[x~y]之间的值改为z,并求和
{
int l=t[p].l,r=t[p].r,mid;
mid=(l+r)/2;
if (x<=l && y>=r)//当前区间属于待更改区间,这个区间集体改值,更新值
{
t[p].set=z;
t[p].value=(r-l+1)*z;
return ;
}
pushdown(p);//当当前区间不属于待更改区间,将set值下传。
if (x<=mid)//将属于左半区间的待更改子区间修改
change (p*2,x,y,z);
if (y>mid)//将属于右半区间的待更改子区间修改
change (p*2+1,x,y,z);
t[p].value=t[p*2].value+t[p*2+1].value;// 子区间更新完毕,从新更新当前区间和 。
}
int main()
{
int T,n,q,x,y,z,Case=0;
scanf ("%d",&T);
while (T--)
{
scanf ("%d%d",&n,&q);
build (1,1,n);
for (int i=0;i<q;i++)
{
scanf ("%d%d%d",&x,&y,&z);
change (1,x,y,z);
}
printf ("Case %d: The total value of the hook is %d.\n",++Case,t[1].value);
}
return 0;
}
上一篇: HDU 1698 Just a Hook
下一篇: HDU1698Just a Hook