Advertisement
nullzero

Little Elephant and Array

Aug 31st, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <cstdio>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. const int N(100001);
  8. typedef pair<int,int> II;
  9.  
  10. int main(){
  11.     vector<int> lv[N];
  12.     vector<pair<II,int>> arr;
  13.     int n, q, a, b, v, g;
  14.     g = scanf("%d %d", &n, &q);
  15.     for(int i = 1; i <= n; ++i){
  16.         g = scanf("%d", &v);
  17.         if(v > n) continue;
  18.         lv[v].push_back(i);
  19.     }
  20.     int round[] = {1, -2, 1};
  21.     for(int i = 1; i <= n; ++i){
  22.         for(int k = 0; k <= 2; ++k){
  23.             int l = 0, r = i + k - 1;
  24.             while(r < (int)lv[i].size())
  25.                 arr.push_back(make_pair(II(lv[i][l++], lv[i][r++]), round[k]));
  26.         }
  27.     }
  28.     sort(arr.begin(), arr.end());
  29.     for(int t = 0; t < q; ++t){
  30.         g = scanf("%d %d", &a, &b);
  31.         int cnt = 0;
  32.         auto x = lower_bound(arr.begin(), arr.end(), make_pair(II(a, -1), -1));
  33.         while(x != arr.end() and (x->first).first <= b){
  34.             if((x->first).second <= b) cnt += x->second;
  35.             x++;
  36.         }
  37.         printf("%d\n", cnt);
  38.     }
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement