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 ;
}

hdu just a hook(线段树,区间修改)的更多相关文章

  1. HDU - 4578 Transformation(线段树区间修改)

    https://cn.vjudge.net/problem/HDU-4578 题意 4种操作,区间加,区间乘,区间变为一个数,求区间的和.平方和以及立方和. 分析 明显线段树,不过很麻烦..看kuan ...

  2. HDU 5861 Road(线段树 区间修改 单点查询)

    Road Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  3. hdu 3577 Fast Arrangement(线段树区间修改,求区间最小值)

    Problem Description Chinese always have the railway tickets problem because of its' huge amount of p ...

  4. HDU1698Just a Hook(线段树 + 区间修改 + 求和)

    题目链接 分析:1-N区间内初始都是1,然后q个询问,每个询问修改区间[a,b]的值为2或3或者1,统计最后整个区间的和 本来想刷刷手速,结果还是写了一个小时,第一个超时,因为输出的时候去每个区间查找 ...

  5. 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)

    Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...

  6. HDU.1689 Just a Hook (线段树 区间替换 区间总和)

    HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...

  7. Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)

    题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...

  8. poj 2528 线段树区间修改+离散化

    Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...

  9. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

  10. HDU 1698 Just a Hook(线段树 区间替换)

    Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...

随机推荐

  1. hdu 1596 find the safest road

    http://acm.hdu.edu.cn/showproblem.php?pid=1596 #include <cstdio> #include <cstring> #inc ...

  2. Unix/Linux环境C编程入门教程(40) 初识文件操作

     1.函数介绍 close(关闭文件) 相关函数 open,fcntl,shutdown,unlink,fclose 表头文件 #include<unistd.h> 定义函数 int ...

  3. Unix/Linux环境C编程入门教程(39) shell命令之系统管理

    df命令 用于检测文件系统的磁盘空间占用和空余情况,可以显示所有文件系统对节点和磁盘块的使用情况.命令的使用格式如下: df  [选项] 常用参数及含义如下表所示. df -a:显示所有文件系统的磁盘 ...

  4. 多个文件目录下Makefile的写法

    1.前言 目前从事于linux下程序开发,涉及到多个文件,多个目录,这时候编译文件的任务量比较大,需要写Makefile.关于Makefile的详细内容可以参考网上流传非常广泛的<跟我一起写Ma ...

  5. 方案:解决 wordpress 中 gravatar 头像被墙问题

    Gravatar头像具有很好的通用性,但是却遭到了无辜的拦截,对于无法加载头像URL,我们在WordPress系统中通过修改默认的URL链接可以达到恢复头像的功能. 修改文件路径为 /wp-inclu ...

  6. Kafka测试

    准备工作 硬件:笔记本,windows10系统4核8G内存 软件:接口测试工具,以及kafka自带测试工具 影响测试结果配置分析 Borker num.network.thread=3 用于接收并处理 ...

  7. EEPlat vs saleforce 配置 Knowledge Article 演示样例

    ==================================================================================================== ...

  8. apache访问控制设置

    apache访问控制设置 (2009-03-17 11:24:36) 转载▼ 标签: it 杂谈   Order allow,deny    默认情况下禁止所有客户机访问 Order deny,all ...

  9. Android开发 ADB server didn't ACK, failed to start daemon解决方案

    有时候在打开ddms的时候,会看到adb会报如题的错误,解决方案是打开任务管理器,(ctrl+shift+esc),然后关掉adb.exe的进程,重启eclipse就ok了. 还有许多无良商家开发的垃 ...

  10. Android学习笔记--广播(Broadcast)

    1.Android广播分类 android的广播类型分为两类:标准广播和有序广播. 标准广播:异步广播,广播发出后,所有注册了的广播接收器都会同时接收到该广播.打个比方:做地铁过程中的语音播报,当列车 ...