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依次 ...
随机推荐
- CSS 尺寸 (Dimension) 实例
CSS 尺寸 (Dimension) 实例CSS 尺寸属性CSS 尺寸属性允许你控制元素的高度和宽度.同样,还允许你增加行间距. 属性 描述height 设置元素的高度.line-height 设置行 ...
- [T-ARA][ORGR]
歌词来源:http://music.163.com/#/song?id=29343993 作曲 : 4번타자/에스킴 [作曲 : 4p/beon-Ta-c/ja-/e-seu-Kim] 作词 : 4번 ...
- 树的最长链-POJ 1985 树的直径(最长链)+牛客小白月赛6-桃花
求树直径的方法在此转载一下大佬们的分析: 可以随便选择一个点开始进行bfs或者dfs,从而找到离该点最远的那个点(可以证明,离树上任意一点最远的点一定是树的某条直径的两端点之一:树的直径:树上的最长简 ...
- Individual Project - Word frequency program——12061154Joy
Description&Requirement: http://www.cnblogs.com/jiel/p/3978727.html 项目时间估计 理解项目要求: 1h 构建项目逻辑: 1h ...
- 20145221 《Java程序设计》实验报告四:Android开发基础
20145221 <Java程序设计>实验报告四:Android开发基础 实验要求 基于Android Studio开发简单的Android应用并部署测试; 了解Android组件.布局管 ...
- CMake系列之三:多个源文件-同一目录
同一目录,多个源文件 把前面的main.c里面的power函数单独写入到一个MathFunctions.c源文件里,目录下的文件结构如下: ./Demo2 | +--- main.c | +--- M ...
- 软件工程(GZSD2015) 第二次作业进度
贵州师范大学软件工程第二次作业 徐 镇 王铭霞 张 英 涂江枫 张 燕 安 坤 周 娟 杨明颢 杨家堂 罗文豪 娄秀盛 周 娟 李盼 岳庆 张颖 李丽思 邓婷 唐洁 郑倩 尚清丽 陈小丽 毛茸 宋光能 ...
- mongoDB的配置和使用
如何启动mongodb? mongod --dbpath C:\appStore\mongodata //数据库地址 再开一个cmder窗口 进入C:\Program Files\MongoDB\Se ...
- js返回值 数组去重
function myfun(arr){ var arr1 = new Array(); var len = arr.length; ;i <len ;i++){ ) { arr1.push(a ...
- Centos7搭建LAMP+Typecho博客
一.安装Apache的httpd服务 yum install httpd # 安装httpd服务 systemctl start httpd # 启动httpd服务 systemctl status ...