关闭

HDU 5714 (离散化 树状数组)

211人阅读 评论(0) 收藏 举报
分类:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define maxn 21111

int a[maxn][3], b[maxn][3];
int cnt1, cnt2;
int c[maxn], x[maxn], Max[maxn];
int n;
int cnt;

int lowbit (int x) {
    return x&(-x);
}

void add (int pos, int num, bool op) {
    for (int i = pos; i <= cnt; i += lowbit (i)) {
        if (!op) c[i] += num;
        else x[i] += num;
    }
}

int sum (int pos, int op) {
    int ans = 0;
    for (int i = pos; i > 0; i -= lowbit (i)) {
        if (!op) ans += c[i];
        else
            ans += x[i];
    }
    return ans;
}
vector <int> num, l1, r1, l2, r2;
map <int, int> gg;
void init () {
    gg.clear ();
    sort (num.begin (), num.end ());
    int Max = num.size ();
    cnt = 1;
    for (int i = 0; i < Max; i++) {
        if (!i || num[i] != num[i-1]) {
            gg[num[i]] = cnt++;
        }
    }
}

int solve () {
    num.clear (); l1.clear (), l2.clear(), r1.clear (), r2.clear ();
    memset (c, 0, sizeof c);
    for (int i = 0; i < cnt1; i++) {
        int r = a[i][0]+a[i][2], l = a[i][1]-a[i][2];
        if (r >= l) {
            l1.push_back (l);
            r1.push_back (r);
            num.push_back (l);
            num.push_back (r);
        }
    }
    memset (x, 0, sizeof x);
    for (int i = 0; i < cnt2; i++) {
        int r = b[i][0]+b[i][2], l = b[i][1]-b[i][2];
        if (r >= l) {
            l2.push_back (l);
            r2.push_back (r);
            num.push_back (l);
            num.push_back (r);
        }
    }
    init ();
    for (int i = 0; i < l1.size (); i++) {
        int l = gg[l1[i]], r = gg[r1[i]];
        add (l, 1, 0);
        add (r+1, -1 , 0);
    }
    for (int i = 0; i < l2.size (); i++) {
        int l = gg[l2[i]], r = gg[r2[i]];
        add (l, 1, 1);
        add (r+1, -1, 1);
    }
    Max[1] = x[1];
    for (int i = 2; i <= cnt; i++) {
        int cur = sum (i, 1);
        Max[i] = max (Max[i-1], cur);
    }
    int ans = 0;
    for (int i = 1; i <= cnt; i++) {
        ans = max (ans, sum (i, 0) + Max[i]);
    }
    return ans;
}

int main () {
    //freopen ("in.txt", "r", stdin);
    int t, kase = 0;
    scanf ("%d", &t);
    while (t--) {
        scanf ("%d", &n);
        printf ("Case #%d:\n", ++kase);
        cnt1 = 0, cnt2 = 0;
        for (int i = 1; i <= n; i++) {
            int x, y, d, op;
            scanf ("%d%d%d%d", &x, &y, &d, &op);
            if (op == -1) {
                a[cnt1][0] = x, a[cnt1][1] = y, a[cnt1][2] = d;
                cnt1++;
            }
            else {
                b[cnt2][0] = x, b[cnt2][1] = y, b[cnt2][2] = d;
                cnt2++;
            }
        }
        int ans = solve ();
        printf ("%d\n", ans);
    }
    return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:92694次
    • 积分:5358
    • 等级:
    • 排名:第5022名
    • 原创:463篇
    • 转载:0篇
    • 译文:0篇
    • 评论:12条
    最新评论