zoukankan      html  css  js  c++  java
  • BZOJ 4866 [Ynoi2017]由乃的商场之旅

    题解:

    把每个字母表示成二进制

    令sum[i]表示i的前缀异或

    则一个区间是合法的条件是异或和=0或只有一位是1

    上莫队,然后T了

    优化先留个坑

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn=60009;
    const int SIZ=400;
    const int u=26;
    
    int n,m;
    char ss[maxn];
    int a[maxn]={0};
    int s[maxn]={0};
    
    int ql[maxn],qr[maxn],p[maxn];
    long long ans[maxn];
    bool cmp(const int &rhs1,const int &rhs2){
    	if(ql[rhs1]/SIZ==ql[rhs2]/SIZ){
    		return qr[rhs1]<qr[rhs2];
    	}else{
    		return ql[rhs1]<ql[rhs2];
    	}
    }
    unsigned short tong[1<<26]={0};
    
    int Cal(int x){
    	int ret=tong[x];
    	for(int i=1;i<=26;++i){
    		ret+=tong[x^(1<<(i-1))];
    	}
    	return ret;
    }
    
    int main(){
    	scanf("%d%d",&n,&m);
    	scanf("%s",ss+1);
    	for(int i=1;i<=n;++i)a[i]=(1<<(ss[i]-'a'));
    	
    	for(int i=1;i<=n;++i)s[i]=s[i-1]^a[i];
    	for(int i=1;i<=m;++i)scanf("%d%d",&ql[i],&qr[i]);
    	for(int i=1;i<=m;++i)--ql[i];
    	for(int i=1;i<=m;++i)p[i]=i;
    	sort(p+1,p+1+m,cmp);
    	
    	long long nowans=0;
    	int p1=1,p2=0;
    	for(int i=1;i<=m;++i){
    		int t=p[i];
    		int l=ql[t],r=qr[t];
    		
    		while(p1>l){
    			--p1;
    			nowans+=Cal(s[p1]);
    			++tong[s[p1]];
    		}
    		while(p2<r){
    			++p2;
    			nowans+=Cal(s[p2]);
    			++tong[s[p2]];
    		}
    		while(p1<l){
    			--tong[s[p1]];
    			nowans-=Cal(s[p1]);
    			++p1;
    		}
    		while(p2>r){
    			--tong[s[p2]];
    			nowans-=Cal(s[p2]);
    			--p2;
    		}
    		ans[t]=nowans;
    	}
    	for(int i=1;i<=m;++i)printf("%lld
    ",ans[i]);
    	return 0;
    }
    

      

    自己还是太辣鸡了
  • 相关阅读:
    poj 2728 Desert King
    uva 439 Knight Moves
    hdu 1875 畅通工程再续
    scau实验题 8600 骑士周游问题(有障碍物)
    scau实验题 8596 Longest Ordered Subsequence
    poj 1679 The Unique MST
    uva 527 Oil Deposits
    poj 2533 Longest Ordered Subsequence
    .net 程序员 java 开发入门
    Collation conflict occur at operation on User define funtion & table's column
  • 原文地址:https://www.cnblogs.com/zzyer/p/8562848.html
Copyright © 2011-2022 走看看