Lucky - HDU 5213 莫队算法
2015-06-14 17:31
274 查看
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(1≤N≤30000).
The following line contains an integer K(2≤K≤2∗N),WLD's
lucky number.K is odd.
The following line contains N integers a1,a2,...,aN(1≤ai≤N).
The following line contains an integer M(1≤M≤30000),the
sum of the questions WLD has to answer.
The following M lines,the
i-th line contains 4 numbers Li,Ri,Ui,Vi(1≤Li≤Ri<Ui≤Vi≤N),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 Hinta1+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]); } }
相关文章推荐
- 探讨socket引发SIGPIPE信号的问题
- elasticsearch在window环境下的安装
- Html5多浏览器支持情况
- 完全二叉树的三种遍历方式
- 五大常用算法之五:分支限界法
- 五大常用算法之四:回溯算法
- 五大常用算法之三:贪心算法
- DevExpress DocumentManager 操作类
- 分类之数据集导入matlab方法
- Statistics Basis
- 五大常用算法之二:动态规划算法
- vs2012 导入项目加载失败
- Ubuntu 13.10 用sogou拼音输入法替换ibus
- (C++)C++类继承中的构造函数和析构函数
- [ATL/WTL]_[初级]_[如何使用GetOpenFileName多选文件-根据文件名长度计算lpstrFile长度]
- [ATL/WTL]_[初级]_[如何使用GetOpenFileName多选文件-根据文件名长度计算lpstrFile长度]
- 程序的调试(堆栈的使用)
- mysql 修改字符集为utf8mb4
- struts2 开发中,一些功能需要在struts.xml中的配置
- Java多线程生命周期