CodeForces - 1190D Tokitsukaze and Strange Rectangle ( 离散化 + 树状数组 )

Describe:

There are n points on the plane, the i-th of which is at (xi,yi). Tokitsukaze wants to draw a strange rectangular area and pick all the points in the area.
The strange area is enclosed by three lines, x=l, y=a and x=r, as its left side, its bottom side and its right side respectively, where l, r and a can be any real numbers satisfying that l<r. The upper side of the area is boundless, which you can regard as a line parallel to the x-axis at infinity. The following figure shows a strange rectangular area.
A point (xi,yi) is in the strange rectangular area if and only if l<xia. For example, in the above figure, p1 is in the area while p2 is not.
在这里插入图片描述
Tokitsukaze wants to know how many different non-empty sets she can obtain by picking all the points in a strange rectangular area, where we think two sets are different if there exists at least one point in one set of them but not in the other.
Input
The first line contains a single integer n (1≤n≤2×105) — the number of points on the plane.
The i-th of the next n lines contains two integers xi, yi (1≤xi,yi≤109) — the coordinates of the i-th point.
All points are distinct.

Output
Print a single integer — the number of different non-empty sets of points she can obtain.

sample input:

3
1 1
1 2
1 3

sample output:

3
题意:

平面上有n个点,你可以画一个左边界为l, 右边界为r, 下边界为a的无上界的区域(l, r, a 都可以为实数),把一些点框在里面,求有多少种不同的点集可以被框出来。(比如图片中可以框出三种点集:{p1} , {p2}, {p1, p2}

题解:

离散化,按y从大到小扫,如果处于y这条线的点只有一个,那么统计到目前为止,左边有多少间隔,右边有多少间隔,乘起来就是这个点的贡献。
如果处于y这条线的点有多个,先把这条线上所有点的x坐标对应间隔都加入当前的间隔中,然后再把这条线上的点从左到右扫一遍,一个点的贡献为:右边的间隔数 * (当前点左边的间隔数 - 前一个点左边的间隔数)。

题目链接
ac代码:

#include<bits/stdc++.h>
#define ll long long
#define lowbit(x) (x & -x)
using namespace std;
const int maxn = 2e5 + 50;
ll cx[maxn], cy[maxn];
int nx, ny;
ll l[maxn], r[maxn];
int n;
ll a[maxn];
void add(int i, int x)
{
    while(i < maxn){
        a[i] += x;
        i += lowbit(i);
    }
}
ll query(int i)
{
    ll ans = 0;
    while(i){
        ans += a[i];
        i -= lowbit(i);
    }return ans;
}
struct node{
    ll x, y;
    bool operator < (const node & a){
        if(y != a.y) return y > a.y;
        return x < a.x;
    }
}e[maxn];
int main()
{
    memset(l,0x3f,sizeof l);
    memset(r,0,sizeof r);
    nx = ny = 0;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) {
        scanf("%I64d%I64d",&e[i].x, &e[i].y);
        cx[++nx] = e[i].x;
        cy[++ny] = e[i].y;
    }
    cx[++nx] = 0x3f3f3f3f;
    sort(cx+1, cx+1+nx);
    nx = unique(cx+1, cx+1+nx) - cx - 1;
    sort(cy+1, cy+1+ny);
    ny = unique(cy+1, cy+1+ny) - cy - 1;
    ll ans = 0;
    sort(e+1, e+1+n);
    add(nx, 1);
    int pre = -1;
    for(int i = 1; i <= n; ++i){
        int posx = lower_bound(cx+1, cx+nx+1, e[i].x) - cx;
        if(query(posx) - query(posx-1) == 0) add(posx, 1);
        if(e[i].y != e[i+1].y){
            if(pre == -1){
                int posx = lower_bound(cx+1, cx+nx+1, e[i].x) - cx;
                ll L = query(posx);
                ll R = query(nx) - L;
                ans += L*R;
            }
            else{
                ll res = 0;
                ll A = query(nx);
                for(int j = pre; j <= i; ++j){
                    int posx = lower_bound(cx+1, cx+nx+1, e[j].x) - cx;
                    ll L = query(posx) - query(res);
                    ans += L*(A - query(posx));
                    res = posx;
                }
                pre = -1;
            }
        }
        else if(pre == -1) pre = i;
    }
    cout<<ans<<endl;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值