题目链接

题意:

给定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. (线段树)的更多相关文章

  1. bzoj 3489 A simple rmq problem - 线段树

    Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一次的数,并且要求找的这个数尽可能大.如果找不到这样的数,则直 ...

  2. ZOJ-3686 A Simple Tree Problem 线段树

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 题意:给定一颗有根树,每个节点有0和1两种值.有两种操作: ...

  3. 【BZOJ4999】This Problem Is Too Simple!(线段树)

    [BZOJ4999]This Problem Is Too Simple!(线段树) 题面 BZOJ 题解 对于每个值,维护一棵线段树就好啦 动态开点,否则空间开不下 剩下的就是很简单的问题啦 当然了 ...

  4. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  5. HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模

    Multiply game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  6. HDU 4974 A simple water problem(贪心)

    HDU 4974 A simple water problem pid=4974" target="_blank" style="">题目链接 ...

  7. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

  8. HDU 5475 An easy problem 线段树

    An easy problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  9. bzoj 3489 A simple rmq problem——主席树套线段树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3489 题解:http://www.itdaan.com/blog/2017/11/24/9b ...

随机推荐

  1. Careercup - Facebook面试题 - 6685828805820416

    2014-05-02 02:33 题目链接 原题: Given the following by grid ,): , , , , , , , , null we need to find ,) an ...

  2. 3044 矩形面积求并 - Wikioi

    题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Description 可能有多组数据,读到n=0为止(不超过15组) 每组数据第一行 ...

  3. SqlServer 系统存储过程

    exec sp_databases; --查看数据库exec sp_tables; --查看表exec sp_columns Categories;--查看列exec sp_helpIndex Cat ...

  4. LAMP安装配置过程

    Mysql ./configure --prefix=/usr/local/mysql (注意/configure前有“.”,是用来检测你的安装平台的目标特征的,prefix是安装路径) #make ...

  5. 【BZOJ】【1532】【POI2005】Kos-Dicing

    网络流/二分法 最大值最小……直接做不太好做的时候就可以用二分+判定来搞. 这题我们就也可以二分最大胜场v,那么怎么来判定呢?首先我们发现:每场比赛要么A赢,要么B赢,这一点跟二分图匹配非常类似,那么 ...

  6. 【BZOJ】【3143】【HNOI2013】游走

    数学期望/高斯消元/贪心 啊……用贪心的思路明显是要把经过次数期望越大的边的权值定的越小,那么接下来的任务就是求每条边的期望经过次数. 拆边为点?nonono,连接x,y两点的边的期望经过次数明显是 ...

  7. PAT-乙级-1048. 数字加密(20)

    1048. 数字加密(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求实现一种数字加密方法.首先固 ...

  8. EBP与ESP寄存器的使用

    push ebp mov esp,ebp esp是堆栈指针 ebp是基址指针 这两条指令的意思是将栈顶指向ebp的地址 ---------------------------------------- ...

  9. Unity3d游戏中添加移动MM支付SDK问题处理

    原地址:http://www.tuicool.com/articles/I73QFb 由于移动mm的SDK将部分资源文件放在jar包中,导致Unity无法识别,提示failed to find res ...

  10. 国内最大的 Node.js 社区将 New Relic 的监控产品换成了 OneAPM

    国内最知名的 CNode 社区把 New Relic 的监控产品换成了 OneAPM .难道 APM 的老大 New Relic 已经被 OneAPM 超越? 毋庸置疑,在全球应用性能管理 SaaS ...