bzoj2124 等差子序列(树状数组+hash)

题意

给你一个1~n排列,问有没有一个等差数列(长度至少为3)

题解

我居然自己想到了正解。

但我最后写挂了,所以我又看了题解。

我们维护了一个以权值为下标的01序列。

我们扫描整个序列。对于每一个正在扫描的数,我们判断以这个数的权值作为对称点,01序列是否对称。

这个序列用权值树状数组维护就行。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<algorithm>
 6 #define LL long long
 7 const LL mod=1e9+7;
 8 int n,t;
 9 const int N=10010;
10 LL pw[N],c1[N],c2[N];
11 int a[N];
12 int min(int a,int b){
13     if(a<b)return a;
14     else return b;
15 }
16 int lowbit(int x){
17     return x&(-x);
18 }
19 LL check1(int x){
20     LL ans=0;
21     for(int i=x;i>=1;i-=lowbit(i)){
22         ans=(ans+(c1[i]*pw[x-i])%mod)%mod;
23     }
24     return ans;
25 }
26 LL check2(int x){
27     LL ans=0;
28     for(int i=x;i>=1;i-=lowbit(i)){
29         ans=(ans+(c2[i]*pw[x-i])%mod)%mod;
30     }
31     return ans;
32 }
33 LL add1(int x){
34        for(int i=x;i<=n;i+=lowbit(i)){
35            c1[i]=(c1[i]+pw[i-x])%mod;
36     }
37 }
38 LL add2(int x){
39     for(int i=x;i<=n;i+=lowbit(i)){
40         c2[i]=(c2[i]+pw[i-x])%mod;
41     }
42     return 0;
43 }
44 LL query1(int l,int r){
45     LL p=check1(l-1),q=check1(r);
46     return ((q-p*pw[r-l+1])%mod+mod)%mod;
47 }
48 LL query2(int l,int r){
49     LL p=check2(l-1),q=check2(r);
50     return ((q-p*pw[r-l+1])%mod+mod)%mod;
51 }
52 int main(){
53     scanf("%d",&t);
54     pw[0]=1;
55     for(int i=1;i<=10001;i++)pw[i]=(pw[i-1]*(LL)2)%mod;
56        for(int z=1;z<=t;z++){
57         scanf("%d",&n);
58         for(int i=1;i<=n;i++)scanf("%d",&a[i]);
59         memset(c1,0,sizeof(c1));memset(c2,0,sizeof(c2));
60         for(int i=1;i<=n;i++){
61             int x=a[i];
62             int len=min(n-x,x-1);
63             if(len&&query1(x-len,x-1)!=query2(n-x-len+1,n-x)){printf("Y\n");break;}
64             add1(x);add2(n-x+1);
65             if(i==n)printf("N\n");
66         }
67     }
68     return 0;
69 }

原文地址:https://www.cnblogs.com/Xu-daxia/p/9402502.html

时间: 2024-08-13 05:02:32

bzoj2124 等差子序列(树状数组+hash)的相关文章

bzoj 2124 等差子序列 树状数组维护hash+回文串

等差子序列 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 1919  Solved: 713[Submit][Status][Discuss] Description 给一个1到N的排列{Ai},询问是否存在1<=p1<p2<p3<p4<p5<…<pLen<=N (Len>=3), 使得Ap1,Ap2,Ap3,…ApLen是一个等差序列. Input 输入的第一行包含一个整数T,表示组数. 下接T组数据,

CF452F Permutations/Luogu2757 等差子序列 树状数组、Hash

传送门--Luogu 传送门--Codeforces 这种题目还能跟哈希扯上关系也是很神了-- 如果存在长度\(>3\)的等差子序列,那么一定存在长度\(=3\)的等差子序列,所以我们只需要找长度为\(3\)的等差子序列.可以枚举等差子序列的第二个元素\(b\),那么存在长度为\(3\)的等差子序列等价于:可以在\(b\)左边找到一个元素\(a\),在\(b\)右边找到一个元素\(c\),满足\(b - a = c - b\). 对于找到\(ac\)两个元素,一个比较直观的想法是:对\(b\)左

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> #include<cstdio> using namespace std; const int N=5005; int n,a[N],f[N],ans; int read() { int r=0,f=1; char p=getchar(); while(p>'9'||p<'0') { if(

AC dreamoj 1011 树状数组+hash维护字符串的前缀和

http://acdream.info/problem?pid=1019 Problem Description Now we have a long long string, and we will have two kinds of operation on it. C i y : change the ith letter to y. Q i j : check whether the substring from ith letter to jth letter is a palindr

bzoj3173 最长上升子序列 树状数组

链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3173 题意:向序列中动态插入$1~n$排列元素,求出插入每个元素后最长上升子序列长度. 如Claris所言,面对这种数据结构,必有高论.如果只想着数据结构,我们可以通过平衡树动态维护序列,同时使用树状数组计算最长上升子序列. 但是我们不是猩猩不是数据结构狂人,我们毕竟还是要本着能不上树就不上树能少用数据结构就少用的原则来设计算法的. 重新考虑这个题目.本题没有要求强制在线,于是我们把整个操作

hdu 5773 The All-purpose Zero 最长上升子序列+树状数组

题目链接:hdu 5773 The All-purpose Zero 官方题解:0可以转化成任意整数,包括负数,显然求LIS时尽量把0都放进去必定是正确的. 因此我们可以把0拿出来,对剩下的做O(nlogn)的LIS,统计结果的时候再算上0的数量. 为了保证严格递增,我们可以将每个权值S[i]减去i前面0的个数,再做LIS,就能保证结果是严格递增的. 个人看法:对于显然把所以0放进去部分我解释一下: 如果0位于最长上升子序列两边,这两个零要加进去是显然的 如果有一个0夹于最长上升子序列之间,那么

[bzoj2124]等差子序列(hash+树状数组)

我又来更博啦 2124: 等差子序列 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 941  Solved: 348[Submit][Status][Discuss] Description 给一个1到N的排列{Ai},询问是否存在1<=p1=3),使得Ap1,Ap2,Ap3,…ApLen是一个等差序列. Input 输入的第一行包含一个整数T,表示组数.下接T组数据,每组第一行一个整数N,每组第二行为一个1到N的排列,数字两两之间用空格隔开. O

ACdreamoj 1011(树状数组维护字符串hash前缀和)

题目链接:http://acdream.info/problem? pid=1019 题意:两种操作,第一种将字符串某个位置的字符换为还有一个字符.另外一种查询某个连续子序列是否是回文串: 解法:有两种hash的办法,所以写了两种解法;首先hash是x1 * p^1+ x2*p^2 +x3*p^3...能够用树状数组维护前缀和,维护两个串,一个是正串.还有一个是反串用于比較.比較时候乘以对应的p倍数推断是否相等. 刘汝佳白书上的hash方法处理这道题更复杂:改动i会对后缀j产生的影响为a*p^(

POJ2299 Ultra-QuickSort 【树状数组】+【hash】

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 39529   Accepted: 14250 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin