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. Linux怎样修改系统时间

    修改linux的时间可以使用date指令 修改日期: 时间设定成2009年5月10日的命令如下: #date -s 05/10/2009 修改时间: 将系统时间设定成上午10点18分0秒的命令如下. ...

  2. POJ 2986 A Triangle and a Circle

    题意:给定一个三角形,以及一个圆的圆心坐标和半径,求圆和三角形的相交面积. 思路: 用三角剖分,三角形上每个线段都变成这个线段与圆心的三角形,然后算出每个三角形与圆的相交面积,然后根据有向面积的正负累 ...

  3. LeetCode_Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. /* ...

  4. Android之SharedPreferences两个工具类

    相信Android的这个最简单的存储方式大家都很熟悉了,但是有一个小小技巧,也许你没有用过,今天就跟大家分享一下,我们可以把SharedPreferences封装在一个工具类中,当我们需要写数据和读数 ...

  5. Non-negative Partial Sums(单调队列)

    Non-negative Partial Sums Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  6. UIKit和Core Graphics绘图(三)——绘制虚线,椭圆以及饼图

    绘制虚线 虚线绘制主要调用CGContextSetLineDash函数. 这个函数有4个参数,除了一个是上下文外,phase为初始跳过几个点开始绘制,第三个参数为一个CGFloat数组,指定你绘制的样 ...

  7. Guava源码分析——ServiceManager

    ServiceManager类:      用于监控服务集的管理器,该类提供了诸如startAsync.stopAsync.servicesByState方法来运行.结束和检查服务集,而且,通过监听器 ...

  8. HDU4496_D-City(并查集删边/逆向)

    D-City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Sub ...

  9. Git 上传本地命令

    1.首先建立一个文件夹用以测试 2.在test中写入一个main.c的文件 其内容如下: 3.然后就建立一个git仓库了 4.然后就是把内容加进去了,上传上去 5.然后我们看下git log的信息 6 ...

  10. zabbix server is not running: the information displayed may not be current

    一.1.关闭selinux及防火墙 2.在/etc/hosts文件里加入ip及对应的主机名. 3.修改配置文件:zabbix.conf.php /opt/data/apache2/htdocs/zab ...