Just a Hook

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

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.
 
一开始忘记了r--,l--
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int Max=+;
int segTree[Max<<];
int change[Max<<];
int T,q,r,l,z,n;
int PushUp(int node) //回溯,节点更新
{
segTree[node]=(segTree[node<<]+segTree[node<<|]);
return ;
}
int PushDown(int node,int arange)
{
if(change[node]){
change[node<<]=change[node<<|]=change[node];
segTree[node<<]=change[node]*(arange-(arange>>));
segTree[node<<|]=change[node]*(arange>>);
change[node]=;
}
return ;
}
void build(int node,int begin,int end)
{
segTree[node]=;
change[node]=;
if(begin==end)
return;
int m=(begin+end)>>;
build(node<<,begin,m);
build(node<<|,m+,end);
PushUp(node);
return;
}
int updata(int node,int begin,int end)
{
if(l<=begin&&end<=r)
{
segTree[node]=z*(end-begin+);
change[node]=z;
return ;
}
PushDown(node,end-begin+);
int mid=(begin+end)>>;
if(mid>=l) updata(node<<,begin,mid);
if(mid<r) updata(node<<|,mid+,end);
PushUp(node);
}
int main()
{
int i,j;
freopen("in.txt","r",stdin);
cin>>T;
int k=T;
while(T--)
{
scanf("%d%d",&n,&q);
build(,,n-);
for(i=;i<q;i++)
{
scanf("%d%d%d",&l,&r,&z);
l--,r--;
updata(,,n-);
}
printf("Case %d: The total value of the hook is %d.\n",k-T,segTree[]);
}
}

更简单的代码:

 #include <cstdio>
using namespace std;
#define maxN 100005
#define m ((L+R)>>1)
int T, N, Q, X, Y, Z, i;
int val[ * maxN];
void push_down(int o){
val[o * ] = val[o * + ] = val[o];
val[o] = ;
}
void update(int o, int L, int R){
if (X <= L&&R <= Y) val[o] = Z;
else{
if (val[o]) push_down(o);
if (X <= m) update(o * , L, m);
if (Y > m) update(o * + , m + , R);
}
}
int sum(int o,int L,int R){
if (val[o]) return (R-L+)*val[o];
return sum(o * , L, m) + sum(o * + , m + , R);
}
int main(){
scanf("%d", &T);
for (int Case = ; Case <= T; Case++){
val[] = ;
scanf("%d%d", &N, &Q);
for (i = ; i < Q; i++){
scanf("%d%d%d", &X, &Y, &Z);
update(, , N);
}
printf("Case %d: The total value of the hook is %d.\n",Case,sum(,,N));
}
return ;
}

最新文章

  1. (哈夫曼树)HuffmanTree的java实现
  2. 使用VS Code 从零开始开发并调试.NET Core 应用程序
  3. 国内npm镜像源推荐及使用
  4. gitlab 无法查看提交的文件Errno::ENOMEM (Cannot allocate memory - /opt/gitlab/embedded/bin/git):
  5. C#,Java,C -循环冗余检验:CRC-16-CCITT查表法
  6. [Java] cmd命令行如何切换目录
  7. Mysql导入导出 改密命令总结(笔记三)
  8. javascript 将多维数组转换为一维数组
  9. python中read、readline和readlines的区别
  10. 前端要怎么学createjs!!!???
  11. java面对对象 关键字this super
  12. Android studio 中国的垃圾问题解决
  13. 什么是测试开发工程师-google的解释
  14. Flask 系列之 优化项目结构
  15. 基于consul高可用
  16. h5py快速入门指南
  17. 机器学习三剑客之matplotlib 数据绘图展示
  18. TextView设置文字包含中英文时自动换行问题的终极解决方案
  19. maven多模块项目找不到Class错误
  20. 计算机网络&mdash;&mdash;网络层

热门文章

  1. [POJ] 2352 Stars [线段树区间求和]
  2. 【转】sqlmap用户手册
  3. 使用PULL方式解析XML资源文件下面的xml文件
  4. LeetCode_Surrounded Regions
  5. JS获取按下的键盘字符
  6. hihoCoder 1116 计算 (线段树)
  7. fcitx-rime添加五笔/五笔拼音
  8. Chosen 基本使用
  9. ios 计算文字的尺寸
  10. Vijos1051. 送给圣诞夜的极光