HDU - 5741 Helter Skelter 扫描线 + 树状数组

强颜欢笑 提交于 2019-11-29 12:00:35

HDU - 5741

我们枚举段的起点和终点, 那么每一种情况0的范围是[lx, rx], 1的出现范围是[ly, ry], 可以在二维平面上用矩形表示。

然后问题就变成了询问点有没有被至少一个矩形覆盖, 扫描线 + 树状数组就可以了。

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 3e6 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = (int)1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}

struct Line {
    LL x, y1, y2;
    bool operator < (const Line &rhs) const {
        return x < rhs.x;
    }
    int w;
};

struct Qus {
    bool operator < (const Qus &rhs) const {
        return x < rhs.x;
    }
    LL x, y, id;
};

LL hs[N], hs_cnt;

struct Bit {
    int a[N];
    void init() {
        for(int i = 1; i <= hs_cnt; i++) {
            a[i] = 0;
        }
    }
    inline void modify(int x, int v) {
        for(int i = x; i <= hs_cnt; i += i & -i) {
            a[i] += v;
        }
    }
    inline int sum(int x) {
        int ans = 0;
        for(int i = x; i; i -= i & -i) {
            ans += a[i];
        }
        return ans;
    }
};

int n, m, mx[2], a[N], ans[N];
LL sum[2][N];

int L_cnt;
Line L[N];

int Q_cnt;
Qus Q[N];

Bit bit;

void init() {
    mx[0] = mx[1] = 0;
    L_cnt = Q_cnt = hs_cnt = 0;
}

int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &n, &m);
        init();
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            sum[0][i] = sum[0][i - 1];
            sum[1][i] = sum[1][i - 1];
            sum[!(i & 1)][i] += a[i];
            chkmax(mx[!(i & 1)], a[i]);
        }
        for(int i = 1; i <= n; i++) {
            for(int j = i + 1; j <= n; j++) {
                LL lx = 0, ly = 0, rx = 0, ry = 0;
                if(i + 1 < j) {
                    lx = sum[0][j - 1] - sum[0][i];
                    ly = sum[1][j - 1] - sum[1][i];
                }
                rx = lx; ry = ly;
                if(i & 1) rx += a[i];
                else ry += a[i];
                if(j & 1) rx += a[j];
                else ry += a[j];
                L[++L_cnt] = Line{lx, ly, ry, 1};
                L[++L_cnt] = Line{rx + 1, ly, ry, -1};
                hs[++hs_cnt] = ly;
                hs[++hs_cnt] = ry;
            }
        }
        for(int i = 1; i <= m; i++) {
            LL x, y; scanf("%lld%lld", &x, &y);
            if(!x) {
                ans[i] = mx[1] >= y;
            }
            else if(!y) {
                ans[i] = mx[0] >= x;
            }
            else {
                hs[++hs_cnt] = y;
                Q[++Q_cnt] = Qus{x, y, i};
            }
        }
        sort(hs + 1, hs + 1 + hs_cnt);
        hs_cnt = unique(hs + 1, hs + 1 + hs_cnt) - hs - 1;
        bit.init();
        for(int i = 1; i <= L_cnt; i++) {
            L[i].y1 = lower_bound(hs + 1, hs + 1 + hs_cnt, L[i].y1) - hs;
            L[i].y2 = lower_bound(hs + 1, hs + 1 + hs_cnt, L[i].y2) - hs;
        }
        for(int i = 1; i <= Q_cnt; i++) {
            Q[i].y = lower_bound(hs + 1, hs + 1 + hs_cnt, Q[i].y) - hs;
        }
        sort(L + 1, L + 1 + L_cnt);
        sort(Q + 1, Q + 1 + Q_cnt);
        for(int i = 1, j = 1; i <= Q_cnt; i++) {
            while(j <= L_cnt && L[j].x <= Q[i].x) {
                bit.modify(L[j].y1, L[j].w);
                bit.modify(L[j].y2 + 1, -L[j].w);
                j++;
            }
            ans[Q[i].id] = bit.sum(Q[i].y);
        }
        for(int i = 1;  i <= m; i++) {
            putchar(ans[i] ? '1' : '0');
        }
        puts("");
    }
    return 0;
}

/*
*/

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!