zoukankan      html  css  js  c++  java
  • HDU 5213 Lucky 莫队+容斥

    Lucky


    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 an 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
     
    Sample Output
     
    2
    Hint
    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,l21

      容易知道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+5, M = 6e4+5, mod = 1e9+7, inf = 1e9+1000;
    typedef long long ll;
    
    int T,n,a[N],ans[N],belong[N],mp[M * 10],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 * 4];
    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,0,sizeof(mp));
            memset(ans,0,sizeof(ans));
            scanf("%d",&k);
            for(int i = 1; i <= n; ++i) scanf("%d",&a[i]);
            int q,cnt=0;cin>>q;
            for(int i = 1; i <= q; ++i) {
                int l1,r1,l2,r2;
                scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
                Q[++cnt] = ss (l1,r2,i,1);
                if(l2-1>=r1+1)Q[++cnt] = ss (r1+1,l2-1,i,1);
                Q[++cnt] = ss(r1+1,r2,i,-1);
                Q[++cnt] = ss(l1,l2-1,i,-1);
            }
            int t = sqrt(n);
            for(int i = 1; i <= n; ++i) belong[i] = (i-1) / t + 1;
            sort(Q+1,Q+cnt+1);
            int l = 1, r = 0, ret = 0;
            for(int i = 1; i <= cnt; ++i) {
                for(;r<Q[i].r;r++) {
                    ret += mp[k-a[r+1]+M];
                    mp[a[r+1]+M]++;
                }
                for(;l>Q[i].l;l--) {
                    ret += mp[k-a[l-1]+M];
                    mp[a[l-1]+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 = 1; i <= q; ++i) {
                printf("%d
    ",ans[i]);
            }
        }
    }
  • 相关阅读:
    Dynamic attention in tensorflow
    Dynamic seq2seq in tensorflow
    Tensorflow Seq2seq attention decode解析
    zz图像卷积与滤波的一些知识点
    Android SDK更新失败对策
    高维数据降维 国家自然科学基金项目 2009-2013 NSFC Dimensionality Reduction
    近期深度学习论文汇总
    PHP远程连接mysql报错处理办法
    zz 启动Matlab提示Microsoft Visual C++ 2005 Redistributable存在问题问题
    `fw服务端非完整` 工程开发初期的工作
  • 原文地址:https://www.cnblogs.com/zxhl/p/5717753.html
Copyright © 2011-2022 走看看