hdu 4973 A simple simulation problem. (线段树)
题意:
给定n长的序列 m个操作
序列默认为 1, 2, 3···n
操作1:D [l,r] 把[l,r]区间增长 :( 1,2,3,4 进行 D [1,3]变成 1,1,2,2,3,3,4 )
操作2:Q [l,r] 问区间[l,r] 上出现最多次数的数 的次数
分析:
会线段树,但是做题的时候没想到如何把这个处理,这是问题的关键。
当时比赛的时候卡了一题,然后快结束的时候看的这个题,所以很乱,没有想出来。
赛后, 我自己有写了一遍,但是我很sb的吧中间的一个变量写错了,结果错了好几天。
今天看别人的题解的时候,他们有加了一个lz【】延迟标记的,就是更新的时候更新到一整个区间的时候,就停止。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define LL __int64
#define lson l, mid, 2*rt
#define rson mid+1, r, 2*rt+1
const int maxn = +;
using namespace std;
LL cnt[*maxn], mx[*maxn]; void pushup(LL rt)
{
cnt[rt] = cnt[*rt]+cnt[*rt+];
mx[rt] = max(mx[*rt], mx[*rt+]);
}
void build(LL l, LL r, LL rt)
{
if(l==r)
{
cnt[rt] = ;
mx[rt] = ;
return;
}
int mid = (l+r)/;
build(lson);
build(rson);
pushup(rt);
}
void update(LL ll, LL rr, LL l, LL r, LL rt) //每次更新到底
{
if(l==r)
{
cnt[rt] += (rr-ll+);
mx[rt] = cnt[rt];
return;
}
int mid = (l+r)/;
LL tmp = cnt[*rt]; //就是这个变量写错了,错了好几天
//还有这里一定要提前记录下cnt[2*rt],因为递归以后cnt[2*rt]的值会改变。
if(tmp>=rr) update(ll, rr, lson);
else if(tmp<ll) update(ll-tmp, rr-tmp, rson);
else
{
update(ll, tmp, lson);
update(, rr-tmp, rson);
}
pushup(rt);
}
LL query(LL ll, LL rr, LL l, LL r, LL rt)
{
if(cnt[rt]==(rr-ll+)) //查找的时候如果发现个数和那个区间的个数相同就是 区间都覆盖了
return mx[rt];
if(l==r)
return (rr-ll+); int mid = (l+r)/;
LL tmp = cnt[*rt]; if(tmp>=rr) return query(ll, rr, lson);
else if(tmp<ll) return query(ll-tmp, rr-tmp, rson);
else return max(query(ll, tmp, lson), query(, rr-tmp, rson));
}
int main()
{
int t, ca = ;
LL n, m;
LL l, r;
char ch;
scanf("%d", &t);
while(t--)
{
memset(mx, , sizeof(mx));
memset(cnt, , sizeof(cnt));
scanf("%I64d%I64d", &n, &m);
build(, n, );
printf("Case #%d:\n", ca++);
while(m--)
{
getchar();
scanf("%c %I64d %I64d", &ch, &l, &r);
if(ch=='D')
update(l, r, , n, );
else
printf("%I64d\n", query(l, r, , n, ));
}
}
return ;
}
hdu 4973 A simple simulation problem. (线段树)的更多相关文章
- bzoj 3489 A simple rmq problem - 线段树
Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一次的数,并且要求找的这个数尽可能大.如果找不到这样的数,则直 ...
- ZOJ-3686 A Simple Tree Problem 线段树
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 题意:给定一颗有根树,每个节点有0和1两种值.有两种操作: ...
- 【BZOJ4999】This Problem Is Too Simple!(线段树)
[BZOJ4999]This Problem Is Too Simple!(线段树) 题面 BZOJ 题解 对于每个值,维护一棵线段树就好啦 动态开点,否则空间开不下 剩下的就是很简单的问题啦 当然了 ...
- hdu 5274 Dylans loves tree(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模
Multiply game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 4974 A simple water problem(贪心)
HDU 4974 A simple water problem pid=4974" target="_blank" style="">题目链接 ...
- HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)
HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意: 给一个序列由 ...
- HDU 5475 An easy problem 线段树
An easy problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- bzoj 3489 A simple rmq problem——主席树套线段树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3489 题解:http://www.itdaan.com/blog/2017/11/24/9b ...
随机推荐
- Ubuntu 14.04 安装 Xilinx ISE 14.7 全过程
生命在于折腾. 这个帖子作为我安装xilinx ISE 14.7版本一个记录.希望给需要的人一些帮助,这些内容绝大部分也是来源于互联网. 软硬件: lsb_release -a No LSB modu ...
- JPA学习---第一节:JPA详解
一.详解 JPA JPA(Java Persistence API)是Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关系映射工具来管理Java应用中的关系数据.他的出现主要是 ...
- LintCode-Word Segmentation
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 玩转SmartQQ之登录
SmartQQ是腾讯新出的一个WebQQ,登录地址是:http://w.qq.com/,目前之前的WebQQ可以继续使用,登录地址:http://web2.qq.com/webqq.html,Smar ...
- 搭建Asp.Net MVC4
启动vs2012,开始创建一个新的web应用程序.使用菜单:“文件”>“新建项目” 请在左侧选择 Visual C#,然后选择ASP.NET MVC 4 Web 应用程序.命名您的工程为&quo ...
- Exception in thread "http-bio-8081-exec-3" java.lang.OutOfMemoryError: PermGen space
前言: 在http://www.cnblogs.com/wql025/p/4865673.html一文中我曾描述这种异常也提供了解决方式,但效果不太理想,现在用本文的方式,效果显著. 目前此项目只能登 ...
- nodeJS实战
github代码托管地址: https://github.com/Iwillknow/microblog.git 根据<NodeJS开发指南>实例进行实战{{%并且希望一步步自己能够逐步将 ...
- TensorFlow 基本使用
使用 TensorFlow, 你必须明白 TensorFlow: 使用图 (graph) 来表示计算任务. 在被称之为 会话 (Session) 的上下文 (context) 中执行图. 使用 ten ...
- [转载]Spring Annotation Based Configuration
Annotation injection is performed before XML injection, thus the latter configuration will override ...
- java Hotspot 内存管理白皮书(中文翻译)
转自: http://my.oschina.net/u/568779/blog/166891 1引言 一个健壮的 Java™2平台,Standard Edition (J2SE™)拥有一个自动内存管理 ...