洛谷 [P1198] 最大数
首先这是一道线段树裸题,但是线段树长度不确定,那么我们可以在建树的时候,将每一个节点初始化为-INF,每次往队尾加一个元素即一次单节点更新,注意本题的数据范围,其实并不用开 long long,具体请看注释。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
const int MAXN=200005;
int read(){
int rv=0,fh=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') fh=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
rv=(rv<<1)+(rv<<3)+c-'0';
c=getchar();
}
return rv*fh;
}
int MOD,m,t,ma[MAXN<<2],n;
void PushUp(int rt){
ma[rt]=max(ma[rt<<1],ma[rt<<1|1]);
}
void build(int l,int r,int rt){
if(l==r){
ma[rt]=-0x7fffffff;
return;
}
int mid=l+((r-l)>>1);
build(lson);
build(rson);
PushUp(rt);
}
void Update(int add,int loc,int l,int r,int rt){
if(r==loc&&l==loc){
ma[rt]=add;
return;
}
int mid=l+((r-l)>>1); //这样取平均数可以防溢出
if(loc<=mid) Update(add,loc,lson);
else Update(add,loc,rson);
PushUp(rt);
}
int query(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R){
return ma[rt];
}
int mid=l+((r-l)>>1);
int q=-0x7fffffff;
if(L<=mid){
q=max(q,query(L,R,lson));
}
if(mid<R) q=max(q,query(L,R,rson));
return q;
}
int main(){
freopen("in.txt","r",stdin);
m=read();MOD=read();
build(1,m,1);
for(int i=1;i<=m;i++){
char c;
scanf(" %c ",&c);
int k=read();
if(c=='A'){
n++;
k=((long long)k+t)%MOD;//注意防止溢出
Update(k,n,1,m,1);
}else {
t=query(n-k+1,n,1,m,1);
printf("%d\n",t);
}
}
fclose(stdin);
return 0;
}
等等。。本题只要求在队尾加入元素,而且要求的是队尾几个元素的最小值,那么这道题就可以用单调栈+二分来做
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=200005;
int read(){
int rv=0,fh=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') fh=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
rv=(rv<<1)+(rv<<3)+c-'0';
c=getchar();
}
return rv*fh;
}
int m,MOD,t,stack[MAXN],head,num[MAXN],cnt;
int main(){
freopen("in.txt","r",stdin);
m=read();MOD=read();
for(int i=1;i<=m;i++){
char c;
scanf(" %c ",&c);
int k=read();
if(c=='A'){
k=((long long)k+t)%MOD;
cnt++;
while(stack[head]<=k&&head) head--;
head++;
stack[head]=k;
num[head]=cnt;
}else {
int l=1,r=head,mid;
k=cnt-k+1;
while(l<=r){
mid=(l+r)>>1;
if(num[mid]<k){
l=mid+1;
}else {
r=mid-1;
}
}
t=stack[l];
printf("%d\n",t);
}
}
fclose(stdin);
return 0;
}
洛谷 [P1198] 最大数的更多相关文章
- 洛谷 - P1198 - 最大数 - 线段树
https://www.luogu.org/problemnew/show/P1198 要问区间最大值,肯定是要用线段树的,不能用树状数组.(因为没有逆元?但是题目求的是最后一段,可以改成类似前缀和啊 ...
- 洛谷p1198 最大数
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; in ...
- 洛谷 P1198 [JSOI2008]最大数
洛谷 P1198 [JSOI2008]最大数 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. ...
- 洛谷P1198 [JSOI2008]最大数(单点修改,区间查询)
洛谷P1198 [JSOI2008]最大数 简单的线段树单点问题. 问题:读入A和Q时,按照读入一个字符会MLE,换成读入字符串就可以了. #include<bits/stdc++.h> ...
- 「线段树」「单点修改」洛谷P1198 [JSOI2008]最大数
「线段树」「单点修改」洛谷P1198 [JSOI2008]最大数 题面描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数, ...
- 洛谷P1198 [JSOI2008]最大数(BZOJ.1012 )
To 洛谷.1198 最大数 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制:L不超过当 ...
- 【题解】洛谷P1198 [JSOI2008] 最大数(线段树)
洛谷P1198:https://www.luogu.org/problemnew/show/P1198 思路 一道水水的线段树 20分钟A掉 这道题只涉及到单点修改和区间查询 所以这道题甚至不用Laz ...
- 洛谷P1198 [JSOI2008]最大数
P1198 [JSOI2008]最大数 267通过 1.2K提交 题目提供者该用户不存在 标签线段树各省省选 难度提高+/省选- 提交该题 讨论 题解 记录 最新讨论 WA80的戳这QwQ BZOJ都 ...
- [洛谷P1198/BZOJ1012][JSOI2008] 最大数 - 树状数组/线段树?
其实已经学了树状数组和线段树,然而懒得做题,所以至今没写多少博客 Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数 ...
随机推荐
- VS2012 TFS解决离职后代码遗留未迁入问题
第一步: 在命令行中输入 C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE 第二步: 根据用户查找该用户下workspac ...
- koala 编译scss不支持中文(包括中文注释),解决方案如下
进入Koala安装目录,例如:C:\Program Files (x86)\Koala\rubygems\gems\sass-3.4.9\lib\sass 找到engine.rb文件,在该文件中找到最 ...
- Kubernetes volumes简介
容器中的磁盘文件生命周期比较短暂,在一些比较复杂的容器应用中会产生一些问题.一.容器crash后,kubelet会重启该容器,但这些文件会丢失掉.二.pod中的多个容器经常需要共享文件.因此,Kube ...
- input[type=file]样式更改以及图片上传预览
以前知道input[type=file]可以上传文件,但是没用过,今天初次用,总感觉默认样式怪怪的,想修改一下,于是折腾了半天,总算是小有收获. 以上是默认样式,这里我想小小的修改下: HTML代码如 ...
- UVA 673 Parentheses Balance (栈)
题意描述: 给出一段只包含()和[]的字符串,判断是否合法,合法输出YES,不合法输出NO 规则: 1.该串为空,则合法 2.若A合法,B合法,则AB合法 3.若A合法,则(A)和[A]均合法 解题思 ...
- {style}/index_article.htm {style}表示什么意思啊
LS有点安全意识好不好.... 在你的后台系统设置有个"模板默认风格:________ cfg_df_style " 默认是default也就是 {style}=模板路径+模板默 ...
- 使用vue框架运行npm run dev 时报错解决
使用使用vue框架运行npm run dev 时报错 如下: 原因: localhost:8080 有可能其他软件占用了,导致其他问题的出现 我们可以动态修改地址 解决: 进入项目文件的config文 ...
- redis常见命令使用
这篇经验主要介绍了Redis常见用的一些操作命令.这篇例子是在windows上操作的.linux类似.写的一些基础,大神就别看了. 工具/原料 redis windows 方法/步骤 1 可以 ...
- CSS用HTML中的style属性替换
废话不多说上代码: 1.用CSS给文字添加背景色: <html> <head> <style type="text/css"> body {ba ...
- mysql 两个时间段的差,可以是秒,天,星期,月份,年...
SELECT TIMESTAMPDIFF(SECOND, now(), "2012-11-11 00:00:00") 语法为:TIMESTAMPDIFF(unit,datetime ...