UVA 12937 字典树

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <cmath>
#include <vector>
#include <map>
#include <string>
#include <string.h>
#include <set>
#include <queue>
#include <algorithm>
#include <sstream>
#include <queue>

using namespace std;
typedef long long LL ;

int  n ,  a , b ;
const int nodeSize = 300000 * 50 + 8  ;
std::vector<int>lable[nodeSize] ;
int _next[nodeSize][2] ;
struct Tire{
   int root ;
   int totel ;

   int newNode(){
       _next[totel][0] = _next[totel][1] = 0 ;
       lable[totel].clear() ;
       return totel++  ;
   }

   void clear(){
        totel = 0 ;
        root = newNode() ;
   }

   void add(LL x , int id){
       int now = root ;
       int son ;
       for(int i = 0 ; i < n ; i++){
           if(x & (1LL<<i)) son = 1 ;
           else son = 0 ;
           if(! _next[now][son])
               _next[now][son] = newNode() ;
           now = _next[now][son] ;
       }
       lable[now].push_back(id) ;
   }

   int find(LL x , int id){
       int now = root ;
       int son ;
       for(int i = 0 ; i < n ; i++){
           if(x & (1LL<<i)) son = 1 ;
           else son = 0 ;
           son ^= 1 ;
           if(! _next[now][son])
              return 0 ;
           now = _next[now][son] ;
       }
       int res = 0 ;
       for(std::vector<int>::iterator it = lable[now].begin() ; it != lable[now].end() ; it++){
           if(id - *it >= a && id - *it <= b) res++ ;
       }

       return res ;
   }
} ;

const int N = 300008 ;
LL  sum[N] ;
char light[58] ;


Tire tree ;
int main(){

   int  m , ca = 1 ;
   LL x ;
   while(scanf("%d%d%d%d" , &n , &m , &a , &b) != EOF){
       sum[0] = 0 ;
       for(int i = 1 ; i <= m ; i++){
           scanf("%s" , light) ;
           x = 0 ;
           for(int j = 0 ; j < n ; j++){
              if(light[j] == '1') x |= (1LL<<j) ;
           }
           sum[i] = sum[i-1] ^ x ;
       }

       LL res = 0 ;
       tree.clear() ;
       tree.add(0LL , 0) ;
       for(int i = 1 ; i <= m ; i++){
            res += tree.find(sum[i] , i) ;
            tree.add(sum[i] , i) ;
       }
       printf("Case %d: %lld\n" , ca++ , res) ;

   }

   return 0 ;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值