CF452F Permutations/Luogu2757 等差子序列 树状数组、Hash
如果存在长度\(>3\)的等差子序列,那么一定存在长度\(=3\)的等差子序列,所以我们只需要找长度为\(3\)的等差子序列。可以枚举等差子序列的第二个元素\(b\),那么存在长度为\(3\)的等差子序列等价于:可以在\(b\)左边找到一个元素\(a\),在\(b\)右边找到一个元素\(c\),满足\(b - a = c - b\)。
对于找到\(ac\)两个元素,一个比较直观的想法是:对\(b\)左边和右边的所有元素各建一个bitset\(B1,B2\),对于某一个元素\(d \neq b\),如果\(d\)在\(b\)左边,那么\(B1[d]=1\),否则\(B2[d]=1\)。
不存在等差子序列意味着如果\(d\)在左边,则\(2 \times b - d\)一定不能在右边,反之同理。这等价于对于\([l,b-1](l \geq 1),[b + 1 , r](r \leq N)\),满足\(b - l = r - b\)时,有
\[B1[l,b-1] \lor rev(B2[b+1 , r]) = 2^{b - l} - 1\]
上面两个条件等价的原因是:若结果的第\(d\)位为\(0\),则\(B1[l,b-1]\)和\(rev(B2[b+1 , r])\)的第\(d\)位要么同时为\(0\),要么同时为\(1\)。若同时为\(0\)意味着\(b-d\)不在左边且\(b+d\)不在右边,同时为\(1\)意味着\(b-d\)在左边且\(b+d\)在右边,都存在等差子序列。如果其中有一个为\(0\),有一个为\(1\),\(b-d\)和\(b+d\)就会在同一边。
这样就可以从右往左枚举\(b\)的位置,动态维护\(B1,B2\)并查询。但是当数据范围到\(3 \times 10^5\)的时候bitset会TLE,这时可以使用Hash+树状数组维护与上面bitset意义相同的01串。复杂度变为\(O(nlogn)\)。
虽然CF是神机,但仍然需要注意常数。
//Luogu2757
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstdlib>
#include<algorithm>
#include<cstring>
//This code is written by Itst
using namespace std;
inline int read(){
int a = 0;
char c = getchar();
while(!isdigit(c)) c = getchar();
while(isdigit(c)){
a = a * 10 + c - 48;
c = getchar();
}
return a;
}
#define st first
#define nd second
struct PII{
int first , second;
PII(int x = 0 , int y = 0):first(x) , second(y){}
bool operator ==(PII a){return a.st == first && a.nd == second;}
bool operator !=(PII a){return !(*this == a);}
};
const int MAXN = 1e4 + 7 , Base = 131 , MOD1 = 1e9 + 7 , MOD2 = 1e9 + 9;
PII powBs[MAXN] , sum[MAXN] , powInv[MAXN];
int arr[MAXN] , N , T;
bool f;
PII operator *(PII a , PII b){
return PII(1ll * a.st * b.st % MOD1 , 1ll * a.nd * b.nd % MOD2);
}
PII operator +(PII a , PII b){
PII t(a.st + b.st , a.nd + b.nd);
if(t.st >= MOD1) t.st -= MOD1;
if(t.nd >= MOD2) t.nd -= MOD2;
return t;
}
PII operator -(PII a , PII b){return a + PII(MOD1 - b.st , MOD2 - b.nd);}
struct BIT{
#define lowbit(x) (x & -x)
PII arr[MAXN];
BIT(){memset(arr , 0 , sizeof(PII) * (N + 1));}
void add(int pos , PII cur){
while(pos <= N){
arr[pos] = arr[pos] + cur;
pos += lowbit(pos);
}
}
PII get(int pos){
PII sum = PII(0 , 0);
while(pos){
sum = sum + arr[pos];
pos -= lowbit(pos);
}
return sum;
}
}Tree1 , Tree2;
inline int poww(long long a , int b , int MOD){
int times = 1;
while(b){
if(b & 1) times = times * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return times;
}
void init(){
powBs[0] = sum[0] = powInv[0] = PII(1 , 1);
for(int i = 1 ; i <= 10000 ; ++i){
powBs[i] = powBs[i - 1] * PII(Base , Base);
sum[i] = sum[i - 1] + powBs[i];
}
powInv[1] = PII(poww(Base , MOD1 - 2 , MOD1) , poww(Base , MOD2 - 2 , MOD2));
for(int i = 2 ; i <= 10000 ; ++i)
powInv[i] = powInv[i - 1] * powInv[1];
}
void work(){
for(int i = 1 ; i <= N ; ++i)
Tree1.add(arr[i] , powBs[arr[i]]);
for(int i = N ; i ; --i){
Tree1.add(arr[i] , PII(0 , 0) - powBs[arr[i]]);
if(arr[i] != 1 && arr[i] != N){
int l1 = 1 , r1 = arr[i] - 1 , l2 = arr[i] + 1 , r2 = N;
if(r2 - l2 < r1 - l1) l1 = r1 - (r2 - l2);
else r2 = l2 + (r1 - l1);
PII t = (Tree1.get(r1) - Tree1.get(l1 - 1)) * powInv[l1] + (Tree2.get(N + 1 - l2) - Tree2.get(N - r2)) * powInv[N - r2 + 1];
if(t != sum[r1 - l1]){
f = 1;
return;
}
}
Tree2.add(N - arr[i] + 1 , powBs[N - arr[i] + 1]);
}
}
signed main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
//freopen("out","w",stdout);
#endif
init();
for(int T = read() ; T ; --T){
N = read();
f = 0;
for(int i = 1 ; i <= N ; ++i)
arr[i] = read();
Tree1 = BIT(); Tree2 = BIT();
work();
puts(f ? "Y" : "N");
}
return 0;
}
CF452F Permutations/Luogu2757 等差子序列 树状数组、Hash的更多相关文章
- bzoj 2124 等差子序列 树状数组维护hash+回文串
等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 1919 Solved: 713[Submit][Status][Discuss] Desc ...
- 【BZOJ2124】等差子序列 树状数组维护hash值
[BZOJ2124]等差子序列 Description 给一个1到N的排列{Ai},询问是否存在1<=p1<p2<p3<p4<p5<…<pLen<=N ...
- bzoj 1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛【dp+树状数组+hash】
最长上升子序列.虽然数据可以直接n方但是另写了个nlogn的 转移:f[i]=max(f[j]+1)(a[j]<a[i]) O(n^2) #include<iostream> #in ...
- bzoj2124 等差子序列(树状数组+hash)
题意 给你一个1~n排列,问有没有一个等差数列(长度至少为3) 题解 我居然自己想到了正解. 但我最后写挂了,所以我又看了题解. 我们维护了一个以权值为下标的01序列. 我们扫描整个序列.对于每一个正 ...
- 【bzoj5157】[Tjoi2014]上升子序列 树状数组
题目描述 求一个数列本质不同的至少含有两个元素的上升子序列数目模10^9+7的结果. 题解 树状数组 傻逼题,离散化后直接使用树状数组统计即可.由于要求本质不同,因此一个数要减去它前一次出现时的贡献( ...
- Maximum Subsequence Sum【最大连续子序列+树状数组解决】
Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i < ...
- bzoj5157: [Tjoi2014]上升子序列(树状数组LIS)
5157: [Tjoi2014]上升子序列 题目:传送门 题解: 学一下nlogn的树状数组求最长上生子序列就ok(%爆大佬) 离散化之后,用一个数组记录一下,直接树状数组做 吐槽:妈耶...一开始不 ...
- BZOJ2124: 等差子序列(树状数组&hash -> bitset 求是否存在长度为3的等差数列)
2124: 等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 2354 Solved: 826[Submit][Status][Discuss ...
- BZOJ 3173 最长上升子序列(树状数组+二分+线段树)
给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上升子序列长度是多少? 由于序列是顺序插入的,所以当前插入的数字对之 ...
随机推荐
- MyBatis之分页插件(PageHelper)工作原理
数据分页功能是我们软件系统中必备的功能,在持久层使用mybatis的情况下,pageHelper来实现后台分页则是我们常用的一个选择,所以本文专门类介绍下. PageHelper原理 相关依赖 & ...
- webpack4.0各个击破(6)—— Loader篇
webpack作为前端最火的构建工具,是前端自动化工具链最重要的部分,使用门槛较高.本系列是笔者自己的学习记录,比较基础,希望通过问题 + 解决方式的模式,以前端构建中遇到的具体需求为出发点,学习we ...
- BOM简单知识
JS分为ECMAScript,DOM,BOM BOM是用来和浏览器进行‘’对话‘’的 一:与window对象进行交互: 1.查看用户信息: window.navigator.userAgent; 可以 ...
- qduoj前端二次开发简略流程
为缩减篇幅,已略去nodejs.git等软件安装操作,若有疑问请搜索相关教程. 为区分win和ubuntu的命令,作如下约定: $ cd //以$标记win下命令 # cd //以#标记linux命令 ...
- 1.SDL介绍
01.什么是SDL SDL是微软提出的一种软件开发安全生命周期管理的一种最佳安全实践,全称为Security Development Lifecycle. SDL是微软软件开发安全保障流程,结合了软件 ...
- Dynamics 365中的非交互式账号(Non-interactive User)介绍
摘要: 本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复272或者20180616可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyon ...
- java 线程方法 ---- join()
class MyThread2 implements Runnable{ @Override public void run() { for (int i = 0; i < 5; i++){ S ...
- jupyter notebook安装、登录
pip install jupyter 提示pip需要升级(本人装的是anaconda) 输入:python -m pip install --upgrade pip 安装完成. 运行jupyter ...
- C#设置电脑时间帮助类
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...
- Skywalking部署常见问题以及注意事项
Skywalking部署常见问题以及注意事项 Intro SkyWalking 创建与2015年,提供分布式追踪功能.从5.x开始,项目进化为一个完成功能的Application Performanc ...