Lucky

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

Hint

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

 

题意

  给你你n个数一个k

  m次询问,每次给你两区间

  问你这两个区间 任选两个数a[i] + a[j] = k 的对数

题解:

  这道题需要一些莫队算法的知识 定义记号f(A,B)f(A,B)表示询问区间A,B时的答案 用记号+表示集合的并 利用莫队算法我们可以计算出任意f(A,A)f(A,A)的值

  不妨假设A=[l1,r1],B=[l2,r2],C=[r1+1,l2-1]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)f(A,B)=f(A+B+C,A+B+C)+f(C,C)−f(A+C,A+C)−f(C+B,C+B)

  因此一个询问被拆成四个可以用莫队算法做的询问 总的时间复杂度为O(msqrt(n))O(msqrt(n))

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 3e4+, M = 6e4+, mod = 1e9+, inf = 1e9+;
typedef long long ll; int T,n,a[N],ans[N],belong[N],mp[M * ],k;
struct ss{
int l,r,id,res;
ss () {}
ss (int l,int r,int id,int res) : l(l), r(r), id(id), res(res) {}
}Q[N * ];
bool operator < (ss s1 , ss s2) {
if(belong[s1.l] == belong[s2.l]) return s1.r<s2.r;
else return belong[s1.l] < belong[s2.l];
} int main()
{
while(~scanf("%d",&n)) {
memset(mp,,sizeof(mp));
memset(ans,,sizeof(ans));
scanf("%d",&k);
for(int i = ; i <= n; ++i) scanf("%d",&a[i]);
int q,cnt=;cin>>q;
for(int i = ; i <= q; ++i) {
int l1,r1,l2,r2;
scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
Q[++cnt] = ss (l1,r2,i,);
if(l2->=r1+)Q[++cnt] = ss (r1+,l2-,i,);
Q[++cnt] = ss(r1+,r2,i,-);
Q[++cnt] = ss(l1,l2-,i,-);
}
int t = sqrt(n);
for(int i = ; i <= n; ++i) belong[i] = (i-) / t + ;
sort(Q+,Q+cnt+);
int l = , r = , ret = ;
for(int i = ; i <= cnt; ++i) {
for(;r<Q[i].r;r++) {
ret += mp[k-a[r+]+M];
mp[a[r+]+M]++;
}
for(;l>Q[i].l;l--) {
ret += mp[k-a[l-]+M];
mp[a[l-]+M]++;
}
for(;r>Q[i].r;r--) {
mp[a[r]+M]--;
ret -= mp[k-a[r]+M]; }
for(;l<Q[i].l;l++) {
mp[a[l]+M]--;
ret -= mp[k-a[l]+M]; }
// cout<<Q[i].l<<" "<<Q[i].r<<" ";
//cout<<Q[i].res*ret<<endl;
ans[Q[i].id] += Q[i].res*ret;
}
for(int i = ; i <= q; ++i) {
printf("%d\n",ans[i]);
}
}
}

HDU 5213 Lucky 莫队+容斥的更多相关文章

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

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

  2. HDU 5145 分块 莫队

    给定n个数,q个询问[l,r]区间,每次询问该区间的全排列多少种. 数值都是30000规模 首先考虑计算全排列,由于有同种元素存在,相当于每次在len=r-l+1长度的空格随意放入某种元素即$\bin ...

  3. HDU 5768 Lucky7 (中国剩余定理+容斥)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5768 给你n个同余方程组,然后给你l,r,问你l,r中有多少数%7=0且%ai != bi. 比较明显 ...

  4. hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion

    http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...

  5. HDU 6053 TrickGCD 莫比乌斯函数/容斥/筛法

    题意:给出n个数$a[i]$,每个数可以变成不大于它的数,现问所有数的gcd大于1的方案数.其中$(n,a[i]<=1e5)$ 思路:鉴于a[i]不大,可以想到枚举gcd的值.考虑一个$gcd( ...

  6. hdu 5768 Lucky7 中国剩余定理+容斥+快速乘

    Lucky7 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem D ...

  7. hdu 4336 Card Collector —— Min-Max 容斥

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4336 bzoj 4036 的简单版,Min-Max 容斥即可. 代码如下: #include<cst ...

  8. hdu 4638 Group 莫队算法

    题目链接 很裸的莫队, 就不多说了... #include<bits/stdc++.h> using namespace std; #define pb(x) push_back(x) # ...

  9. HDU 6397 Character Encoding (组合数学 + 容斥)

    题意: 析:首先很容易可以看出来使用FFT是能够做的,但是时间上一定会TLE的,可以使用公式化简,最后能够化简到最简单的模式. 其实考虑使用组合数学,如果这个 xi 没有限制,那么就是求 x1 + x ...

随机推荐

  1. 如何将ADT项目导入Android studio及常見問題

    ADT导出Android studio项目 右键-->ExportAndroid/Generate Gradle build files--> Android studio导入项目 Fil ...

  2. ios CoreData NSManagedObject 生命周期

    用同样的检索条件从context检索出的对象是一个????所以 在主页的3个brand没法释放,在仅仅处理brand的时候???? 和 多个 context无关 我重写了NSManagedObject ...

  3. Effective C++ -----条款18:让接口容易被正确使用,不易被误用

    好的接口很容易被正确使用,不容易被误用.你应该在你IDE所有接口中努力达成这些性质. “促进正确使用”的办法包括接口的一致性,以及与内置类型的行为兼容. “阻止误用"的办法包括建立新类型.限 ...

  4. UIButton修改文字大小问题

    一.问题描述 通过UIButton对象font属性设置文字大小,却发现该属性在2.0.3.0就已经被废弃,ios不建议使用. 图1-1:点出UIButton对象的font属性提示被废弃 图1-2:UI ...

  5. js 中 toString( ) 和valueOf( )

    1.toString()方法:主要用于Array.Boolean.Date.Error.Function.Number等对象转化为字符串形式.日期类的toString()方法返回一个可读的日期和字符串 ...

  6. 一个简单的代码计算行数demo编写

    最近手头的项目基本上已经完结,历经了5个月的开发和迭代,各种的需求调整,想对自己的代码量进行一个客观的计算,于是抽了点时间写下了这个小demo,朋友们有需要的可以看看,很简单. 基本的思想就是:根目录 ...

  7. osg设置相机参数,包括初始位置

    严重注意!!!以下设置必须在viewer.realize();之后,否则不起作用!!!! 设置相机的位置,可以通过CameraManipulator(一般是osgGA::TrackballManipu ...

  8. 定时器(NSTimer)

    iOS中定时器NSTimer的使用 1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget sel ...

  9. object实现小老鼠交互

    直接使用 <p style="text-align: center; "> <object type="application/x-shockwave- ...

  10. SQLServer自定义函数简单演示

    CREATE FUNCTION [ schema_name. ] function_name ( [ { @parameter_name [ AS ][ type_schema_name. ] par ...