题目:http://acm.hdu.edu.cn/showproblem.php?pid=5213

Lucky

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

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 Mquestions.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

Hint

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

 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  5659 5658 5657 5656 5655 
 
题意:给你一个a数列,再给出一些询问,每次询问给两个区间,分别为[l,r]和[u,v],且1<=l<=r<u<=v<=n,让你从[l,r]中找一个a[i],在[u,v]中找一个a[j],使得a[i]+a[j]=K,问有多少对。
题解:
好几天没发题解了。。。
来个莫队压压惊。。。
莫队+容斥
莫队很好想的,主要是如何用容斥。
我们把每组询问的两个区间写出来。
         l-----r--------u-----v
         |<A>|
                            |<B>|
                |<--C-->|
然后定义f(x,y)为第一个数i在x区间,第二个数j在y区间的a[i]+a[j]=K的方案数。
我们要求的为f(A,B)=f(A+B+C,A+B+C)-f(A+C,A+C)-f(B+C,B+C)+f(C,C)
然后就可以用莫队做了。
这里有些小技巧:在加区间的时候,我们可以把区间的 两端点 和 当前区间的值是加还是减 记录下来,这样比较简单。
另外这道题的数组大小好离奇。。。
具体看程序:
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define MAXN 30010
  4. #define MAXM 30010
  5. struct node
  6. {
  7. int l,r,id,fh;
  8. }q[MAXM*];
  9. int a[MAXN],pos[MAXN],sum[MAXN*],N,ans[MAXM*];
  10. int read()
  11. {
  12. int s=,fh=;char ch=getchar();
  13. while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
  14. while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
  15. return s*fh;
  16. }
  17. void Add(int ll,int rr,int ii,int ff){q[++N].l=ll;q[N].r=rr;q[N].id=ii;q[N].fh=ff;}
  18. bool cmp(node aa,node bb)
  19. {
  20. if(pos[aa.l]==pos[bb.l])return aa.r<bb.r;
  21. return aa.l<bb.l;
  22. }
  23. int main()
  24. {
  25. int n,k,i,m,block,tot,L,R,U,V;
  26. while(scanf("%d",&n)!=EOF)
  27. {
  28. k=read();
  29. for(i=;i<=n;i++)a[i]=read();
  30. m=read();
  31. N=;
  32. for(i=;i<=m;i++)
  33. {
  34. L=read();R=read();U=read();V=read();
  35. Add(L,V,i,);Add(L,U-,i,-);Add(R+,V,i,-);Add(R+,U-,i,);
  36. }
  37. block=(int)sqrt(n);
  38. for(i=;i<=n;i++)pos[i]=(int)(i-)/block+;
  39. sort(q+,q+N+,cmp);
  40. memset(ans,,sizeof(ans));
  41. L=;R=;
  42. tot=;//当前区间有多少对a[i]+a[j]=k.
  43. memset(sum,,sizeof(sum));//当前区间数字为i的有sum[i]个.
  44. for(i=;i<=N;i++)
  45. {
  46. while(L<q[i].l)
  47. {
  48. sum[a[L]]--;
  49. tot-=sum[k-a[L]];
  50. //sum[a[L]]--;
  51. //if(k==a[L]*2)tot++;
  52. L++;
  53. }
  54. while(L>q[i].l)
  55. {
  56. L--;
  57. tot+=sum[k-a[L]];
  58. sum[a[L]]++;
  59. }
  60. while(R<q[i].r)
  61. {
  62. R++;
  63. tot+=sum[k-a[R]];
  64. sum[a[R]]++;
  65. }
  66. while(R>q[i].r)
  67. {
  68. sum[a[R]]--;
  69. tot-=sum[k-a[R]];
  70. //sum[a[R]]--;
  71. //if(k==a[R]*2)tot++;
  72. R--;
  73. }
  74. ans[q[i].id]+=q[i].fh*tot;
  75. }
  76. for(i=;i<=m;i++)printf("%d\n",ans[i]);
  77. }
  78. fclose(stdin);
  79. fclose(stdout);
  80. return ;
  81. }

Hdu 5213-Lucky 莫队,容斥原理,分块的更多相关文章

  1. HDU 5213 Lucky 莫队+容斥

    Lucky Problem Description WLD is always very lucky.His secret is a lucky number K.k is a fixed odd n ...

  2. Lucky HDU - 5213 (莫队,容斥)

    WLD is always very lucky.His secret is a lucky number . is a fixed odd number. Now he meets a strang ...

  3. 【bzoj3585/bzoj3339】mex/Rmq Problem 莫队算法+分块

    原文地址:http://www.cnblogs.com/GXZlegend/p/6805283.html 题目描述 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区间内最小没 ...

  4. 【bzoj3809/bzoj3236】Gty的二逼妹子序列/[Ahoi2013]作业 莫队算法+分块

    原文地址:http://www.cnblogs.com/GXZlegend/p/6805252.html bzoj3809 题目描述 Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了 ...

  5. 【BZOJ 3735】苹果树 树上莫队(树分块+离线莫队+鬼畜的压行)

    2016-05-09 UPD:学习了新的DFS序列分块,然后发现这个东西是战术核导弹?反正比下面的树分块不知道要快到哪里去了 #include<cmath> #include<cst ...

  6. 【BZOJ】2038: [2009国家集训队]小Z的袜子(hose)(组合计数+概率+莫队算法+分块)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2038 学了下莫队,挺神的orzzzz 首先推公式的话很简单吧... 看的题解是从http://for ...

  7. [BZOJ 3236] [Ahoi2013] 作业 && [BZOJ 3809] 【莫队(+分块)】

    题目链接: BZOJ - 3236   BZOJ - 3809 算法一:莫队 首先,单纯的莫队算法是很好想的,就是用普通的第一关键字为 l 所在块,第二关键字为 r 的莫队. 这样每次端点移动添加或删 ...

  8. XOR and Favorite Number(莫队算法+分块)

    E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  9. [SNOI2017]一个简单的询问【莫队+容斥原理】

    题目大意 给你一个数列,让你求两个区间内各个数出现次数的乘积的和. 分析 数据范围告诉我们可以用莫队过. 我并不知道什么曼哈顿什么乱七八糟的东西,但是我们可以用容斥原理将这个式子展开来. \[\sum ...

随机推荐

  1. linux管理文件系统指令

    就一个基本的linux系统而言,其计算机硬盘只能有三个分区:一个交换分区(用于处理物理内存存不下的信息),一个包含引导转载程序的内核的启动分区,一个根文件系统分区,后两个常采用 ext3文件系统 与e ...

  2. 完整的 AJAX 写法(支持多浏览器)

    代码如下: <script type="text/javascript"> var xmlhttp; function Submit() { //1.创建 XMLHtt ...

  3. 【DP_背包专题】 背包九讲

    这段时间看了<背包九讲>,在HUST VJUDGE上找到了一个题单,挑选了其中16道题集中做了下,选题全部是HDU上的题,大多是简单题.目前做了点小总结,大概提了下每道题的思路重点部分,希 ...

  4. Ubuntu下gcc及g++环境配置

    直接在命令行中输入以下命令即可. sudo apt-get install build-essential 安装完成后输入 gcc 和 g++ 进行确认.

  5. ubuntu mint 15 编译安装PHP开发环境

    php 5.3.5(download zip) httpd 2.2.24(download zip) mysql: apt-get install mysql step 1: install mysq ...

  6. 网络安全设备Bypass功能介绍及分析

    from:http://netsecurity.51cto.com/art/200910/159948.htm 网络安全平台厂商往往需要用到一项比较特殊的技术,那就是Bypass,那么到底什么是Byp ...

  7. Lambda Expression In Java

     题记在阅读JDK源码java.util.Collections的时候在UnmodifiableCollection类中看到了这么一段代码: public void forEach(Consumer& ...

  8. Quartz 之 windowService

    (一)创建服务 QuarzService using System.ServiceProcess;using System.Text; using Quartz;using Quartz.Impl; ...

  9. H5小内容(一)

    HTML5目前最新的规范(标准)是2014年10月推出   2005年左右出现HTML5版本(非标准)     W3C组织(两个组织定义H5规范)   学习(研究)HTML5是学习未来(将来主流)   ...

  10. PHP源代码分析(第一章):Zend HashTable详解【转】

    转载于http://www.phppan.com/2009/12/zend-hashtable/ 在PHP的Zend引擎中,有一个数据结构非常重要,它无处不在,是PHP数据存储的核心,各种常量.变量. ...