Lucky - HDU 5213 莫队算法

来源:互联网 发布:php多文件上传 编辑:程序博客网 时间:2024/06/06 00:50

Lucky

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 419    Accepted Submission(s): 143


Problem Description
WLD is always very lucky.His secret is a lucky number K.k is a fixed odd number. Now he meets a stranger with N numbers:a1,a2,...,aN.The stranger asks him M questions.Each question is like this:Given two ranges [Li,Ri] and [Ui,Vi],you can choose two numbers X and Y to make aX+aY=K.The X you can choose is between Li and Ri and the Y you can choose is between Ui and Vi.How many pairs of numbers(X,Y) you can choose?
If WLD can answer all the questions correctly,he'll be the luckiest man in the world.Can you help him?
 

Input
There are multiple cases.(At MOST 5)

For each case:

The first line contains an integer N(1N30000).

The following line contains an integer K(2K2N),WLD's lucky number.K is odd.

The following line contains N integers a1,a2,...,aN(1aiN).

The following line contains an integer M(1M30000),the sum of the questions WLD has to answer.

The following M lines,the i-th line contains 4 numbers Li,Ri,Ui,Vi(1LiRi<UiViN),describing the i-th question the stranger asks.
 

Output
For each case:

Print the total of pairs WLD can choose for each question.
 

Sample Input
53 1 2 1 2 31 1 2 3 5
 

Sample Output
2
Hint
a1+a4=a2+a3=3=K.So we have two pairs of numbers (1,4) and (2,3).Good luck!
 

题意:给出n个数个m个查询,对于每个查询找到ax(l<=x<=r)和ay(u<=y<=v)中有多少对使得ax+ay=K。

思路:这道题需要一些莫队算法的知识
定义记号f(A,B)表示询问区间A,B时的答案
用记号+表示集合的并
利用莫队算法我们可以计算出任意f(A,A)的值
不妨假设A=[l1,r1],B=[l2,r2],C=[r1+1,l2−1]
容易知道f(A,B)=f(A+B+C,A+B+C)+f(C,C)−f(A+C,A+C)−f(C+B,C+B)
因此一个询问被拆成四个可以用莫队算法做的询问

AC代码如下:

#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;struct node{    int l,r,ans,id,type;}arr[120010];int n,m,M,K,pos[30010],c[30010],num[60010],ans,f[30010];bool cmp(node a,node b){    return pos[a.l]<pos[b.l] ||(pos[a.l]==pos[b.l] && a.r<b.r);}void update(int p,int add){    ans+=num[K-c[p]]*add;    num[c[p]]+=add;}void solve(){    int i,j,k,l,r;    l=1;r=0;    ans=0;    memset(num,0,sizeof(num));    for(i=1;i<=M;i++)    {        for(;r<arr[i].r;r++)           update(r+1,1);        for(;r>arr[i].r;r--)           update(r,-1);        for(;l<arr[i].l;l++)           update(l,-1);        for(;l>arr[i].l;l--)           update(l-1,1);        arr[i].ans=ans;    }}int main(){    int i,j,k,l,r,u,v;    while(~scanf("%d",&n))    {        scanf("%d",&K);        for(i=1;i<=n;i++)           scanf("%d",&c[i]);        k=(int)sqrt(n);        for(i=1;i<=n;i++)           pos[i]=(i-1)/k+1;        scanf("%d",&m);        M=m*4;        for(i=1;i<=m;i++)        {            scanf("%d%d%d%d",&l,&r,&u,&v);            arr[i*4-3].l=l;arr[i*4-3].r=v;arr[i*4-3].id=i;arr[i*4-3].type=1;            arr[i*4-2].l=l;arr[i*4-2].r=u-1;arr[i*4-2].id=i;arr[i*4-2].type=-1;            arr[i*4-1].l=r+1;arr[i*4-1].r=v;arr[i*4-1].id=i;arr[i*4-1].type=-1;            arr[i*4  ].l=r+1;arr[i*4  ].r=u-1;arr[i*4  ].id=i;arr[i*4  ].type=1;        }        sort(arr+1,arr+1+M,cmp);        solve();        memset(f,0,sizeof(f));        for(i=1;i<=M;i++)           f[arr[i].id]+=arr[i].type*arr[i].ans;        for(i=1;i<=m;i++)           printf("%d\n",f[i]);    }}



0 0