hdu 5213 Lucky(容斥+莫队)

题意:给2个区间,从2个区间内各取一个数字,求a_i+a_j = k的对数。

做法:假设区间范围是[x1,y1]和[x2,y2],我们设f(a,b)为区间a,b中等于k的对数总数,那么答案就是f(x1,y2)-f(x1,x2-1)-f(y1+1,x2)+f(y1+1,x2-1)。那么该如何求f(a,b)?还记得小z的袜子么,我们只需要改变下思路对于a来说求k-a的个数即可。。

PS:第一次离4题如此之近。。结果写完时间刚好到了(虽然后来发觉没判k-a是否大于0)。还是太弱了。。

AC代码:

//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll __int64
#define ull unsigned long long
#define eps 1e-8
#define NMAX 1000000000
#define MOD 10007
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
template<class T>
inline void scan_d(T &ret)
{
    char c;
    int flag = 0;
    ret=0;
    while(((c=getchar())<'0'||c>'9')&&c!='-');
    if(c == '-')
    {
        flag = 1;
        c = getchar();
    }
    while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
    if(flag) ret = -ret;
}
template<class T> inline T Max(T a, T b){ return a > b ? a : b; }
template<class T> inline T Min(T a, T b){ return a < b ? a : b; }
const int maxn = 30000+10;
struct node
{
    int x,y,block,id,pos;
    bool operator < (const node &t) const
    {
        return block == t.block ? y < t.y : block < t.block;
    }
}no[maxn*4];


int a[maxn];

int as[30005][4];
int num[30005];
int main()
{
#ifdef GLQ
    freopen("input.txt","r",stdin);
//    freopen("o.txt","w",stdout);
#endif
    int n,k;
    while(~scanf("%d",&n))
    {
        memset(num,0,sizeof(num));
        scanf("%d",&k);
//        int kuai = 200;
        int kuai = sqrt(n);
        if(kuai*kuai < n) kuai++;
        for(int i = 1; i <= n; i++)
            scanf("%d",&a[i]);
        int q,nct = 0;
        scanf("%d",&q);
        for(int i = 1; i <= q; i++)
        {
            int x1,y1,x2,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            no[nct].x = x1; no[nct].y = y2;
            no[nct].id = i; no[nct].pos = 0;
            nct++;
            no[nct].x = x1; no[nct].y = x2-1;
            no[nct].id = i; no[nct].pos = 1;
            nct++;
            no[nct].x = y1+1; no[nct].y = y2;
            no[nct].id = i; no[nct].pos = 2;
            nct++;
            no[nct].x = y1+1; no[nct].y = x2-1;
            no[nct].id = i; no[nct].pos = 3;
            nct++;
        }
        memset(as,0,sizeof(as));
        for(int i = 0; i < nct; i++)
        {
            no[i].block = no[i].x/kuai;
        }
        sort(no,no+nct);
        int L = 1,R = 0,ans = 0;
        for(int i = 0; i < nct; i++)
        {
            if(no[i].x > no[i].y)
            {
                as[no[i].id][no[i].pos] = 0;
                continue;
            }
            while(R < no[i].y)
            {
                R++;
                if(k > a[R] && k-a[R] <= 30000) ans += num[k-a[R]];
                num[a[R]]++;
            }
            while(R > no[i].y)
            {
                if(k > a[R] && k-a[R] <= 30000) ans -= num[k-a[R]];
                num[a[R]]--;
                R--;
            }
            while(L < no[i].x)
            {
                if(k > a[L] && k-a[L] <= 30000) ans -= num[k-a[L]];
                num[a[L]]--;
                L++;
            }
            while(L > no[i].x)
            {
                L--;
                if(k > a[L] && k-a[L] <= 30000) ans += num[k-a[L]];
                num[a[L]]++;
            }
            as[no[i].id][no[i].pos] = ans;
//            cout<<no[i].x<<" "<<no[i].y<<" "<<ans<<endl;
        }
        for(int i = 1; i <= q; i++)
        {
            int tmp = as[i][0]-as[i][1]-as[i][2]+as[i][3];
            printf("%d\n",tmp);
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值