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的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上升子序列长度是多少? 由于序列是顺序插入的,所以当前插入的数字对之 ...
随机推荐
- Linux基础知识第九讲,linux中的解压缩,以及软件安装命令
目录 Linux基础知识第九讲,linux中的解压缩,以及软件安装命令 一丶Linux Mac Windows下的压缩格式简介 2.压缩以及解压缩 3.linux中的软件安装以及卸载 1.apt进行安 ...
- JDK源码分析(5)之 HashMap 相关
HashMap作为我们最常用的数据类型,当然有必要了解一下他内部是实现细节.相比于 JDK7 在JDK8 中引入了红黑树以及hash计算等方面的优化,使得 JDK8 中的HashMap效率要高于以往的 ...
- 聊聊 scala 的模式匹配
一. scala 模式匹配(pattern matching) pattern matching 可以说是 scala 中十分强大的一个语言特性,当然这不是 scala 独有的,但这不妨碍它成为 sc ...
- Linux 进程管理工具 supervisord 安装及使用
Supervisor是用Python实现的一款非常实用的进程管理工具 1.安装过程非常简单 安装python 安装meld3-0.6.8.tar.gz 安装supervisor-3.0a12.tar. ...
- 前端异步技术之Promise
前言 从事前端的朋友或多或少的接触过Promise,当代码中回调函数层级过多你就会发现Promise异步编程的魅力,相信此文一定能帮你排忧解惑! Promise概念 Promise是JS异步编程中的重 ...
- response.redirect 与location.href 的区别
最近做项目时发现,先弹出提示框,再跳转页面 这样写:Jscript.Alert("你好,Hello!"); Response.Redirect("/index.aspx& ...
- keras & tensorflow 列出可用GPU 和 切换CPU & GPU
列出可用GPU from tensorflow.python.client import device_lib print(device_lib.list_local_devices()) from ...
- windows10 企业版完整激活
windows10 企业版完整激活 cmd管理员运行 1. 以管理员身份执行cmd命令,然后输入以下命令: slmgr.vbs /upk 由于Win10正式版允许在命令提示符界面使用"Ct ...
- [Android framework学习] ViewGroup的addView函数分析
博客首页:http://www.cnblogs.com/kezhuang/p/ Android中整个的View的组装是采用组合模式. ViewGroup就相当与树根,各种Layout就相当于枝干,各种 ...
- Python使用Plotly绘图工具,绘制气泡图
今天来讲讲如何使用Python 绘图工具,Plotly来绘制气泡图. 气泡图的实现方法类似散点图的实现.修改散点图中点的大小,就变成气泡图. 实现代码如下: import plotly as py i ...