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 ;
}

你可能感兴趣的:(UVA 12937 字典树)