Gym101138D Strange Queries 莫队前缀和容斥

Posted itst

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gym101138D Strange Queries 莫队前缀和容斥相关的知识,希望对你有一定的参考价值。

传送门


THUWC2019D1T1撞题可还行

以前有些人做过还问过我,但是我没有珍惜,直到进入考场才追悔莫及……

(que_{i,j})表示询问((1,i,1,j))的答案,那么询问((a,b,c,d)=que_{b,d} - que_{a-1 , d} - que_{b , c - 1} + que_{a - 1 , c - 1})

把一个询问拆成(4)个询问,然后对这(4)个询问莫队就可以了

不知道怎么回事THUWC上想到了莫队想到了前缀和想到了容斥就是没想到莫队+前缀和+容斥……

#include<bits/stdc++.h>
//This code is written by Itst
using namespace std;

inline int read(){
    int a = 0;
    char c = getchar();
    bool f = 0;
    while(!isdigit(c) && c != EOF){
        if(c == ‘-‘)
            f = 1;
        c = getchar();
    }
    if(c == EOF)
        exit(0);
    while(isdigit(c)){
        a = a * 10 + c - 48;
        c = getchar();
    }
    return f ? -a : a;
}

const int MAXN = 5e4 + 3;
int N , M , T , cnt , cntQ;
int arr[MAXN] , times[MAXN][2];
long long ans[MAXN] , cur;
struct query{
    int p1 , p2 , ind , flg;
    query(int a = 0 , int b = 0 , int c = 0 , int d = 0):p1(a) , p2(b) , ind(c) , flg(d){}
    bool operator <(const query b)const{
        return p1 / T == b.p1 / T ? p2 < b.p2 : p1 < b.p1;
    }
}que[MAXN << 2];

inline void add(int a , int ind){
    cur += times[a][ind ^ 1];
    ++times[a][ind];
}

inline void del(int a , int ind){
    cur -= times[a][ind ^ 1];
    --times[a][ind];
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("in","r",stdin);
    freopen("out","w",stdout);
#endif
    N = read();
    T = sqrt(N);
    for(int i = 1 ; i <= N ; ++i)
        arr[i] = read();
    M = read();
    for(int i = 1 ; i <= M ; ++i){
        int a = read() , b = read() , c = read() , d = read();
        que[++cntQ] = query(b , d , i , 1);
        if(a - 1)
            que[++cntQ] = query(a - 1 , d , i , -1);
        if(b - 1)
            que[++cntQ] = query(b , c - 1 , i , -1);
        if(a - 1 && b - 1)
            que[++cntQ] = query(a - 1 , c - 1 , i , 1);
    }
    sort(que + 1 , que + cntQ + 1);
    int l1 = 0 , l2 = 0;
    for(int i = 1 ; i <= cntQ ; ++i){
        while(l1 < que[i].p1)
            add(arr[++l1] , 0);
        while(l1 > que[i].p1)
            del(arr[l1--] , 0);
        while(l2 < que[i].p2)
            add(arr[++l2] , 1);
        while(l2 > que[i].p2)
            del(arr[l2--] , 1);
        ans[que[i].ind] += que[i].flg * cur;
    }
    for(int i = 1 ; i <= M ; ++i)
        cout << ans[i] << ‘
‘;
    return 0;
}

以上是关于Gym101138D Strange Queries 莫队前缀和容斥的主要内容,如果未能解决你的问题,请参考以下文章

Strange Memory Gym - 102832F

css css:media-querie-orientation

css css:min-max-media-querie

css Boostrap 3媒体查询 - https://scotch.io/quick-tips/default-sizes-for-twitter-bootstraps-media-querie

UVA10519 Really Strange大数+递推

国赛 strange_int