[NOIP模拟赛] seq
seq

试题分析
介绍一种方法叫做回滚莫队。
- 回滚莫队是一种只加不删的莫队。
首先处理\(l,r\)都在同一个块内的询问,暴力即可。
然后对于\(l,r\)不同在一个块,我们将左端点挂在其所在的块。
将挂在每个块上的右端点排序,这样对于左边我们每次暴力滚回当前块的右端点,这里可以用主席树之类的数据结构维护。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
#define LL long long
inline int read(){
int x=0,f=1; char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int INF = 2147483600;
const int MAXN = 100000;
int N,M; int a[MAXN+1];
int bel[MAXN+1];
struct data{int l,r,id;}q[MAXN+1];
bool cmp(data a,data b){
if(bel[a.l]!=bel[b.l]) return bel[a.l]<bel[b.l];
return a.r<b.r;
}
int L[MAXN+1],R[MAXN+1]; int res;
inline void init_over(){
res=0; memset(L,0,sizeof(L));
memset(R,0,sizeof(R)); return ;
}
struct stak{int pos,x;}staR[MAXN+1],staL[MAXN+1];
int ans[MAXN+1];
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
N=read(),M=read(); int sqr=(int)sqrt(N)+100,top;
for(int i=1;i<=N;i++) a[i]=read();
for(int i=1;i<=M;i++) q[i].l=read(),q[i].r=read(),q[i].id=i;
for(int i=1;i<=N;i++) bel[i]=(i-1)/sqr+1;
sort(q+1,q+M+1,cmp); int l=bel[q[1].l]*sqr,r=bel[q[1].l]*sqr;
for(int i=1;i<=M;i++){
//cout<<q[i].l<<" "<<q[i].r<<":"<<bel[q[i].l]<<" "<<bel[q[i].r]<<endl;
l=bel[q[i].l]*sqr; //l=min(l,N);
if(bel[q[i].l]!=bel[q[i-1].l]) r=bel[q[i].l]*sqr,init_over();
/*if(bel[q[i].l]==bel[q[i].r]){
res=0;
for(int j=q[i].l;j<=q[i].r;j++){
res=max(res,L[a[j]-1]+R[a[j]+1]+1);
R[a[j]-L[a[j]-1]]=L[a[j]+R[a[j]+1]]=L[a[j]-1]+R[a[j]+1]+1;
}
ans[q[i].id]=res;
for(int j=q[i].l;j<=q[i].r;j++) L[a[j]]=R[a[j]]=0;
continue;
}*/
while(r<q[i].r){
++r; res=max(res,L[a[r]-1]+R[a[r]+1]+1);
R[a[r]-L[a[r]-1]]=L[a[r]+R[a[r]+1]]=L[a[r]-1]+R[a[r]+1]+1;
} top=0;
int res2=res;
for(l=min(l,q[i].r);l>=q[i].l;--l){
res2=max(res2,L[a[l]-1]+R[a[l]+1]+1);
int ll=a[l]-L[a[l]-1],rr=a[l]+R[a[l]+1];
staR[++top].pos=ll; staR[top].x=R[ll];
staL[top].pos=rr; staL[top].x=L[rr];
R[ll]=L[rr]=L[a[l]-1]+R[a[l]+1]+1;
} ans[q[i].id]=res2;
for(;top;--top){
R[staR[top].pos]=staR[top].x;
L[staL[top].pos]=staL[top].x;
}
}
for(int i=1;i<=M;i++) printf("%d\n",ans[i]);
return 0;
}
[NOIP模拟赛] seq的更多相关文章
- NOIP模拟赛 6.29
2017-6-29 NOIP模拟赛 Problem 1 机器人(robot.cpp/c/pas) [题目描述] 早苗入手了最新的Gundam模型.最新款自然有着与以往不同的功能,那就是它能够自动行走, ...
- NOIP模拟赛20161022
NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...
- contesthunter暑假NOIP模拟赛第一场题解
contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...
- NOIP模拟赛 by hzwer
2015年10月04日NOIP模拟赛 by hzwer (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...
- 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程
数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...
- 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...
- 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...
- 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...
- CH Round #58 - OrzCC杯noip模拟赛day2
A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...
随机推荐
- 2009 Round2 A Crazy Rows (模拟)
Problem You are given an N x N matrix with 0 and 1 values. You can swap any two adjacent rows of the ...
- 24、redis中的sentinel的作用?
redis中的sentinel的作用? Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-slave的高可用方案时,假如master宕机了,Re ...
- vue清空input file
input file是只读的,给form一个id,用form.reset()干掉里面input的值 document.getElementById("uploadForm")&am ...
- bzoj 3450 DP
首先我们设len[i]表示前i位,从第i位往前拓展,期望有多少个'o',那么比较容易的转移 len[i]=len[i-1]+1 s[i]='o' len[i]=0 s[i]='x' len[i]=(l ...
- import学习
一.import as import socket, os, regex模块导入时可以使用 as 关键字来改变模块的引用对象名字: import os as system //当多个引入时 ...
- Call Mode feature
起源 user 在插著 充電器 打電話的狀況下, 為了安全起見, 避免 充電器在這時損害手機,間接造成 user 的傷害, 而有了這 feature, 在 battery voltage Vbat & ...
- sicily 数据结构 1014. Translation
Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...
- hadoop 安装 错误及解决方法
1.ssh 相关问题: rm ~/.ssh/known_hosts //与ssh中的不服 //再重新生成密钥 2.ERROR namenode.NameNode: java.io.IOExceptio ...
- MYSQL 索引无效和索引有效的详细介绍
1.WHERE字句的查询条件里有不等于号(WHERE column!=...),MYSQL将无法使用索引 2.类似地,如果WHERE字句的查询条件里使用了函数(如:WHERE DAY(column)= ...
- P1466 集合 Subset Sums(01背包求填充方案数)
题目链接:https://www.luogu.org/problem/show?pid=1466 题目大意:对于从1到N (1 <= N <= 39) 的连续整数集合,能划分成两个子集合, ...