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

传送门——Luogu 传送门——Codeforces


这种题目还能跟哈希扯上关系也是很神了……

如果存在长度\(>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;
}

猜你喜欢

转载自www.cnblogs.com/Itst/p/10629524.html