码迷,mamicode.com
首页 > 其他好文 > 详细

Loj #6287 诗歌

时间:2018-02-26 11:20:23      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:ring   bsp   修改   using   AC   amp   log   stream   str   

link: https://loj.ac/problem/6287

一开始差点写FFT了23333,并且FFT还能算这样的三元组的数量而且还不用要求这是一个排列。。。。

但这太大材小用了(而且很可能被卡)

 

记得bzoj上有道类似的,大致的操作就是单点修改和查询当前点到边界的对称子串是否是回文串。。。

这个HASH一下就好了,(但是本蒟蒻第一次写hash)

 

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#define ll long long
#define maxn 300005
using namespace std;
const int ba=131;
const int ha=998244353;
int ci[maxn],f[maxn];
int a[maxn],n,m;
int g[maxn];

inline int add(int x,int y){
	x+=y;
	if(x>=ha) return x-ha;
	else return x;
}

inline int queryqz(int x){
	int an=0;
	for(;x;x-=x&-x) an=add(an,f[x]);
	return an;
}

inline void updateqz(int x,int y){
	for(;x<=n;x+=x&-x) f[x]=add(f[x],y);
}

inline int queryhz(int x){
	int an=0;
	for(;x<=n;x+=x&-x) an=add(an,g[x]);
	return an;
}

inline void updatehz(int x,int y){
	for(;x;x-=x&-x) g[x]=add(g[x],y);
}

inline bool solve(int x){
	int num=a[x],len=min(num,n-num+1);
	int o=queryqz(num),u=queryhz(num);
	
	if(len==num){
		if(n-num+1>len) u=add(u,ha-queryhz(num+len));
		o=o*(ll)ci[n-num-len+1]%ha; 
	}
	else{
		o=add(o,ha-queryqz(num-len));
		u=u*(ll)ci[num-len]%ha;
	}
	
	updateqz(num,ci[num]);
	updatehz(num,ci[n-num+1]);
	
	return u==o;
}

int main(){
	ci[0]=1;
	for(int i=1;i<=300000;i++) ci[i]=ci[i-1]*(ll)ba%ha;
	
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i);
	
	for(int i=1;i<=n;i++) if(!solve(i)){
		puts("YES");
		return 0;
	}
	
	puts("NO");
	return 0;
}

  

Loj #6287 诗歌

标签:ring   bsp   修改   using   AC   amp   log   stream   str   

原文地址:https://www.cnblogs.com/JYYHH/p/8470485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!