2019CCPC湖南全国邀请赛(广东省赛、江苏省赛) C.Chika and Friendly Pairs

Problem Description

Chika gives you an integer sequence a1,a2,…,an and m tasks. For each task, you need to answer the number of "friendly pairs" in a given interval.

friendly pair: for two integers ai and aj, if i

 

 

Input

The first line contains 3 integers n (1≤n≤27000), m (1≤m≤27000) and K (1≤K≤109), representing the number of integers in the sequence a, the number of tasks and the given constant integer.
The second line contains n non-negative integers, representing the integers in the sequence a. Every integer of sequence a is no more than 109.
Then m lines follow, each of which contains two integers L, R (1≤L≤R≤n). The meaning is to ask the number of "friendly pairs" in the interval [L,R]。

 

 

Output

For each task, you need to print one line, including only one integer, representing the number of "friendly pairs" in the query interval.

 

 

Sample Input

 

7 5 3 2 5 7 5 1 5 6 6 6 1 3 4 6 2 4 3 4

 

 

Sample Output

 

0 2 1 3 1

思路:莫队加树状数组

 

#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn=30050;
struct Q{
    int l,r,id,blk;
};
bool operator < (Q a, Q b){
    return a.blkk)  l++;
        while(r<=num&&a[r]-a[i]<=k)  r++;
        r--;
        l0[i]=l;r0[i]=r;
    }
    blk=(int)sqrt(n);
    for(int i=1;i<=m;i++){
        scanf("%d %d",&l,&r);
        q[i].l=l;q[i].r=r;q[i].id=i;q[i].blk=l/blk;
    }
    sort(q+1,q+m+1);
    l=1,r=0;
    int sum=0;
    for(int i=1;i<=m;i++){
        while(lq[i].l){
            int _val=val[--l];
            sum+=query(r0[_val])-query(l0[_val]-1);add(_val,1);
        }
        while(rq[i].r){
            int _val=val[r--];
            add(_val,-1);sum-=query(r0[_val])-query(l0[_val]-1);
        }
        res[q[i].id]=sum;
    }
    for(int i=1;i<=m;i++)   printf("%d\n",res[i]);
}
int main(){
    solve();
    return 0;
}
/*
7 5 3
2 5 7 5 1 5 6
6 6
1 3
4 6
2 4
3 4
*/

 

你可能感兴趣的:(ccpc邀请赛)