HDU_1556_线段树区间更新
Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15157 Accepted Submission(s): 7544
当N = 0,输入结束。
1 1
2 2
3 3
3
1 1
1 2
1 3
0
3 2 1
基础线段树区间更新。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
#define maxn 100005
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
struct Node
{
int add,sum;
int l,r;
} tree[maxn<<2]; void buildTree(int l,int r,int rt)
{
tree[rt].l=l;
tree[rt].r=r;
tree[rt].add=0;
tree[rt].sum=0;
if(l==r)
{
return;
}
int m=(l+r)/2;
buildTree(lson);
buildTree(rson);
} void pushDown(int rt)
{
if(tree[rt].add)
{
tree[rt<<1].add+=tree[rt].add;
tree[rt<<1|1].add+=tree[rt].add;
tree[rt].add=0; //第一次交代码时wa,没注意到pushdown过后要将根节点的lazy标记归零
}
} void update(int L,int R,int l,int r,int rt)
{
if(L==l&&r==R)
{
tree[rt].add++;
return;
}
pushDown(rt);
int m=(l+r)/2;
if(R<=m)
update(L,R,lson);
else if(L>m)
update(L,R,rson);
else
{
update(L,m,lson);
update(m+1,R,rson);
}
} int n;
void query(int rt)
{
if(tree[rt].l==tree[rt].r)
{
printf("%d",tree[rt].add);
if(tree[rt].l==n)
printf("\n");
else
printf(" ");
return;
}
pushDown(rt);
query(rt<<1);
query(rt<<1|1);
} int main()
{
int a,b;
while(scanf("%d",&n)!=EOF&&n)
{
memset(tree,0,sizeof(tree));
buildTree(1,n,1);
for(int i=0; i<n; i++)
{
scanf("%d%d",&a,&b);
update(a,b,1,n,1);
}
query(1);
}
return 0;
}
HDU_1556_线段树区间更新的更多相关文章
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- hihoCoder 1080 : 更为复杂的买卖房屋姿势 线段树区间更新
#1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...
- HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...
- HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...
- HDU 1698 线段树 区间更新求和
一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...
- POJ-2528 Mayor's posters (线段树区间更新+离散化)
题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...
- ZOJ 1610 Count the Colors (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- HDU5039--Hilarity DFS序+线段树区间更新 14年北京网络赛
题意:n个点的树,每个条边权值为0或者1, q次操作 Q 路径边权抑或和为1的点对数, (u, v)(v, u)算2个. M i修改第i条边的权值 如果是0则变成1, 否则变成0 作法: 我们可以求出 ...
随机推荐
- react jsx 常见问题
问题一: Expected to return a value in arrow function 解决方案: 修改后: // 使用 store return ( <div> <h1 ...
- 混合式框架-AgileLite
Agile Lite是一个HTML5移动前端框架.支持jQuery和Zepto双引擎.而且提供与UI无关的独立框架,内置了Flat UI样式和Ratchet样式.同一时候也支持单页模式和多页模式开发. ...
- POJ 3281 Dining(最大流)
POJ 3281 Dining id=3281" target="_blank" style="">题目链接 题意:n个牛.每一个牛有一些喜欢的 ...
- JSP内建对象
① out - javax.servlet.jsp.jspWriter out对象用于把结果输出到网页上. 方法: 1. void clear() ; 清除输出缓冲区的内容,可是不输出到c ...
- Codeforces Round #100 A. New Year Table
A. New Year Table time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Semaphore and SemaphoreSlim
https://msdn.microsoft.com/en-us/library/z6zx288a(v=vs.110).aspx The System.Threading.Semaphore clas ...
- 并不对劲的spoj1811
题意是求两个字符串的lcs,两个串都只包含小写字母. 本题既可以用后缀自动机,又可以用后缀数组. 对于后缀自动机,就是一道模板题,直接对于一个字符串建后缀自动机再用另一个串查询就行. 对于后缀数组,其 ...
- 深入理解JMM(Java内存模型) --(五)锁
锁的释放-获取建立的happens before 关系 锁是Java并发编程中最重要的同步机制.锁除了让临界区互斥执行外,还可以让释放锁的线程向获取同一个锁的线程发送消息. 下面是锁释放-获取的示例代 ...
- bzoj1509
树的直径 我先开始以为是个图,想想并不知道什么求图的直径的方法,结果是棵树 那么直觉告诉我们是在直径上面,实际上就是直径+min(i->u,i->v),扫一遍就行了 #include< ...
- 将属性和方法添加到Date原型中
<button onclick="myFunction()">点我</button> <script> Date.prototype.myMet ...