您的位置:首页 > 其它

LightOJ 1089 Points in Segments (II) 离散化 标记

2016-02-27 19:40 351 查看

题目

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26806

题目来源:2016.2.26ACM群赛

简要题意:给定一些一维线段,问某点处被线段覆盖了多少次。

题解

aia_i的范围小些,就只需要在lil_i处+1+1,ri+1r_i+1处−1-1然后做个前缀就行了。

于是只要把数据的li,ri+1l_i,r_i+1排序然后离散化就行了。

了解相关的方法可以比较快写出,一道典型的乱搞题。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
// head
const int N = 5e4+5;

PII a
;
int dis[N*2];
int res[N*2];

int main() {
int t, n, q, x, cas = 1;
scanf("%d", &t);
while (t--) {
memset(res, 0, sizeof res);

scanf("%d%d", &n, &q);
int cnt = 0;
for (int i = 0; i < n; i++) {
scanf("%d%d", &a[i].fi, &a[i].se);
a[i].se++;
dis[cnt++] = a[i].fi;
dis[cnt++] = a[i].se;
}
dis[cnt++] = 0;
dis[cnt++] = 1e8+5;

sort(dis, dis + cnt);
int m = unique(dis, dis + cnt) - dis;
for (int i = 0; i < n; i++) {
int l = lower_bound(dis, dis + m, a[i].fi) - dis;
int r = lower_bound(dis, dis + m, a[i].se) - dis;
res[l]++;
res[r]--;
}
for (int i = 1; i < m; i++) {
res[i] += res[i-1];
}

printf("Case %d:\n", cas++);
while (q--) {
scanf("%d", &x);
int pos = upper_bound(dis, dis + m, x) - dis - 1;
printf("%d\n", res[pos]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: