Lucky
TimeLimit: 6000/3000 MS (Java/Others) Memory Limit:65536/65536 K (Java/Others)
Total Submission(s): 1114 Accepted Submission(s): 398
Problem Description
WLD is always very lucky.His secret is a lucky number K.k is a fixed oddnumber. Now he meets a stranger with N numbers:a1,a2,...,aN.The strangerasks him M questions.Eachquestion is like this:Given two ranges [Li,Ri] and [Ui,Vi],you can choosetwo numbers X and Y to make aX+aY=K.The X you can chooseis between Li and Ri and the Y you can chooseis between Ui and Vi.How many pairsof numbers(X,Y) you can choose?
If WLD can answer all the questions correctly,he'll be the luckiest man in theworld.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 luckynumber.K is odd.
The following line contains N integersa1,a2,...,aN(1≤ai≤N).
The following line contains an integer M(1≤M≤30000),the sum of thequestions WLD has to answer.
The following M lines,thei-th line contains 4 numbersLi,Ri,Ui,Vi(1≤Li≤Ri<Ui≤Vi≤N),describing thei-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 wehave two pairs of numbers (1,4) and (2,3).
Goodluck!
我们设f(a,b)为区间[a,b]中满足ax+ay=k的组合数,设我们要求的结果为ans,则根据容斥原理可知ans=f(L1,R2)-f(L1,L2-1)-f(R1+1,R2)+f(R1+1,L2-1),然后就可以用莫队分开算四个区间。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
typedef long long ll;
const int maxn= 30005;
const int mod = 10007;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
#define rush() int T;scanf("%d",&T);while(T--)
int n,m,k;
int a[maxn],type[maxn];
ll num[maxn],cur;
struct node
{
int l,r,id;
ll ans;
} e[maxn*4];
bool cmp1(const node &a,const node &b)
{
if(type[a.l]!=type[b.l]) return type[a.l]<type[b.l];
return a.r<b.r;
}
bool cmp2(const node &a,const node &b)
{
return a.id<b.id;
}
void add(int pos)
{
if(k-a[pos]>0)
{
cur+=num[k-a[pos]];
}
num[a[pos]]++;
}
void del(int pos)
{
num[a[pos]]--;
if(k-a[pos]>0)
cur-=num[k-a[pos]];
}
int main ()
{
while(~scanf("%d",&n))
{
int L1,R1,L2,R2;
scanf ("%d",&k);
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
}
int limit=sqrt(n*1.0);
for(int i=1; i<=n; i++)
{
type[i]=(i-1)/limit;
}
scanf("%d",&m);
int tot=0;
for(int i=0; i<m; i++)
{
scanf("%d%d%d%d",&L1,&R1,&L2,&R2);
node tmp1= {L1,R2,tot,0};
e[tot++]=tmp1;
node tmp2= {L1,L2-1,tot,0};
e[tot++]=tmp2;
node tmp3= {R1+1,R2,tot,0};
e[tot++]=tmp3;
node tmp4= {R1+1,L2-1,tot,0};
e[tot++]=tmp4;
}
sort (e,e+tot,cmp1);
int l=1,r=1;
cur=0;
mst(num,0);
add(1);
for(int i=0; i<tot; i++)
{
if(e[i].l>e[i].r)
{
e[i].ans=0;
continue;
}
while(l>e[i].l)
{
l--;
add(l);
}
while(l<e[i].l)
{
del(l);
l++;
}
while(r>e[i].r)
{
del(r);
r--;
}
while(r<e[i].r)
{
r++;
add(r);
}
e[i].ans = cur;
}
sort (e, e+tot, cmp2);
for(int i=0;i<m;i++)
{
printf ("%I64d\n", e[i*4].ans-e[i*4+1].ans-e[i*4+2].ans+e[i*4+3].ans);
}
}
return 0;
}