Lucky - HDU 5213 莫队算法

本文介绍了一种解决特定查询问题的算法实现,该问题涉及在一串整数中寻找能够组合成固定奇数K的数对,并通过莫队算法进行高效求解。

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
5 3 1 2 1 2 3 1 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]);
    }
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值