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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章