hdu 5213 Lucky(莫队算法分块+容斥定理)

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?
 

Input
There are multiple cases.(At MOST 5 )

For each case:

The first line contains an integer N(1N30000) .

The following line contains a integer K(2K2N) ,WLD's lucky number.K is odd.

The following line contains N integers a1,a2,...,aN(1aiN) .

The following line contains an integer M(1M30000) ,the sum of the questions WLD has to answer.

The following M lines,the i-th line contains 4 numbers Li,Ri,Ui,Vi(1LiRi<UiViN) ,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
题目分析:
设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] );
    }
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值