题目链接

题意:

给定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. android 设置半透明

    对于Button和ImageButton 还有一些View 设置半透明或者透明都是通过 android:background="#b0000000" 这是就是半透明 android ...

  2. 【转载】struct和typedef struct彻底明白了

    分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int a; }Stu; 于是在声明变量的时候就可 ...

  3. [转载]C# 多线程、控制线程数提高循环输出效率

    C#多线程及控制线程数量,对for循环输出效率. 虽然输出不规律,但是效率明显提高. 思路: 如果要删除1000条数据,只使用for循环,则一个接着一个输出.所以,把1000条数据分成seed段,每段 ...

  4. uva 10120

    bfs搜索  当n大于等于49 是 总是可能的 ~ http://www.algorithmist.com/index.php/UVa_10120 #include <cstdio> #i ...

  5. jQuery1.9.1源码分析--Animation模块

    var fxNow, // 使用一个ID来执行动画setInterval timerId, rfxtypes = /^(?:toggle|show|hide)$/, // eg: +=30.5px / ...

  6. Implicitly Typed Local Variables

    Implicitly Typed Local Variables It happens time and time again: I’ll be at a game jam, mentoring st ...

  7. unity3d 射线扫描 忽略图层

    原地址:http://blog.csdn.net/w88193363/article/details/38331205 函数说明 static RaycastHit2D[] RaycastAll(Ve ...

  8. Chp17: Moderate

    17.1 swap a number in place.(without temporary variables) a = a ^ b; b = a ^ b; a = a ^ b; 17.3 Writ ...

  9. hdoj 2112 HDU Today

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=2112 分析:多了一个地方的条件,用map来映射地点编号,Dijkstra求解即可 //2013-10- ...

  10. js和jquery获取文档对象以及滚动条位置

    <div style="width:120px;height:120px;border:1px solid red; position:absolute; left:800px; to ...