Hdu 5213-Lucky 莫队,容斥原理,分块
题目: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
If WLD can answer all the questions correctly,he'll be the luckiest man in the world.Can you help him?
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.
Print the total of pairs WLD can choose for each question.
3
1 2 1 2 3
1
1 2 3 5
a1+a4=a2+a3=3=K.
So we have two pairs of numbers (1,4) and (2,3).
Good luck!
#include<bits/stdc++.h>
using namespace std;
#define MAXN 30010
#define MAXM 30010
struct node
{
int l,r,id,fh;
}q[MAXM*];
int a[MAXN],pos[MAXN],sum[MAXN*],N,ans[MAXM*];
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
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;}
bool cmp(node aa,node bb)
{
if(pos[aa.l]==pos[bb.l])return aa.r<bb.r;
return aa.l<bb.l;
}
int main()
{
int n,k,i,m,block,tot,L,R,U,V;
while(scanf("%d",&n)!=EOF)
{
k=read();
for(i=;i<=n;i++)a[i]=read();
m=read();
N=;
for(i=;i<=m;i++)
{
L=read();R=read();U=read();V=read();
Add(L,V,i,);Add(L,U-,i,-);Add(R+,V,i,-);Add(R+,U-,i,);
}
block=(int)sqrt(n);
for(i=;i<=n;i++)pos[i]=(int)(i-)/block+;
sort(q+,q+N+,cmp);
memset(ans,,sizeof(ans));
L=;R=;
tot=;//当前区间有多少对a[i]+a[j]=k.
memset(sum,,sizeof(sum));//当前区间数字为i的有sum[i]个.
for(i=;i<=N;i++)
{
while(L<q[i].l)
{
sum[a[L]]--;
tot-=sum[k-a[L]];
//sum[a[L]]--;
//if(k==a[L]*2)tot++;
L++;
}
while(L>q[i].l)
{
L--;
tot+=sum[k-a[L]];
sum[a[L]]++;
}
while(R<q[i].r)
{
R++;
tot+=sum[k-a[R]];
sum[a[R]]++;
}
while(R>q[i].r)
{
sum[a[R]]--;
tot-=sum[k-a[R]];
//sum[a[R]]--;
//if(k==a[R]*2)tot++;
R--;
}
ans[q[i].id]+=q[i].fh*tot;
}
for(i=;i<=m;i++)printf("%d\n",ans[i]);
}
fclose(stdin);
fclose(stdout);
return ;
}
Hdu 5213-Lucky 莫队,容斥原理,分块的更多相关文章
- HDU 5213 Lucky 莫队+容斥
Lucky Problem Description WLD is always very lucky.His secret is a lucky number K.k is a fixed odd n ...
- Lucky HDU - 5213 (莫队,容斥)
WLD is always very lucky.His secret is a lucky number . is a fixed odd number. Now he meets a strang ...
- 【bzoj3585/bzoj3339】mex/Rmq Problem 莫队算法+分块
原文地址:http://www.cnblogs.com/GXZlegend/p/6805283.html 题目描述 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区间内最小没 ...
- 【bzoj3809/bzoj3236】Gty的二逼妹子序列/[Ahoi2013]作业 莫队算法+分块
原文地址:http://www.cnblogs.com/GXZlegend/p/6805252.html bzoj3809 题目描述 Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了 ...
- 【BZOJ 3735】苹果树 树上莫队(树分块+离线莫队+鬼畜的压行)
2016-05-09 UPD:学习了新的DFS序列分块,然后发现这个东西是战术核导弹?反正比下面的树分块不知道要快到哪里去了 #include<cmath> #include<cst ...
- 【BZOJ】2038: [2009国家集训队]小Z的袜子(hose)(组合计数+概率+莫队算法+分块)
http://www.lydsy.com/JudgeOnline/problem.php?id=2038 学了下莫队,挺神的orzzzz 首先推公式的话很简单吧... 看的题解是从http://for ...
- [BZOJ 3236] [Ahoi2013] 作业 && [BZOJ 3809] 【莫队(+分块)】
题目链接: BZOJ - 3236 BZOJ - 3809 算法一:莫队 首先,单纯的莫队算法是很好想的,就是用普通的第一关键字为 l 所在块,第二关键字为 r 的莫队. 这样每次端点移动添加或删 ...
- XOR and Favorite Number(莫队算法+分块)
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- [SNOI2017]一个简单的询问【莫队+容斥原理】
题目大意 给你一个数列,让你求两个区间内各个数出现次数的乘积的和. 分析 数据范围告诉我们可以用莫队过. 我并不知道什么曼哈顿什么乱七八糟的东西,但是我们可以用容斥原理将这个式子展开来. \[\sum ...
随机推荐
- MyEclipse 搭建webservice (axis1.4)
0 引言 以前都是做javaweb的 最近因工作需要 接触了webservice 关于什么事webservice,与web的区别,soap,跟http的区别,asix1和asix2的区别,为什么不用 ...
- SVG
目前SVG在国内的使用并不常见,并且关于svg的相关js库也不多,这里指出两款svg的库Snap.svg和svg.js,Snap.svg张鑫旭的博客上有关于他的使用APi http://www.zha ...
- 解决修改SQL SERVER 默认1433端口 访问出错的问题;
1. 如何修改 数据库默认的 1433端口: SQL SERVER 配置管理器中 SQLSERVER 网络配置 xxx数据实例的协议中的 TCP/IP 中 默认端口 都修改为 自己定义的端口 例如 ...
- php文件锁解决少量并发问题
阻塞(等待)模式: <?php $fp = fopen("lock.txt", "r"); if(flock($fp,LOCK_EX)) { //..处理 ...
- struts2整合jfreechart
需要的包: struts2-jfreechart-plugin-2.2.1.1.jar jfreechart-1.0.13.jar jcommon-1.0.17.jar 前台jsp页面中可以使用ifr ...
- python分割sql文件
之前用joomla帮一学校做了个网站,然后要部署到他们到服务器上,他们只提供了sftp和phpmyadmin的账号,上传网站文件倒是挺顺利的,但后来用phpmyadmin导入mysql数据就遇到问题了 ...
- 查看Oracle表空间使用情况与增大表空间
1,查看表空间使用情况 SELECT D.TABLESPACE_NAME, SPACE || 'M' "SUM_SPACE(M)", BLOCKS "SUM_BLOCKS ...
- sqlsever2008数据库的备份与还原
本文数据库的名称为ProjectControl public static SqlConnection conn = new SqlConnection("server=(local);u ...
- Axure草记
页面控件和DataSet绑定,DataSet和输入控件绑定(通过临时变量) 双击Repeater进入之后,你会发现下面已经默认添加了3行,这代表着,每增加一行将会重复3遍: Repeater可以只是部 ...
- 微软CSS面试全记录
先是会有一轮简单的电话技术面试,聊的比较随意,什么都会问,跟职位相关的都有.然后会发一些材料说是要学习,是windows内存管理相关的东西. 完了就是一轮oral test,和技术没有任何关系,问问为 ...