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. 

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.hdu1

题意:就是区间又该为一样东西,不是加加减减之类的东西,而是,直接将一段区间改为一个数。

题解:就是可以将整一段标记为一个值,或者-1,-1表示该区间内有多个不同的值。
 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
const int MAXN=;
char s[],c;
long long tree[MAXN*]={},add[MAXN*]={};
int t,time,num,x,n,y,m,z,q,a[MAXN]={};
using namespace std;
long long query(int l,int r,int p,int x,int y)
{
int res=;
if (l>r) exit;
if (l==x&&r==y&&tree[p]!=-) res+=tree[p]*(r-l+);
else
{
int mid=(l+r)>>;
if (y<=mid) res+=query(l,mid,p*,x,y);
else if(x>=mid+) res+=query(mid+,r,p*+,x,y);
else res+=query(l,mid,p*,x,mid)+query(mid+,r,p*+,mid+,y);
}
return res;
}
void change(int l,int r,int p,int x,int y,int zhi)
{
if (l>r) exit;
if (l==x&&r==y) tree[p]=zhi;
else
{
if (tree[p]!=-)
{
tree[p*]=tree[p*+]=tree[p];
tree[p]=-;
}
int mid=(l+r)>>;
if (y<=mid) change(l,mid,p*,x,y,zhi);
else if (x>=mid+) change(mid+,r,p*+,x,y,zhi);
else
{
change(l,mid,p*,x,mid,zhi);
change(mid+,r,p*+,mid+,y,zhi);
}
}
}
int main()
{
scanf("%d",&time);
t=time;
while (time--)
{
cout<<"Case "<<t-time<<": The total value of the hook is ";
scanf("%d%d",&n,&q);
memset(tree,-,sizeof(tree));
tree[]=;
for (int i=;i<=q;i++)
{
scanf("%d%d%d",&x,&y,&z);
change(,n,,x,y,z);
}
printf("%d.\n",query(,n,,,n));
}
}
 

HDU1698 线段树(区间更新区间查询)的更多相关文章

  1. hdu1698 线段树区间更新

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  2. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  3. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  4. codevs 1690 开关灯 线段树区间更新 区间查询Lazy

    题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...

  5. A Simple Problem with Integers 线段树 区间更新 区间查询

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 115624   Accepted: 35897 Case Time Lim ...

  6. POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92632   ...

  7. hdu1698线段树区间更新

    题目链接:https://vjudge.net/contest/66989#problem/E 坑爹的线段树照着上一个线段树更新写的,结果发现有一个地方就是不对,找了半天,发现是延迟更新标记加错了!! ...

  8. POJ-3468(线段树+区间更新+区间查询)

    A Simple Problem With Integers POJ-3468 这题是区间更新的模板题,也只是区间更新和区间查询和的简单使用. 代码中需要注意的点我都已经标注出来了,容易搞混的就是up ...

  9. CDOJ 1057 秋实大哥与花 线段树 区间更新+区间查询

    链接: I - 秋实大哥与花 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu Submit ...

随机推荐

  1. vue.js路由参数简单实例讲解------简单易懂

    vue中,我们构建单页面应用时候,一定必不可少用到vue-router vue-router 就是我们的路由,这个由vue官方提供的插件 首先在我们项目中安装vue-router路由依赖 第一种,我们 ...

  2. Python自学日志_2017/9/05

    9月5日今天早晨学习了网易云课程<Python做Web工程师课程>提前预习课程<学会开发静态网页>.轻松的完成了第五节课的两个实战作业--感觉自己这几天的功夫没有白费,总算学会 ...

  3. github+hexo搭建自己的博客网站(六)进阶配置(搜索引擎收录,优化你的url)

    详细的可以查看hexo博客的演示:https://saucxs.github.io/ 绑定了域名: http://www.chengxinsong.cn hexo+github博客网站源码(可以clo ...

  4. The First Article

    由于公司项目比较紧张,开始自己的博客之旅推迟了好几个月.今天终于按捺不住,申请了博客. 心中竟然有一丝丝兴奋,终于可以和众多博友们讨论分享我们一路走来的收获和感悟,记录下我们在工作中遇到和解决的问题, ...

  5. WebServices 之 WSDL

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt234 一,WSDL概述 WebServices Description La ...

  6. ROS学习记录(三)————创建一个简单的发布节点和订阅节点

    暑假在家有些懈怠,不,非常懈怠- -||!良心已经发痛了,想快些补回原来的进度,但忽然发现,中断了一段时间再重新去学习,有的地方连最基本的符号都忘记了 ,这次特意弄个最最基础的,恢复一下,以前的进度. ...

  7. Redis 常用数据结构及其控制命令整合

    Redis 键值支持5种基本结构,分别是字符串,列表,哈希,集合,有序集合.每一种数据结构都有对应的取值和设值命令,辅助命令,除此之外,还有一些全局命令,用来管理Redis存储的所有 键. 全局命令 ...

  8. Tinyhttpd 代码学习

    前阵子,参加了实习生面试,被面试官各种虐,问我说有没有读过一些开源的代码.对于只会用框架的我来说真的是硬伤啊,在知乎大神的推荐下在EZLippi-浮生志找了一些源代码来阅读,于是从小型入手,找了Tin ...

  9. UCOSii项目在NIOSii上的移植

    概览 本次使用Altera公司的NIOS II软核. 使用Quatus工具生成BSP并利用BSP打包工具生成UCOSII嵌入环境. 手动书写LCD驱动与显示函数,对UCOS II加入简单图像显示接口. ...

  10. nginx小问题

    配置nginx与ftp图片服务器:安装后,要在/usr/local/nginx/conf/nginx.conf里面的server中(带有localhost的那一块)修改为location \ {roo ...