POJ - 3109 Inner Vertices

不存在-1的情况,而且最多一轮就结束了。如果新增加的黑点v0会产生新的黑点v1,那么v0和v1肯定是在一条轴上的,而原来这条轴上已经有黑点了。

离散以后扫描线统计,往线段上插点,然后查询区间上点数。

不妨以x为主轴,用一条条平行于y轴的线去扫。

按照x为主y为副排序以后,记录下标,将下标按y为主排序,为的是把y相同的一系列点变成一个入点(d[id] = 1),一个出点(d[id] = -1),

可能某个相同y值的点只有一个,所以最后的出点 -= 1。

点可能有重复,判重的话就标记一下平行x轴的线。

/*********************************************************
*            ------------------                          *
*   author AbyssalFish                                   *
**********************************************************/
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<numeric>
using namespace std;

const int maxn = 1e5;

typedef long long ll;
typedef pair<int,int> Point;
#define xc first
#define yc second
Point p[maxn];
int ys[maxn], d[maxn]; //discrete y, scan line
int n;
int r[maxn];

bool cmp(int a,int b)
{
    return p[a].yc < p[b].yc || (p[a].yc == p[b].yc && p[a].xc < p[b].xc);
}

int C[maxn+1], n0;
bool vis[maxn+1];

int sum(int x)
{
    int re = 0;
    while(x > 0){
        re += C[x]; x &= x-1;
    }
    return re;
}

void add(int x,int d)
{
    while(x <= n0){
        C[x] += d;
        x += x&-x;
    }
}

void solve()
{
    for(int i = 0; i < n; i++){
        scanf("%d%d",&p[i].xc,&p[i].yc);
        r[i] = i;
    }
    //compress
    sort(p,p+n);
    sort(r,r+n,cmp);
    ys[r[0]] = 1; d[r[0]] = 1;
    for(int i = 1; i < n; i++){
        ys[r[i]] = ys[r[i-1]];
        if(p[r[i]].yc != p[r[i-1]].yc) {
            d[r[i-1]] -= 1;
            ys[r[i]]++;
            d[r[i]] = 1;
        }
    }
    n0 = ys[r[n-1]];
    d[r[n-1]] -= 1;
    //memset(C+1,0,sizeof(int)*n0);

    ll ans = n;
    for(int i = 0, j; i < n;){
        j = i;
        while(j < n && p[j].xc == p[i].xc) { //不包含端点
            if(d[j] < 0) {
                vis[ys[j]] = false;
                add(ys[j], -1);
            }
            j++;
        }
        if(ys[i] < ys[j-1]-1) {
            ans += sum(ys[j-1]-1) - sum(ys[i]);
            for(int k = i+1; k < j-1; k++){ //去重
                if(vis[ys[k]]) ans--;
            }
        }
        while(i < j) {
            if(d[i] > 0) {
                add(ys[i],1);
                vis[ys[i]] = true;
            }
            i++;
        }
    }
    printf("%I64d\n", ans);
}

//#define LOCAL
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    scanf("%d", &n);
    solve();
    return 0;
}
时间: 2024-10-11 22:48:32

POJ - 3109 Inner Vertices的相关文章

poj 3109 Inner Vertices(树状数组)

Inner Vertices Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 1692   Accepted: 446 Case Time Limit: 2000MS Description There is an infinite square grid. Some vertices of the grid are black and other vertices are white. A vertex V is cal

POJ题目分类推荐 (很好很有层次感)

著名题单,最初来源不详.直接来源:http://blog.csdn.net/a1dark/article/details/11714009 OJ上的一些水题(可用来练手和增加自信) (POJ 3299,POJ 2159,POJ 2739,POJ 1083,POJ 2262,POJ 1503,POJ 3006,POJ 2255,POJ 3094) 初期: 一.基本算法: 枚举. (POJ 1753,POJ 2965) 贪心(POJ 1328,POJ 2109,POJ 2586) 递归和分治法. 递

POJ 刷题指南

OJ上的一些水题(可用来练手和增加自信) (POJ 3299,POJ 2159,POJ 2739,POJ 1083,POJ 2262,POJ 1503,POJ 3006,POJ 2255,POJ 3094) 初期: 一.基本算法: 枚举. (POJ 1753,POJ 2965) 贪心(POJ 1328,POJ 2109,POJ 2586) 递归和分治法. 递推. 构造法.(POJ 3295) 模拟法.(POJ 1068,POJ 2632,POJ 1573,POJ 2993,POJ 2996) 二

挑战程序设计竞赛 3.3 活用各种数据结构

[Summarize] 1. 线性维护只能处理部分问题的时候要想到数据拆分,容斥解决问题 2. 在不断有人被淘汰的序列问题中,查找左右第几个人是谁的时候可以考虑线段树优化 3. 当问题结果并不同时依赖与两个维度的操作的时候,二维问题可拆分为两个一维问题分别解决 POJ 1990:MooFest /* 题目大意:给出每头奶牛的位置和至少要多少分贝的音量才能听到谈话 现在求奶牛两两交流成功需要的距离*分贝的总和. 题解:我们将奶牛对于需要的分贝排序,那么在计算的时候, 每头奶牛只要计算和序列前面所有

POJ 1741 Tree(树的点分治,入门题)

Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21357   Accepted: 7006 Description Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v)=The min distance between node u and v.Give an in

poj 1113 凸包周长

Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33888   Accepted: 11544 Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he w

【POJ 1741】 Tree (树的点分治)

Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)=The min distance between node u and v. Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v)

POJ 1155 树状dp

TELE Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3856   Accepted: 2054 Description A TV-network plans to broadcast an important football match. Their network of transmitters and users can be represented as a tree. The root of the tre

poj 1113 Wall(标准的凸包果题)

题目链接:http://poj.org/problem?id=1113 Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he would not listen to his Architect's proposals to build