[NEERC13.H][ST表][乱搞]Hack Protection

来源:互联网 发布:网络方面 证书 编辑:程序博客网 时间:2024/06/08 05:09

枚举左端点i,因为左端点固定,那么区间的And值不超过31种(每次And一个数,只可能使这个数二进制下的某一位变成0),那么每次二分出他变化的区间,因为子串xor可以变成两个xor前缀的xor,所以只要找出在这个区间中xor前缀等于B[i] xor A,B[i]为1~i的xor值,A为这个区间的And值。

做这题时我是用二分加线段树有两个log,后来想在线段树上二分,打着打着猛然想起有种东西叫ST表…

#include <cstdio>#include <iostream>#include <algorithm>#include <vector>#include <map>#define N 100010using namespace std;int n;int A[N],B[N],Log2[N];int st[N][25];long long Ans;map<int,vector<int> > M;inline char nc(){  static char buf[100000],*p1=buf,*p2=buf;  return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}inline void rea(int &x){  char c=nc(); x=0;  for(;c>'9'||c<'0';c=nc());for(;c>='0'&&c<='9';x=x*10+c-'0',c=nc());}void Build(){  int t=Log2[n];  for(int i=1;i<=n;i++) st[i][0]=A[i];  for(int i=1;i<=t;i++)    for(int j=1;j+(1<<i-1)<=n;j++)      st[j][i]=(st[j][i-1]&st[j+(1<<i-1)][i-1]);}int query(int l,int r){  int t=Log2[r-l+1];  return st[l][t]&st[r-(1<<t)+1][t];}int main(){  freopen("hack.in","r",stdin);  freopen("hack.out","w",stdout);  rea(n);  for(int i=1;i<=n;i++) rea(A[i]);  for(int i=1;i<=n;i++) M[B[i]=B[i-1]^A[i]].push_back(i);  for(int i=1;i<=n;i++) Log2[i]=Log2[i-1]+((1<<Log2[i-1]+1)==i);  Build();  //for(int i=0;i<M[0].size();i++) printf("%d ",M[0][i]);  for(int i=1;i<=n;i++){    int cur=A[i],r=i;    for(int p;r<=n;r=p+1){      int L=r,R=n,mid; p=r;      //p=check(1,cur,p,n);      while(L<=R) query(i,mid=L+R>>1)==cur?L=(p=mid)+1:R=mid-1;      if(M.count(cur^B[i-1])){    int now=upper_bound(M[cur^B[i-1]].begin(),M[cur^B[i-1]].end(),p)-lower_bound(M[cur^B[i-1]].begin(),M[cur^B[i-1]].end(),r);    Ans+=now;      }      cur&=A[p+1];    }  }  cout<<Ans<<endl;  return 0;}
0 0