Lucky
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 418 Accepted Submission(s): 142
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?
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 a 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.
For each case:
The first line contains an integer N(1≤N≤30000) .
The following line contains a 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.
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
题目分析:
设f(l,r)为在区间中ai+aj=k的对数,设g(l1,r1,l2,r2)表示l1<=i<=r1,l2<=j<=r2的ai+aj=k的对数
根据容斥定理得到
g(l1,r1,l2,r2) = f(l1,r2) - f(l1,l1-1) - f(r1+1,r2) + f(r1+1,l2-1)
那么得到的f(l,r)可以利用莫队进行处理,再每次l或r变换一位时,可以通过hash表获取到的对数修改当前区间的对数,利用分块算法
具体莫队的复杂度是n^1.5
一、i与i+1在同一块内,r单调递增,所以r是O(n)的。由于有n^0.5块,所以这一部分时间复杂度是n^1.5。
二、i与i+1跨越一块,r最多变化n,由于有n^0.5块,所以这一部分时间复杂度是n^1.5
三、i与i+1在同一块内时变化不超过n^0.5,跨越一块也不会超过2*n^0.5,不妨看作是n^0.5。由于有n个数,所以时间复杂度是n^1.5
于是就变成了O(n^1.5)了
莫队算法在无修改的区间操作中十分有效
设f(l,r)为在区间中ai+aj=k的对数,设g(l1,r1,l2,r2)表示l1<=i<=r1,l2<=j<=r2的ai+aj=k的对数
根据容斥定理得到
g(l1,r1,l2,r2) = f(l1,r2) - f(l1,l1-1) - f(r1+1,r2) + f(r1+1,l2-1)
那么得到的f(l,r)可以利用莫队进行处理,再每次l或r变换一位时,可以通过hash表获取到的对数修改当前区间的对数,利用分块算法
具体莫队的复杂度是n^1.5
一、i与i+1在同一块内,r单调递增,所以r是O(n)的。由于有n^0.5块,所以这一部分时间复杂度是n^1.5。
二、i与i+1跨越一块,r最多变化n,由于有n^0.5块,所以这一部分时间复杂度是n^1.5
三、i与i+1在同一块内时变化不超过n^0.5,跨越一块也不会超过2*n^0.5,不妨看作是n^0.5。由于有n个数,所以时间复杂度是n^1.5
于是就变成了O(n^1.5)了
莫队算法在无修改的区间操作中十分有效
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define MAX 30007
using namespace std;
int block;
struct Query
{
int l,r,id,type;
bool operator < ( const Query& a ) const
{
int b1 = l/block , b2 = a.l/block;
return b1 < b2 || ( b1 == b2 && r < a.r );
}
}q[MAX<<2];
int cnt[MAX];
int n,m,k,l,r,u,v;
int a[MAX];
int ans[MAX];
void set ( int id , int l , int r )
{
q[id].l = l;
q[id].r = r;
q[id].id = id/4;
q[id].type = id%4;
}
int main ( )
{
while ( ~scanf ( "%d" , &n ) )
{
block = (int)(sqrt(n+0.5));
scanf ( "%d" , &k );
memset ( cnt , 0 , sizeof ( cnt ) );
memset ( ans , 0 , sizeof ( ans ) );
for ( int i = 1 ; i <= n ; i++ )
scanf ( "%d" , &a[i] );
scanf ( "%d" , &m );
for ( int i = 0 ; i < m ; i++ )
{
scanf ( "%d%d%d%d" , &l , &r , &u , &v );
set ( 4*i , l , v );
set ( 4*i+1 , r+1 , u-1 );
set ( 4*i+2 , l , u-1 );
set ( 4*i+3 , r+1 , v );
}
sort ( q , q+4*m );
int cur = 0 , l = 0 , r = -1;
for ( int i = 0 ; i < 4*m ; i++ )
{
while ( l < q[i].l )
{
cnt[a[l]]--;
if ( k > a[l] && k - a[l] <= n )
cur -= cnt[k-a[l]];
l++;
}
while ( r > q[i].r )
{
cnt[a[r]]--;
if ( k > a[r] && k - a[r] <= n )
cur -= cnt[k-a[r]];
r--;
}
while ( l > q[i].l )
{
l--;
if ( k > a[l] && k-a[l] <= n )
cur += cnt[k-a[l]];
cnt[a[l]]++;
}
while ( r < q[i].r )
{
r++;
if ( k > a[r] && k-a[r] <= n )
cur += cnt[k-a[r]];
cnt[a[r]]++;
}
if ( q[i].type < 2 ) ans[q[i].id] += cur;
else ans[q[i].id] -= cur;
}
for ( int i = 0 ; i < m ; i++ )
printf ( "%d\n" , ans[i] );
}
}