hdu just a hook(线段树,区间修改)
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
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.
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.
#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(线段树,区间修改)的更多相关文章
- HDU - 4578 Transformation(线段树区间修改)
https://cn.vjudge.net/problem/HDU-4578 题意 4种操作,区间加,区间乘,区间变为一个数,求区间的和.平方和以及立方和. 分析 明显线段树,不过很麻烦..看kuan ...
- HDU 5861 Road(线段树 区间修改 单点查询)
Road Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- hdu 3577 Fast Arrangement(线段树区间修改,求区间最小值)
Problem Description Chinese always have the railway tickets problem because of its' huge amount of p ...
- HDU1698Just a Hook(线段树 + 区间修改 + 求和)
题目链接 分析:1-N区间内初始都是1,然后q个询问,每个询问修改区间[a,b]的值为2或3或者1,统计最后整个区间的和 本来想刷刷手速,结果还是写了一个小时,第一个超时,因为输出的时候去每个区间查找 ...
- 题解报告: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 ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- 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序加上一个线 ...
- poj 2528 线段树区间修改+离散化
Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...
- E - Just a Hook HDU - 1698 线段树区间修改区间和模版题
题意 给出一段初始化全为1的区间 后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
随机推荐
- MyFirstStruts2
package com.sdlc.action; public class HelloWorldAction { private String msg; public void setMessage( ...
- 阵列卡,组成的磁盘组就像是一个硬盘,pci-e扩展出sata3.0
你想提升性能,那么组RAID0,主板上的RAID应该是软RAID,肯定没有阵列卡来得稳定.如果你有闲钱,可以考虑用阵列卡. 不会的.即使不能起到RAID的作用,起码也可以当作直接连接了2个硬盘.不会影 ...
- [Oracle] 参数修改小结
v$parameter Oracle参数的修改比较复杂,有些参数是可以在session级别修改,有些则必须在system级别修改,有些参数修改后马上生效(不需要重启),有些参数则必须重启才能生效,那么 ...
- 04747_Java语言程序设计(一)_第6章_图形界面设计(二)
例6.1声明一个面板子类,面板子类对象有3个选择框. class Panel1 extends JPanel { JCheckBox box1, box2, box3; Panel1() { box1 ...
- C#使用自定义字体(从文件获取)
在进行软件开发,尤其是开发WinForm程序时,有时为了实现界面的美化,不可避免的需要使用一些特殊的字体,但是在开发完成之后,将程序移到其他的机器上时,由于这些机器可能没有安装相应的字体,所以整个界面 ...
- hdu 2846
字典树的变形,常规字典树用来求前缀的,所以把每个单词拆成len个词建树,为了避免abab这样的查ab时会出现两次,每次加一个标记,如果该节点上次的建树的单词与本次相同就不更新,否则更新 #includ ...
- 计划任务中使用NT AUTHORITY\SYSTEM用户和普通管理员用户有什么差别
原文地址:http://www.ynufe.edu.cn/metc/Article/ShowArticle.asp?ArticleID=805 系统管理员会碰到这种问题,为什么在更改系统登录用户pas ...
- vue-resource插件使用
本文的主要内容如下: 介绍vue-resource的特点 介绍vue-resource的基本使用方法 基于this.$http的增删查改示例 基于this.$resource的增删查改示例 基于int ...
- rhel5.8-LAMP环境搭建
一.LAMP安装前的准备 安装环境:rhel5.8 zabbix-2.4.5 php-5.6.8 MySQL5.6.23 libpng-1.5.9 zlib-1.2.7 (apr,apr ...
- [服务器运维][Minecraft服务器搭建]
参考资料: http://neekey.net/2016/02/01/%E5%A6%82%E4%BD%95%E7%94%A8%E9%98%BF%E9%87%8C%E4%BA%91ecs%E6%90%A ...