P1198 最大数 线段树水题
这道题模拟一下可以过,但是我们发现线段树也可以安全水过......
写的线段树只需要滋磁单点修改,区间求max即可
我一开始犯了一个很SB的错误:每次插入修改了t,然后疯狂爆0到怀疑人生...
而且我写的线段树还不明不白的碾了胡雨菲几年前写的。
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = ;
typedef long long LL;
LL mo;
LL Max[N<<]; void build(int l,int r,int o)
{
Max[o]=-;
if(l==r)
{
return;
}
int mid=(l+r)>>;
build(l,mid,o<<);
build(mid+,r,o<<|);
return;
} void add(int x,int v,int l,int r,int o)
{
//printf("add:x=%d v=%d l=%d r=%d o=%d\n",x,v,l,r,o);
if(l==x&&r==x)
{
//printf("Max[%d]=%d\n",o,v);
Max[o]=v;
return;
}
int mid=(l+r)>>;
if(x<=mid)add(x,v,l,mid,o<<);
else add(x,v,mid+,r,o<<|);
Max[o]=max(Max[o<<],Max[o<<|]);
return;
} LL ask(int L,int R,int l,int r,int o)
{
if(L<=l&&r<=R) return Max[o];
if(R<l||r<L) return -;
int mid=(l+r)>>;
return max(ask(L,R,l,mid,o<<),ask(L,R,mid+,r,o<<|));
} int main()
{
LL m,top=,x,t=;char flag;
scanf("%lld%lld",&m,&mo);
build(,m,);
for(int i=;i<=m;i++)
{
cin>>flag>>x;
if(flag=='Q')
{
if(!x) {printf("0\n");continue;}
t=ask(top-x,top-,,m,);
printf("%lld\n",t);
}
else
{ add(top,(t+x)%mo,,m,);
top++;
}
}
return ;
}
AC代码:
P1198 最大数 线段树水题的更多相关文章
- POJ 3468 A Simple Problem with Integers(线段树水题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 135904 ...
- hdu 1754 I Hate It(线段树水题)
>>点击进入原题测试<< 思路:线段树水题,可以手敲 #include<string> #include<iostream> #include<a ...
- 【wikioi】1191 数轴染色(线段树+水题)
http://wikioi.com/problem/1191/ 太水的线段树了,敲了10分钟就敲完了,但是听说还有一种并查集的做法?不明觉厉. #include <cstdio> #inc ...
- codeforces 339C Xenia and Bit Operations(线段树水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Xenia and Bit Operations Xenia the beginn ...
- 洛谷 - P1198 - 最大数 - 线段树
https://www.luogu.org/problemnew/show/P1198 要问区间最大值,肯定是要用线段树的,不能用树状数组.(因为没有逆元?但是题目求的是最后一段,可以改成类似前缀和啊 ...
- hdu - 1394 Minimum Inversion Number(线段树水题)
http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum ...
- P1198最大数——线段树点修改&&模板题
题目 题目链接 大意:维护一个数列,有两种操作: 查询操作Q L:查询当前数列中末尾L个数中的最大的数 插入操作A n:将n加上t再对D取模,将所得值插入数列末尾 解决方案 由题意知,只有两种操作 ...
- codevs 1690 开关灯 线段树水题
没什么好说的,标记put表示开关是否开着. #include<cstdio> #include<cstring> #include<algorithm> using ...
- [ACM_数据结构] Color the ball [线段树水题][数组开大]
Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次 ...
随机推荐
- Flutter - 退出App
Flutter退出App的方法一般有两种 ①SystemNavigator.pop 推荐 onTap: () async { await pop(); }, static Future<void ...
- JDK8 stream toMap() java.lang.IllegalStateException: Duplicate key异常解决(key重复)
测试又报bug啦 接到测试小伙伴的问题,说是一个接口不返回数据了,好吧,虽然不是我写的接口任务落到头上也得解决,本地调试了一下,好家伙,直接抛了个异常出来,这又是哪位大哥喝醉了写的代码... Exce ...
- LeetCode之Add Two Numbers
Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...
- js怎么将 base64转换成图片
//获取数组最后一个元素 let hasFiles = files[Object.keys(files).pop()] // 参考上面的图片 let file = hasFiles.url let n ...
- beta阶段测试基本概况报告
文件地址 测试基本信息 Bitmap 测试 ...
- PHP自动加载上——spl_autoload_register
spl_autoload_register函数是实现自动加载未定义类功能的的重要方法,所谓的自动加载意思就是 我们的new 一个类的时候必须先include或者require的类文件,如果没有incl ...
- ECSHOP广告调用广告位添加到首页顶部通栏教程
ECSHOP广告调用广告位添加到首页顶部通栏教程 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2012-05-26 ECSHOP系统默认预留的广告位很少,如何才能 ...
- SQLSERVER 创建对Oracle数据库的DBlink以及查询使用
1. 与针对oracle数据库一样, 在sqlserver中创建对oracle数据库的dblink 安全性上面也进行定义(貌似不需要跟访问字符串只需要填一个即可) 发现有的版本改注册表不管用 还得修改 ...
- ThreadPoolExecutor参数
1.ThreadPoolExecutor个参数的意义(类上的注释内容) * @param corePoolSize the number of threads to keep in the* pool ...
- shell 学习笔记二
一.break命令 break命令允许跳出所有循环(终止执行后面的所有循环). 下面的例子中,脚本进入死循环直至用户输入数字大于5.要跳出这个循环,返回到shell提示符下,就要使用break命令. ...