POJ 3109 Inner Vertices(线段树)

题目链接:点击打开链接

思路:

题目可以转化成这样的问题: 有多少个白点的上下左右都有黑点, 答案是这样的白点个数 + n

怎么求符合要求的白点个数呢?

按照常见思路, 我们先对黑点进行排序, 先按x再按y将其分层。  然后我们处理到了第i列, 符合要求的白点一定在相邻的上下两个黑点之间, 我们假设此时相邻两个黑点的y坐标是L和R, 那么我们要求区间[L+1, R-1]里有多少个白点的左右都有黑点。 为了降低复杂度, 我们可以先预处理出每一个y坐标最右边的黑点的x坐标是什么, 然后每处理完一列,就再扫一遍这列, 看看每个黑点的右边是否还有黑点, 如果有就在线段树中修改为1,否则为0, 用线段树可以快速统计区间和。

细节参见代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <ctime>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const double eps = 1e-6;
const double PI = acos(-1);
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 2e5 + 10;
int T, n, m, sum[maxn<<2], x[maxn], y[maxn], p[maxn], table[maxn];
inline void pushup(int o) {
    sum[o] = sum[o<<1] + sum[o<<1|1];
}
void build(int l, int r, int o) {
    sum[o] = 0;
    if(l == r) return ;
    int m = (l + r) >> 1;
    build(l, m , o<<1);
    build(m+1, r, o<<1|1);
}
void update(int L, int R, int v, int l, int r, int o) {
    if(L <= l && r <= R) {
        sum[o] = v; return ;
    }
    int m = (l + r) >> 1;
    if(L <= m) update(L, R, v, l, m, o<<1);
    if(m < R) update(L, R, v, m+1, r, o<<1|1);
    pushup(o);
}
int query(int L, int R, int l, int r, int o) {
    if(L <= l && r <= R) {
        return sum[o];
    }
    int m = (l + r) >> 1;
    int ans = 0;
    if(L <= m) ans += query(L, R, l, m, o<<1);
    if(m < R) ans += query(L, R, m+1, r, o<<1|1);
    pushup(o);
    return ans;
}
struct node {
    int x, y;
    node(int x=0, int y=0):x(x), y(y) {}
    bool operator < (const node& rhs) const {
        if(x != rhs.x) return x < rhs.x;
        else return y < rhs.y;
    }
}a[maxn];
int main() {
    while(~scanf("%d",&n)) {
        int cnt1 = 0, cnt2 = 0;
        for(int i = 1; i <= n; i++) {
            scanf("%d%d", &a[i].x, &a[i].y);
            x[cnt1++] = a[i].x;
            y[cnt2++] = a[i].y;
            p[i] = 0;
        }
        sort(a+1, a+n+1);
        sort(x, x+cnt1);
        cnt1 = unique(x, x+cnt1) - x;
        sort(y, y+cnt2);
        cnt2 = unique(y, y+cnt2) - y;
        build(1, cnt2, 1);
        for(int i = n; i >= 1; i--) {
            int xx = lower_bound(x, x+cnt1, a[i].x) - x + 1;
            int yy = lower_bound(y, y+cnt2, a[i].y) - y + 1;
            if(p[yy]) ;
            else {
                p[yy] = 1;
                table[yy] = xx;
            }
        }
        int ans = 0;
        for(int i = 1; i <= n; i++) {
            if(i == 1 || a[i].x != a[i-1].x) {
                int xx = lower_bound(x, x+cnt1, a[i].x) - x + 1;
                int yy = lower_bound(y, y+cnt2, a[i].y) - y + 1;
                if(table[yy] > xx) update(yy, yy, 1, 1, cnt2, 1);
                else update(yy, yy, 0, 1, cnt2, 1);
                continue;
            }
            int xx = lower_bound(x, x+cnt1, a[i].x) - x + 1;
            int yy = lower_bound(y, y+cnt2, a[i].y) - y + 1;
            if(table[yy] > xx) update(yy, yy, 1, 1, cnt2, 1);
            else update(yy, yy, 0, 1, cnt2, 1);
            int L = lower_bound(y, y+cnt2, a[i-1].y) - y + 1;
            int R = lower_bound(y, y+cnt2, a[i].y) - y + 1;
            L++; R--;
            if(L > R) continue;
            ans += query(L, R, 1, cnt2, 1);
        }
        printf("%d\n", ans + n);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值