D - Nested Segments CodeForces - 652D (离散化+树桩数组)

D - Nested Segments

CodeForces - 652D

You are given n segments on a line. There are no ends of some segments that coincide. For each segment find the number of segments it contains.

Input

The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of segments on a line.

Each of the next n lines contains two integers *l**i* and *r**i* ( - 109 ≤ li < ri ≤ 109) — the coordinates of the left and the right ends of the i-th segment. It is guaranteed that there are no ends of some segments that coincide.

Output

Print n lines. The j-th of them should contain the only integer *a**j* — the number of segments contained in the j-th segment.

Examples

Input

41 82 34 75 6

Output

3010

Input

33 41 52 6

Output

011

题意:

给你n个线段,

保证没有两个线段的端点是相同的。

问你每一个线段包含了多少个其他的线段?

思路:

离散化+树状数组

首先对给定的坐标进行离散化,那么所有坐标都在 2*n的范围内了。

这个范围是树桩数组可以维护的。

然后我们按照x坐标降序排序。

然后从1~n依次扫描线段,

对于每一个线段,用树状数组询问1~y 的sum和就是它包含的线段个数。

然后把自己的y坐标在树桩数组中+1

因为询问时已经在树状数组中的线段的x都比当前线段的x大,所以只需要查看有多少个y坐标小于当前的y坐标即可。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
// #define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}

inline void getInt(int *p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
struct pii {
    int fi;
    int se;
    int id;
};
pii a[maxn];
std::vector<int> v;
int n;
int tree[maxn];
int lowbit(int x)
{
    return -x & x;
}
void add(int x, int val)
{
    while (x < maxn) {
        tree[x] += val;
        x += lowbit(x);
    }
}
int ask(int x)
{
    int res = 0;
    while (x) {
        res += tree[x];
        x -= lowbit(x);
    }
    return res;
}
pii b[maxn];
int ans[maxn];
bool cmp(pii aa, pii bb)
{
    if (aa.fi != bb.fi) {
        return aa.fi > bb.fi;
    } else {
        return aa.se < bb.se;
    }
}
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    cin >> n;
    repd(i, 1, n) {
        cin >> a[i].fi >> a[i].se;
        a[i].id = i;
        v.push_back(a[i].fi);
        v.push_back(a[i].se);
    }
    sort(ALL(v));
    repd(i, 1, n) {
        b[i].fi = lower_bound(ALL(v), a[i].fi) - v.begin() + 1;
        b[i].se = lower_bound(ALL(v), a[i].se) - v.begin() + 1;
        b[i].id = a[i].id;
    }
    sort(b + 1, b + 1 + n, cmp);
    repd(i, 1, n) {
        ans[b[i].id] = ask(b[i].se);
        add(b[i].se, 1);
    }
    repd(i, 1, n) {
        cout << ans[i] << endl;
    }
    return 0;
}

inline void getInt(int *p)
{
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    } else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}



全部评论

相关推荐

我是秋招面的抖音电商部门,从八月开始给我打电话约面试,当时有加对应的hr微信,前几轮还好,从三面开始就是拖半个月一下在推进,面了三轮以后拖了一个月说再加一个交叉面,面完过了半个多月问hr,hr说她离职了不管我了,我连自己的流程在哪里都不清楚。在官网投诉了三次(前两次正常反馈问题,字节你像听不懂话一样),今天下午突然有其他部门约我面试,说在人才库里看见我简历了觉得很匹配,我拒了说不想面了。过了一会,声称原来那个部门的hr负责人给我回电话,表示抱歉,说因为北京hc不够了把我流程释放了,前面的hr是实习生离职了,没有和我对接非常抱歉,如果有想法可以帮我推荐给其他部门。那么我问你:1.&nbsp;从八月等到11月,三个月时间我前面不断确认还有没有hc,给我的回复都是有hc,没hc你面什么呢?2.&nbsp;作为一个大厂你流程这么一坨?让实习生对接校招流程,实习生离职了对应的候选人就不管了?不是我主动问我的流程就这样一直被卡?3.&nbsp;还说我面评没问题,帮我推荐其他部门,现在各个大厂都开奖了,你还让我从头开始面?我前面面了你们三个月啊,孩子饿死了开始装得想负责了。4.&nbsp;你们官网的流程反馈对应的人员是理解能力有问题吗,一个问题我投诉三次才能看明白是吗?套话一堆在哪里随便选了个常用语就给我回复了?不想开可以直接把入口关了
奋斗的小码农a:字节流程就是恶心死人,一次字节广告5面挂一次字节抖音电商6面挂,最后那个面试官装都不装了直接分屏聊天还外放语音家里真的私募了
秋招吐槽大会
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务