题目链接:传送门 问每条线段被包含了多少次
把线段按左端点排序
左端点相同的按右端点大的在前面
这样就不用考虑左端点的影响了
每次插入一条线段就将1-r加1
询问r-inf的值
因为这时左端点的影响已经忽略
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define
#define
using namespace std;
typedef long long ll;
struct node {
int l, r, w;
}tree[A];
struct N {
int l, r, id;
friend bool operator < (const N a, const N b) {
return a.l == b.l ? a.r > b.r : a.l < b.l;
}
}e[A];
void build(int k, int l, int r) {
tree[k].l = l; tree[k].r = r; tree[k].w = 0;
if (l == r) return;
int m = (l + r) >> 1;
build(k << 1, l, m);
build(k << 1 | 1, m + 1, r);
}
void change(int k, int pos) {
if (tree[k].l == tree[k].r) {
tree[k].w++;
return;
}
int m = (tree[k].l + tree[k].r) >> 1;
if (pos <= m) change(k << 1, pos);
else change(k << 1 | 1, pos);
tree[k].w = tree[k << 1].w + tree[k << 1 | 1].w;
}
int ask(int k, int l, int r) {
if (tree[k].l >= l and tree[k].r <= r) return tree[k].w;
int m = (tree[k].l + tree[k].r) >> 1, ans = 0;
if (l <= m) ans += ask(k << 1, l, r);
if (r > m) ans += ask(k << 1 | 1, l, r);
return ans;
}
int n, ans[A];
int main(int argc, char const *argv[]) {
while (scanf("%d", &n)) {
if (!n) return 0;
memset(&tree, 0, sizeof tree);
for (int i = 1; i <= n; i++) scanf("%d%d", &e[i].l, &e[i].r), e[i].id = i, e[i].l++, e[i].r++;
build(1, 1, 100000); sort(e + 1, e + n + 1);
for (int i = 1; i <= n; i++) {
if (e[i].l == e[i - 1].l and e[i].r == e[i - 1].r)
ans[e[i].id] = ans[e[i - 1].id];
else ans[e[i].id] = ask(1, e[i].r, 100000);
change(1, e[i].r);
}
for (int i = 1; i <= n; i++) i == n ? printf("%d\n", ans[i]) : printf("%d ", ans[i]);
}
return 0;
}