Hdu 1698(线段树 区间修改 区间查询)
(图片走丢了,真不好意思,要不我补一张?)
(对就是这张图)
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.
InputThe 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.
OutputFor 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. 题意:有一个长度为n的钩子,钩子由金银铜三种棒子组成,所以问题来了:骚年,这是你的金棒子,还是你的银棒子,咳咳,扯远了,现在有个浪到飞起的男人,他能将一段的不管曾经是怎样的棒子全部改成自己想要的棒子.好的,现在已知金棒子价值为3,银为2,铜为1.
求修改完以后整个钩子的价值. 再多练几道线段树吧...不过这道题没强制在线,似乎可以搞些别的东东?当我没说....反正就打线段树吧...
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define N 100000
#define lson root<<1
#define rson root<<1|1
using namespace std; int node[N<<],lazy[N<<]; void pushup(int root)
{
node[root]=node[lson]+node[rson];
} void pushdown(int root,int val)
{
if(lazy[root]!=-)
{
lazy[lson]=lazy[root];
lazy[rson]=lazy[root];
node[lson]=(val-(val>>))*lazy[root];
node[rson]=(val>>)*lazy[root];
lazy[root]=-;
}
} void build(int l,int r,int root)
{
lazy[root]=-;
node[root]=;
if(l==r)
{
return;
}
int mid=(l+r)>>;
build(l,mid,lson);
build(mid+,r,rson);
pushup(root);
} void update(int left,int right,int val,int l,int r,int root)
{
if(left<=l&&right>=r)
{
lazy[root]=val;
node[root]=val*(r-l+);
return;
}
pushdown(root,r-l+);
int mid=(l+r)>>;
if(left<=mid)
{
update(left,right,val,l,mid,lson);
}
if(mid<right)
{
update(left,right,val,mid+,r,rson);
}
pushup(root);
} int main()
{
int ttt=,t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
build(,n,); for(int i=;i<=m;i++)
{
int ll,rr,ww;
scanf("%d%d%d",&ll,&rr,&ww);
update(ll,rr,ww,,n,);
}
printf("Case %d: The total value of the hook is %d.\n",++ttt,node[]);
}
}
每天刷题,身体棒棒!
Hdu 1698(线段树 区间修改 区间查询)的更多相关文章
- E - Just a Hook HDU - 1698 线段树区间修改区间和模版题
题意 给出一段初始化全为1的区间 后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...
- HDU - 1698 线段树区间修改,区间查询
这就是很简单的基本的线段树的基本操作,区间修改,区间查询,对区间内部信息打上laze标记,然后维护即可. 我自己做的时候太傻逼了...把区间修改写错了,对给定区间进行修改的时候,mid取的是节点的左右 ...
- hdu 1698 线段树 区间修改
#include <cstdio> #include <cstdlib> #include <cmath> #include <map> #includ ...
- [线段树]区间修改&区间查询问题
区间修改&区间查询问题 [引言]信息学奥赛中常见有区间操作问题,这种类型的题目一般数据规模极大,无法用简单的模拟通过,因此本篇论文将讨论关于可以实现区间修改和区间查询的一部分算法的优越与否. ...
- HDU 1698 线段树 区间更新求和
一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...
- hdu 1698 线段树 区间更新 区间求和
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)
GSS2 - Can you answer these queries II #tree Being a completist and a simplist, kid Yang Zhe cannot ...
- SPOJ BGSHOOT - Shoot and kill (线段树 区间修改 区间查询)
BGSHOOT - Shoot and kill no tags The problem is about Mr.BG who is a great hunter. Today he has gon ...
- HDU(1698),线段树区间更新
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 区间更新重点在于懒惰标记. 当你更新的区间就是整个区间的时候,直接sum[rt] = c*(r- ...
随机推荐
- CyclicBarrier的使用之王者荣耀打大龙
最近一直整并发这块东西,顺便写点Java并发的例子,给大家做个分享,也强化下自己记忆,如果有什么错误或者不当的地方,欢迎大家斧正. LOL和王者荣耀的玩家很多,许多人应该都有打大龙的经历,话说前期大家 ...
- Linux 更改ssh 端口
部署了一个测试服务器之后,在查看linux日志的时候,发现莫名的IP一直在访问服务器,感觉像是某种恶意扫描,来攻击服务器的.因此更改ssh端口. 输入: vim /etc/ssh/sshd_confi ...
- Java为什么把String设计成不可变的(immutable)
在java中,String是字符串常量,可以从内存,同步机制,数据结构等方面分析 1:字符串中常量池的需要 String不同于普通基础变量类型的地方在于对象.java中的字符串对象都保存在字符串常量池 ...
- [Java语言] 《struts2和spring MVC》的区别_动力节点
1.Struts2是类级别的拦截, 一个类对应一个request上下文,SpringMVC是方法级别的拦截,一个方法对应一个request上下文,而方法同时又跟一个url对应,所以说从架构本身上Spr ...
- Java并发/多线程系列——初识篇
回到过去,电脑有一个CPU,一次只能执行一个程序.后来多任务处理意味着计算机可以同时执行多个程序(AKA任务或进程).这不是真的"同时".单个CPU在程序之间共享.操作系统将在运行 ...
- Disharmony Trees 树状数组
Disharmony Trees Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- php版本的选择
简单来说non-thread-safe 非 线程安全 与IIS 搭配环境,thread-safe 线程安全 与apache 搭配的 环境这个大家一定要注意,否则用错了版本,apache是无法启动的,另 ...
- 【小程序】调用wx.request接口时需要注意的问题
写在前面 之前写了一篇<微信小程序实现各种特效实例>,上次的小程序的项目我负责大部分前端后台接口的对接,然后学长帮我改了一些问题.总的来说,收获了不少吧! 现在项目已经完成,还是要陆陆续续 ...
- FirstIDL
pro FIRSTIDL ;控制台输出 print,'first IDL' ;控制台输出 void=dialog_message('Hello,IDL world!',/information) en ...
- RTKLIB编译及RTCM数据读取样例
1.RTKLIB简介 RTKLIB是全球导航卫星系统GNSS(global navigation satellite system)的标准&精密定位开源程序包,RTKLIB由日本东京海洋大学( ...