HDU 5372 Segment Game (树状数组)

题意是指第i此插入操作,插入一条长度为i的线段,左端点在b[i],删除某一条线段,问每次插入操作时,被当前线段完全覆盖的线段的条数。

题解:对于新插入的线段,查询有多少个线段左端点大于等于该线段的左端点。 再查询有多少个线段的右端点大于该线段右端点, 两者之差就是答案。用两个树状数组搞定。时间复杂度nlogn

 

由于坐标范围很大,需要离散。

 

#pragma comment(linker, "/STACK:1677721600")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <bitset>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstdarg>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define root 1, 1, n
#define lc (k << 1)
#define rc (k << 1 | 1)
#define middle ((L + R) >> 1)
#define lson k<<1, L, (L + R)>>1
#define rson k<<1|1,  ((L + R)>>1) + 1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define dec(i, a, b) for(int i = a; i >= b; i --)

//typedef __int64 LL;
//typedef long long LL;
typedef pair<int, int> Pair;
const int MAXN = 200000 + 100;
const int MAXM = 110000;
const double eps = 1e-10;
//LL MOD = 1000000007;

struct Operator {
    int type, lb;//所有操作,lb表示左边界
}op[MAXN];
int c1[MAXN], c2[MAXN], n;//两个树状数组
int h1[MAXN], h2[MAXN], L[MAXN];//用于hash,L[i]表示第i次询问的左边界

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

void update(int *c, int n, int k, int v) {
    while(k <= n) {
        c[k] += v;
        k += lowbit(k);
    }
}

int query(int *c, int k) {
    int ans = 0;
    while(k > 0) {
        ans += c[k];
        k -= lowbit(k);
    }
    return ans;
}

int main()
{
#ifndef ONLINE_JUDGE
    FIN;
//    FOUT;
#endif
    int cas = 0;
    while(~scanf("%d", &n)) {
        mem0(c1); mem0(c2);
        int cnt = 0, sz1 = 0, sz2 = 0;
        rep (i, 1, n) {
            scanf("%d %d", &op[i].type, &op[i].lb);
            if(op[i].type == 0) {
                cnt ++;
                h1[sz1++] = op[i].lb;
                h2[sz2++] = op[i].lb + cnt;
                L[cnt] = op[i].lb;
            }
        }

        sort(h1, h1 + sz1); sz1 = unique(h1, h1 + sz1) - h1;
        sort(h2, h2 + sz2); sz2 = unique(h2, h2 + sz2) - h2;
        printf("Case #%d:\n", ++cas);

        cnt = 0;
        rep (i, 1, n) {
            if( !op[i].type ) { //(cnt_seg - query(c1, lb - 1)) - (cnt_seg - query(c2, rb)) = q2(rb) - q1(lb - 1)
                ++cnt;
                int lb = lower_bound(h1, h1 + sz1, op[i].lb) - h1 + 1;
                int rb = lower_bound(h2, h2 + sz2, op[i].lb + cnt) - h2 + 1;
                printf("%d\n", query(c2, rb) - query(c1, lb - 1));
                update(c1, sz1, lb, 1);
                update(c2, sz2, rb, 1);
            }
            else {
                update(c1, sz1, lower_bound(h1, h1 + sz1, L[op[i].lb]) - h1 + 1, -1);
                update(c2, sz2, lower_bound(h2, h2 + sz2, L[op[i].lb] + op[i].lb) - h2 + 1, -1);
            }
        }
    }
    return 0;
}
posted @ 2015-08-12 14:51  再见~雨泉  阅读(514)  评论(0编辑  收藏  举报