Lucky
Problem Description
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!
题意 :
给你你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]);
}
}
}