Guest User

BIT

a guest
Jan 29th, 2017
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. // Abhay Singh Bhadoria ( _Asb_ ) //
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. const int inf = 2147483647;
  5. const int M = 1e9 + 7;
  6. const int N = 1e5 + 5;
  7. int bit[N][10];
  8. void update(int x,int n,int val,int d){
  9.     while(x<=n){
  10.         bit[x][d]+=val;
  11.         x+=x&(-x);
  12.     }
  13. }
  14. int query(int x,int n,int d){
  15.     int res(0);
  16.     while(x>0){
  17.         res+=bit[x][d];
  18.         x-=x&(-x);
  19.     }
  20.     return res;
  21. }
  22. int main(){
  23.     int n,m;
  24.     scanf("%d%d",&n,&m);
  25.     for(int i=1;i<=n;++i){
  26.         int x;
  27.         scanf("%d",&x);
  28.         while(x>0){
  29.             update(i,n,1,x%10);
  30.             x/=10;
  31.         }
  32.     }
  33.     while(m--){
  34.         int l,r,res(0);
  35.         scanf("%d%d",&l,&r);
  36.         for(int i=0;i<10;++i){
  37.             if(query(r,n,i)-query(l-1,n,i)>0)res++;
  38.         }
  39.         printf("%d\n",res);
  40.     }
  41. }
Add Comment
Please, Sign In to add comment