Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <vector>
- #include <algorithm>
- using namespace std;
- const int N(100001);
- typedef pair<int,int> II;
- int main(){
- vector<int> lv[N];
- vector<pair<II,int>> arr;
- int n, q, a, b, v, g;
- g = scanf("%d %d", &n, &q);
- for(int i = 1; i <= n; ++i){
- g = scanf("%d", &v);
- if(v > n) continue;
- lv[v].push_back(i);
- }
- int round[] = {1, -2, 1};
- for(int i = 1; i <= n; ++i){
- for(int k = 0; k <= 2; ++k){
- int l = 0, r = i + k - 1;
- while(r < (int)lv[i].size())
- arr.push_back(make_pair(II(lv[i][l++], lv[i][r++]), round[k]));
- }
- }
- sort(arr.begin(), arr.end());
- for(int t = 0; t < q; ++t){
- g = scanf("%d %d", &a, &b);
- int cnt = 0;
- auto x = lower_bound(arr.begin(), arr.end(), make_pair(II(a, -1), -1));
- while(x != arr.end() and (x->first).first <= b){
- if((x->first).second <= b) cnt += x->second;
- x++;
- }
- printf("%d\n", cnt);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement