题目链接:3207: 花神的嘲讽计划Ⅰ
Hash值扔进可持久化线段树中即可,代码写了有些时间了
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define hash_base 131
#define inf 18446744073709551615ULL
using namespace std;
#define ULL unsigned long long
const int maxn=150000+1000;
int T[maxn],lson[maxn*66],rson[maxn*66];
int c[maxn*66],a[maxn];
int n,m,k,tot=1;
int update(int root,ULL pos,int val){
if (!root) root=tot++,c[root]=0;
int nr=tot++,tmp=nr;
c[nr]=c[root]+val;
ULL l=0,r=inf;
while (l<r){
ULL mid=(l>>1)+(r>>1)+(l&r&1);
if (pos<=mid){
lson[nr]=tot++; rson[nr]=rson[root];
r=mid; nr=lson[nr]; root=lson[root];
}else{
lson[nr]=lson[root]; rson[nr]=tot++;
l=mid+1; nr=rson[nr]; root=rson[root];
}c[nr]=c[root]+val;
}return tmp;
}
bool query(int x,int y,ULL l,ULL r,ULL v){
if (c[y]-c[x]==0) return 0;
if (l==r) return 1;
ULL mid=(l>>1)+(r>>1)+(l&r&1);
if (v<=mid) return query(lson[x],lson[y],l,mid,v);
else return query(rson[x],rson[y],mid+1,r,v);
}
int main(){
scanf("%d%d%d",&n,&m,&k);
for (int i=1;i<=n;++i) scanf("%d",&a[i]);
for (int i=1;i<=n;++i){
ULL hash=0;
for (int j=i;j<=i+k-1;++j)
hash=hash*1ULL*hash_base+a[j];
T[i]=update(T[i-1],hash,1);
}
for (int i=1;i<=m;++i){
int x,y; scanf("%d%d",&x,&y);
ULL hash=0;int val;
for (int j=1;j<=k;++j){
scanf("%d",&val);
hash=hash*1ULL*hash_base+val;
}
if (query(T[x-1],T[y-k+1],0,inf,hash)) printf("No\n");
else printf("Yes\n");
}
}
题目链接:
2653: middle
大爷题解传送门:orzorzorz
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=300000+10;
int n,m,s[maxn],ls[maxn],rs[maxn],T[maxn];
int tot=1,q[4],lson[maxn],rson[maxn];
struct Seq{int v,id;}seq[maxn];
bool cmp(const Seq &a,const Seq &b){
return a.v<b.v;
}
void push_up(int x){
s[x]=s[lson[x]]+s[rson[x]];
ls[x]=max(ls[lson[x]],s[lson[x]]+ls[rson[x]]);
rs[x]=max(rs[rson[x]],s[rson[x]]+rs[lson[x]]);
}
int build(int l,int r){
int root=tot++;
if(l==r){s[root]=ls[root]=rs[root]=1;return root;}
int mid=(l+r)>>1;
lson[root]=build(l,mid);
rson[root]=build(mid+1,r);
push_up(root); return root;
}
int update(int root,int l,int r,int pos,int val){
int mid=(l+r)>>1,nr=tot++;
if (l==r){s[nr]=ls[nr]=rs[nr]=val;return nr;}
if (pos<=mid){
lson[nr]=update(lson[root],l,mid,pos,val);
rson[nr]=rson[root];
}else if (pos>mid){
rson[nr]=update(rson[root],mid+1,r,pos,val);
lson[nr]=lson[root];
}push_up(nr); return nr;
}
int get1(int root,int l,int r,int L,int R){
if (l==L&&r==R) return s[root];
int mid=(l+r)>>1;
if (R<=mid) return get1(lson[root],l,mid,L,R);
else if (L>mid) return get1(rson[root],mid+1,r,L,R);
else return get1(lson[root],l,mid,L,mid)+get1(rson[root],mid+1,r,mid+1,R);
}
int get2(int root,int l,int r,int L,int R){
if (l==L&&r==R) return ls[root];
int mid=(l+r)>>1;
if (R<=mid) return get2(lson[root],l,mid,L,R);
else if (L>mid) return get2(rson[root],mid+1,r,L,R);
else return max(get2(lson[root],l,mid,L,mid),get1(lson[root],l,mid,L,mid)+get2(rson[root],mid+1,r,mid+1,R));
}
int get3(int root,int l,int r,int L,int R){
if (l==L&&r==R) return rs[root];
int mid=(l+r)>>1;
if (R<=mid) return get3(lson[root],l,mid,L,R);
else if (L>mid) return get3(rson[root],mid+1,r,L,R);
else return max(get3(lson[root],l,mid,L,mid)+get1(rson[root],mid+1,r,mid+1,R),get3(rson[root],mid+1,r,mid+1,R));
}
bool check(int x,int a,int b,int c,int d){
int num=0; if (b+1<=c-1) num+=get1(T[x],0,n-1,b+1,c-1);
num+=get3(T[x],0,n-1,a,b); num+=get2(T[x],0,n-1,c,d);
return num>=0;
}
int main(){
scanf("%d",&n);
for (int i=0;i<n;++i){
scanf("%d",&seq[i].v);
seq[i].id=i;
}
sort(seq,seq+n,cmp);
T[0]=build(0,n-1);
for (int i=1;i<=n;++i)
T[i]=update(T[i-1],0,n-1,seq[i-1].id,-1);
scanf("%d",&m); int ans=0;
for (int i=1;i<=m;++i){
for (int j=0;j<4;++j)
scanf("%d",&q[j]),q[j]=(q[j]+ans)%n;
sort(q,q+4);
int L=0,R=n-1,ret;
while (L<=R){
int mid=(L+R)>>1;
if (check(mid,q[0],q[1],q[2],q[3]))
L=mid+1,ret=mid;
else R=mid-1;
}ans=seq[ret].v; printf("%d\n",ans);
}
}