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?
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 integerN(1≤N≤30000) .
The following line contains an integerK(2≤K≤2∗N) ,WLD's lucky number.K is odd.
The following line containsN integers a1,a2,...,aN(1≤ai≤N) .
The following line contains an integerM(1≤M≤30000) ,the sum of the questions WLD has to answer.
The followingM 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.
For each case:
The first line contains an integer
The following line contains an integer
The following line contains
The following line contains an integer
The following
Output
For each case:
Print the total of pairs WLD can choose for each question.
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
2Hinta1+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
- Lucky - HDU 5213 莫队算法
- hdu 5213 Lucky && 莫队算法的理解
- HDU 5213 Lucky(容斥原理+莫队算法)
- hdu 5213 Lucky(莫队算法分块+容斥定理)
- 【HDU】5213 Lucky 【分块(在线算法)】
- hdu3113 Lucky 莫队算法
- 【莫队算法】[HDU5213]Lucky
- hdu5213 Lucky 莫队算法+容斥
- hdu 5213 Lucky(容斥+莫队)
- HDU 5213 Lucky [莫队+容斥]
- [HDU 5213]Lucky:莫队+容斥原理
- HDU 5213 Lucky(莫队+容斥)
- HDU 5213 Lucky 容斥+莫队
- hdu 5213 Lucky (莫队+容斥)
- hdu 3346 Lucky Number
- hdu 3346 Lucky Number
- HDU 3346 Lucky Number
- HDU 4937 Lucky Number
- DevExpress DocumentManager 操作类
- 五大常用算法之四:回溯算法
- Html5多浏览器支持情况
- 五大常用算法之五:分支限界法
- Android中Paint的相关用法
- Lucky - HDU 5213 莫队算法
- 产品经理常犯的10个错误
- block 实现原理和相关内存管理详解
- Oracle 实例性能分析与优化之AWR学习总结
- Android中项目中各个文件夹的含义和用途详解
- jquery 初步(五) 可见和隐藏过滤器
- Effective C++ 条款02解读: 尽量以const, enum, inline替换#define
- 不要执行用utf-8編碼,并且內容包含非拉丁字符的批处理脚本
- Android UI之自定义——类似iOS的Tabbar