HDU 4107 Gangster Segment Tree线段树
这道题也有点新意,就是须要记录最小值段和最大值段,然后成段更新这个段,而不用没点去更新,达到提快速度的目的。
本题过的人非常少,由于大部分都超时了,我严格依照线段树的方法去写。一開始竟然也超时。
然后修补了两个地方就过了,详细改动的地方请參看程序。
知道最大值段和最小值段,然后修补一下就能过了。不是特别难的题目。
#include <stdio.h>
#include <string>
#include <algorithm>
using namespace std; const int SIZE = 200002;
const int TREESIZE = SIZE + (SIZE<<1);
int N, M, P;
int segTree[TREESIZE];
int smallest[TREESIZE];
int largest[TREESIZE]; inline int lChild(int rt) { return rt<<1; }
inline int rChild(int rt) { return rt<<1|1; } void build(int l, int r, int rt)
{
segTree[rt] = 0;
smallest[rt] = 0;
largest[rt] = 0;
if (l == r) return ; int m = l + ((r-l)>>1);
build(l, m, lChild(rt));
build(m+1, r, rChild(rt));
} inline void pushUp(int rt)
{
smallest[rt] = min(smallest[lChild(rt)], smallest[rChild(rt)]);
largest[rt] = max(largest[lChild(rt)], largest[rChild(rt)]);
} inline void pushDown(int rt)
{
if(segTree[rt])
{
int l = lChild(rt), r = rChild(rt);
largest[l] += segTree[rt];
largest[r] += segTree[rt];
smallest[l] += segTree[rt];
smallest[r] += segTree[rt];
segTree[l] += segTree[rt];
segTree[r] += segTree[rt];
segTree[rt] = 0;
}
} void update(int L, int R, int val, int l, int r, int rt)
{
if (L <= l && r <= R)
{
if(largest[rt] < P)
{
smallest[rt] += val;
largest[rt] += val;
segTree[rt] += val;
return ;
}
if(smallest[rt] >= P)
{
smallest[rt] += val<<1;
largest[rt] += val<<1;
segTree[rt] += val<<1;
return;
}
} //if (r < L || R < l) return;//在这里推断也会超时
pushDown(rt); int m = l + ((r-l)>>1);
if (L <= m) update(L, R, val, l, m, lChild(rt));
if (m < R) update(L, R, val, m+1, r, rChild(rt)); pushUp(rt);
} inline void pushDown_2(int rt)
{
if (segTree[rt])
{
segTree[lChild(rt)] += segTree[rt];
segTree[rChild(rt)] += segTree[rt];
}
} void printTree(int l, int r, int rt)
{
if (l == r)
{
if (l != N) printf("%d ", segTree[rt]);
else printf("%d\n", segTree[rt]);
return ; //记得返回。
}
pushDown_2(rt); int m = l + ((r-l)>>1);
printTree(l, m, lChild(rt));
printTree(m+1, r, rChild(rt));
} int main()
{
int a, b, c;
while (scanf("%d %d %d", &N, &M, &P) != EOF)
{
build(1, N, 1);
//memset(largest, 0, sizeof(largest)); 这样写反而超时
//memset(smallest, 0, sizeof(smallest));
//memset(segTree, 0, sizeof(segTree));
while (M--)
{
scanf("%d %d %d", &a, &b, &c);
update(a, b, c, 1, N, 1);
}
printTree(1, N, 1);
}
return 0;
}
HDU 4107 Gangster Segment Tree线段树的更多相关文章
- SPOJ 11840. Sum of Squares with Segment Tree (线段树,区间更新)
http://www.spoj.com/problems/SEGSQRSS/ SPOJ Problem Set (classical) 11840. Sum of Squares with Segme ...
- Implementation:Segment Tree 线段树
早就听人提起过线段树,今天有题搞不出来,讨论上说要用一下线段树,看了下,本质上是空间划分索引,只不过是一维上面的,如果在二维则是四叉树,三维则是八叉树,如果可以动态调整那么跟R-Tree就很相似了,他 ...
- HDU.5692 Snacks ( DFS序 线段树维护最大值 )
HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)
HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- hdu 1754 I Hate It 线段树 点改动
// hdu 1754 I Hate It 线段树 点改动 // // 不多说,裸的点改动 // // 继续练 #include <algorithm> #include <bits ...
随机推荐
- UIScrollView详解
1.UIScrollView常用属性 contentSize属性--该属性表示滚动的内容的范围大小,是CGPoint类型的. 说明: 默认超出UIScrollView的可视区域的内容是不显示的.相当于 ...
- Orchard 精简版
Orchard Express v1.7.2 精简版 保留Orchard.Framework和Orchard.Core全部源码(一字未改),去除非必要模块(仅剩Orchard.jQuery, Orch ...
- NUnit-Console 命令行选项详解
本文为 Dennis Gao 原创或翻译技术文章,发表于博客园博客,未经作者本人允许禁止任何形式的转载. NUnit-Console 命令行选项 NUnit-Console 命令行选项列表 指定运行哪 ...
- windows下重启mysql
其中第二种方法对我这无效,以后再搞清楚! 一.MYSQL服务 我的电脑——(右键)管理——服务与应用程序——服务——MYSQL——开启(停止.重启动) 二.命令行方式 Windows 1.点击“开始” ...
- SQL Server 2014 安装小记
一.写在前面 由于想体验下微软的Windows Azure在SQL Server数据库方面的使用,笔者花了点时间安装了一下SQL Server 2014,安装很简单,基本就是稍微做些配置即可,笔者在此 ...
- macd综合版
参数设置 SHORE 12 LONG 26 MID 9 DIF:EMA(CLOSE,SHORT)-EMA(CLOSE,LONG); DEA:EMA(DIF,MID),COLOR88888 ...
- C#与数据库访问技术总结(十三)之DataReader对象
DataReader对象与数据获取 DataReader对象以“基于连接”的方式来访问数据库. 也就是说,在访问数据库.执行SQL操作时,DataReader要求一直连在数据库上. 这将会给数据库的连 ...
- 自制操作系统(二) 让bootsector开机启动打印一首诗
qq:992591601 欢迎交流 2016-03-31作 2016-06-01.2016-06-27改 我总结了些基本原理: 1.软盘的第一个扇区为启动区 2.计算机读软盘是以512字节为单位来读写 ...
- JavaScript中for..in循环陷阱介绍
for...in循环中的循环计数器是字符串,而不是数字它包含当前属性的名称或当前数组元素的索引,下面有个不错的示例大家可以参考下 大家都知道在JavaScript中提供了两种方式迭代对象: (1) ...
- Origin的图片导出问题
很多会议投稿都会要求提交的pdf文件用的是type1字体,因为type1字体是矢量字体,无论怎么放大缩小都不会失真.一旦pdf里嵌入了其他非矢量字体,例如type3字体,就会通不过测试,一个典型的例子 ...