(图片走丢了,真不好意思,要不我补一张?) (对就是这张图)
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.
InputThe 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.
OutputFor 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. 题意:有一个长度为n的钩子,钩子由金银铜三种棒子组成,所以问题来了:骚年,这是你的金棒子,还是你的银棒子,咳咳,扯远了,现在有个浪到飞起的男人,他能将一段的不管曾经是怎样的棒子全部改成自己想要的棒子.好的,现在已知金棒子价值为3,银为2,铜为1.
求修改完以后整个钩子的价值. 再多练几道线段树吧...不过这道题没强制在线,似乎可以搞些别的东东?当我没说....反正就打线段树吧...
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define N 100000
#define lson root<<1
#define rson root<<1|1
using namespace std; int node[N<<],lazy[N<<]; void pushup(int root)
{
node[root]=node[lson]+node[rson];
} void pushdown(int root,int val)
{
if(lazy[root]!=-)
{
lazy[lson]=lazy[root];
lazy[rson]=lazy[root];
node[lson]=(val-(val>>))*lazy[root];
node[rson]=(val>>)*lazy[root];
lazy[root]=-;
}
} void build(int l,int r,int root)
{
lazy[root]=-;
node[root]=;
if(l==r)
{
return;
}
int mid=(l+r)>>;
build(l,mid,lson);
build(mid+,r,rson);
pushup(root);
} void update(int left,int right,int val,int l,int r,int root)
{
if(left<=l&&right>=r)
{
lazy[root]=val;
node[root]=val*(r-l+);
return;
}
pushdown(root,r-l+);
int mid=(l+r)>>;
if(left<=mid)
{
update(left,right,val,l,mid,lson);
}
if(mid<right)
{
update(left,right,val,mid+,r,rson);
}
pushup(root);
} int main()
{
int ttt=,t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
build(,n,); for(int i=;i<=m;i++)
{
int ll,rr,ww;
scanf("%d%d%d",&ll,&rr,&ww);
update(ll,rr,ww,,n,);
}
printf("Case %d: The total value of the hook is %d.\n",++ttt,node[]);
}
}
每天刷题,身体棒棒!