hdu4737区间统计

给出N个数,然后问里面有多少个子串,对于每个子串做或运算的结果小于m。

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NavigableSet;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeSet;

import org.omg.CORBA.Object;




public class Main {


    public static void main(String[] args) throws IOException{

           StreamTokenizer cin = new StreamTokenizer(new BufferedInputStream(System.in)); 
           InputReader in = new InputReader(System.in)  ;
           PrintWriter out = new PrintWriter(System.out) ;

           int t = in.nextInt() ;
           for(int i = 1 ; i <= t ; i++){
                out.print("Case #" + i + ": ")  ;
                new Task().solve(in, out)  ;   //out.flush() ;
           }

           out.flush() ; 


    }


}

class Task{

       static int[] bit = new int[31] ;
       static {
              for(int i = 0 ; i <= 30 ; i++) bit[i] = (1<<i) ;
       }

       static boolean[][] dig  = new boolean[100001][31] ; 
       int n , m  ;

       int[] cnt = new int[31] ;

       boolean  f(){
                int s = 0 ;
                for(int i = 0 ; i <= 30 ; i++){
                      if(cnt[i] > 0) s |= bit[i] ; 
                }
                return s < m ;
       }

       public void solve(InputReader in , PrintWriter out) throws IOException{
               n = in.nextInt() ;
               m = in.nextInt() ;

               for(int i = 1 ; i <= n ; i++){
                    int x = in.nextInt() ;
                    for(int j = 0 ; j <= 30 ; j++){
                          if((bit[j] & x) > 0) dig[i][j] = true ;
                          else dig[i][j] = false ;
                    }
               }

               long sum = 0L ;

               Arrays.fill(cnt, 0) ;
               int head = 1 , tail = 1 ;
               while(tail <= n){
                      for(int i = 0 ; i <= 30 ; i++){
                            if(dig[tail][i]) cnt[i]++ ;
                      }

                      while(head <= tail && !f()){
                            for(int i = 0 ; i <= 30 ; i++){
                                  if(dig[head][i]) cnt[i]-- ;
                            }
                            head++ ;
                      }

                      sum += tail - head + 1 ;
                      tail++ ;
               }

               out.println(sum) ;
        }

}


class InputReader{
        public BufferedReader  reader;
        public StringTokenizer  tokenizer;

        public InputReader(InputStream stream){
                    reader = new BufferedReader(new InputStreamReader(stream), 32768) ;
                    tokenizer = null ;
        }

        public String next(){
                    while(tokenizer == null || ! tokenizer.hasMoreTokens()){
                        try{
                                tokenizer = new StringTokenizer(reader.readLine());
                        }catch (IOException e) {
                                throw new RuntimeException(e);
                        }
                    }
                    return tokenizer.nextToken();  
        }

        public int  nextInt() {
                    return Integer.parseInt(next());
        }

        public long nextLong() {
                    return Long.parseLong(next());
        }

}

你可能感兴趣的:(hdu4737区间统计)