题目链接

题意:

给定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. LintCode-Unique Path II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  2. 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

    // test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  3. sysconf和pathconf使用

    问题描述:          查看系统运行时的限制值 问题解决: 执行效果: 源代码:

  4. JS实现Web网页打印功能(IE)

    问题描述:     JS实现Web网页打印功能 问题解决:     这里主要使用WebBrowser控件的ExeWB在IE中打印功能的实现 WebBrowser介绍:         WebBrows ...

  5. 解决Ubuntu开机自动挂载硬盘回收站不可用等权限问题

    1.修改fstab sudo gedit /etc/fstab 2.添加如下代码 #Entry for /dev/sdb7 : UUID=78A675EB46D703C4 /media/anseey/ ...

  6. isEmpty()与equals()、==“”区别

    isEmpty方法源码:public static boolean isEmpty(String str) { return (str == null) || (str.length() == 0); ...

  7. Redis 安装与配置

    启动 Redis WINDOW 服务: https://github.com/ServiceStack/ServiceStack.Redis install-package ServiceStack. ...

  8. CentOS安装RockMongo

    rockmongo官网下载页面在这里: http://rockmongo.com/downloads 找到最新版本的下载链接,一般第一个就是: 右键复制url,比如说是这个: http://rockm ...

  9. 翻译:AngularJS应用的认证技术

    原文: https://medium.com/opinionated-angularjs/7bbf0346acec 认证 最常用的表单认证就是用户名(或者邮件)和密码登录.这就表示要实现一个用户可以输 ...

  10. POJ 1930

    Dead Fraction Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 1762   Accepted: 568 Desc ...