Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 36610 Accepted Submission(s): 17861
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.
![](https://i-blog.csdnimg.cn/blog_migrate/779a6c86f4db19106cba2c46a7dafe46.jpeg)
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.
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.
Source
Recommend
wangye
Statistic | Submit | Discuss | Note
分析:算是线段树的基础题吧,把区间更新的板子给改了一下就过了
AC CODE:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <map>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
const int MAXN= 100000+50;
//线段树区间更新求最值
struct Node
{
int l,r; //区间的左右端点
int Max;
int Min;
int Sum;
int Lazy;
}segTree[MAXN<<2];
//向上更新
void PushUp(int i)
{
segTree[i].Max=max(segTree[i<<1].Max,segTree[(i<<1)|1].Max);
segTree[i].Min=min(segTree[i<<1].Min,segTree[(i<<1)|1].Min);
segTree[i].Sum=segTree[i<<1].Sum+segTree[(i<<1)|1].Sum;
}
//标记下移
void PushDown(int i,int x){
if(segTree[i].Lazy)
{
segTree[i<<1].Lazy = segTree[i].Lazy;
segTree[(i<<1)|1].Lazy = segTree[i].Lazy;
segTree[i<<1].Sum = segTree[i].Lazy*(x-(x>>1));
segTree[(i<<1)|1].Sum = segTree[i].Lazy*(x>>1);
segTree[i].Lazy=0;
}
}
//一般调用Build(1,1,n);
void Build(int i,int l,int r)
{
segTree[i].l=l;
segTree[i].r=r;
segTree[i].Max=0;
segTree[i].Min=INF;
segTree[i].Sum=0;
segTree[i].Lazy=0;
if(l==r)
{
int num=1;
//scanf("%lld",&num);
segTree[i].Max=num;
segTree[i].Min=num;
segTree[i].Sum=num;
return;
}
int mid=(l+r)>>1;
Build(i<<1,l,mid);
Build((i<<1)|1,mid+1,r);
PushUp(i);
}
//根节点i的区间(l,r),改值c
void Add(int i,int l,int r,int c)
{
if(segTree[i].Lazy==c) return;
if(l<=segTree[i].l && segTree[i].r<=r)
{
segTree[i].Lazy = c;
segTree[i].Max = c;
segTree[i].Min = c;
segTree[i].Sum = c*(segTree[i].r-segTree[i].l+1);
return;
}
PushDown(i,segTree[i].r-segTree[i].l+1);
int mid=(segTree[i].l+segTree[i].r)>>1;
if(l<=mid) Add(i<<1,l,r,c); //区间在左边
if(r>mid) Add((i<<1)|1,l,r,c); //区间在右边
PushUp(i);
}
//查询l-r的总和,查询同时更新
LL QuerySum(int i,int l,int r){
if(l<=segTree[i].l && segTree[i].r<=r){
return segTree[i].Sum;
}
PushDown(i,segTree[i].r-segTree[i].l+1);
int mid=(segTree[i].l+segTree[i].r)>>1;
//判断区间并返回
LL res=0;
if(l<=mid) res+=QuerySum(i<<1,l,r);
if(r>mid) res+=QuerySum((i<<1)|1,l,r);
return res;
}
LL QueryMin(int i,int l,int r){
if(l<=segTree[i].l && segTree[i].r<=r){
return segTree[i].Min;
}
PushDown(i,segTree[i].r-segTree[i].l+1);
int mid=(segTree[i].l+segTree[i].r)>>1;
LL res=INF;
if(l<=mid) res=min(QueryMin(i<<1,l,r),res);
if(r>mid) res=min(QueryMin((i<<1)|1,l,r),res);
return res;
}
LL QueryMax(int i,int l,int r){
if(l<=segTree[i].l && segTree[i].r<=r){
return segTree[i].Max;
}
PushDown(i,segTree[i].r-segTree[i].l+1);
int mid=(segTree[i].l+segTree[i].r)>>1;
LL res=0;
if(l<=mid) res=max(res,QueryMax(i<<1,l,r));
if(r>mid) res=max(res,QueryMax((i<<1)|1,l,r));
return res;
}
int main(){
int t;
scanf("%d",&t);
for(int i=1;i<=t;i++){
int n,q;
scanf("%d%d",&n,&q);
Build(1,1,n);
while(q--){
int l,r,c;
scanf("%d%d%d",&l,&r,&c);
Add(1,l,r,c);
}
printf("Case %d: The total value of the hook is %d.\n",i,segTree[1].Sum);
}
return 0;
}