UVA 11983 - Weird Advertisement(线段树)

UVA 11983 - Weird Advertisement

题目链接

题意:给定几个矩形,之后,求覆盖k次以上的点

思路:先把坐标离散化掉,然后把每个矩形x2,y1加一,这样就把求点转化为求面积,然后每个矩形拆分成上下两个线段,按y排序之后,从下往上每访问一条线段(第一条除外),答案就加上目前整条线段上次数大于等于k的长度乘上这条线段和上一条线段的高度差,然后再用这条线段,根据它是矩形下面还是上面的线段去更新整条线段,维护线段大于等于k的长度这步利用线段树

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define lson(x) ((x<<1)+1)
#define rson(x) ((x<<1)+2)

const int N = 30005;

int hash[N * 2], hn;

int t, n, k;

struct Line {
    int l, r, y, flag;
    Line() {}
    Line(int l, int r, int y, int flag) {
	this->l = l;
	this->r = r;
	this->y = y;
	this->flag = flag;
    }
} line[N * 2];

bool cmp(Line a, Line b) {
    return a.y < b.y;
}

void init() {
    scanf("%d%d", &n, &k);
    int x1, y1, x2, y2;
    for (int i = 0; i < n; i++) {
	scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
	x2++; y2++;
	hash[i] = x1; hash[i + n] = x2;
	line[i] = Line(x1, x2, y1, 1);
	line[i + n] = Line(x1, x2, y2, -1);
    }
    hn = 1;
    sort(hash, hash + 2 * n);
    for (int i = 1; i < 2 * n; i++) {
	if (hash[i] != hash[i - 1])
	    hash[hn++] = hash[i];
    }
    sort(line, line + 2 * n, cmp);
}

int F(int x) {
    int l = 0, r = hn - 1;
    while (l < r) {
	int mid = (l + r) / 2;
	if (hash[mid] < x) l = mid + 1;
	else r = mid;
    }
    return l;
}

struct Node {
    int l, r, sum[11], kv;
    Node() {}
    Node(int l, int r) {
	this->l = l;
	this->r = r;
	memset(sum, 0, sizeof(sum));
	kv = 0;
    }
} node[N * 8];

void build(int l, int r, int x = 0) {
    node[x] = Node(l, r);
    node[x].sum[0] = hash[r + 1] - hash[l];
    if (l == r)
	return;
    int mid = (l + r) / 2;
    build(l, mid, lson(x));
    build(mid + 1, r, rson(x));
}

void pushup(int x) {
    memset(node[x].sum, 0, sizeof(node[x].sum));
    if (node[x].l == node[x].r)
	node[x].sum[min(k, max(0, node[x].kv))] = hash[node[x].r + 1] - hash[node[x].l];
    else {
	for (int i = 0; i <= k; i++)
	    node[x].sum[min(k, max(0, i + node[x].kv))] += node[lson(x)].sum[i] + node[rson(x)].sum[i];
    }
}

void add(int l, int r, int v, int x = 0) {
    if (node[x].l >= l && node[x].r <= r) {
	node[x].kv += v;
	pushup(x);
	return;
    }
    int mid = (node[x].l + node[x].r) / 2;
    if (l <= mid) add(l, r, v, lson(x));
    if (r > mid) add(l, r, v, rson(x));
    pushup(x);
}

long long solve() {
    build(0, hn - 2);
    long long ans = 0;
    for (int i = 0; i < 2 * n; i++) {
	if (i) ans += (long long)(line[i].y - line[i - 1].y) * node[0].sum[k];
	add(F(line[i].l), F(line[i].r) - 1, line[i].flag);
    }
    return ans;
}

int main() {
    int cas = 0;
    scanf("%d", &t);
    while (t--) {
	init();
	printf("Case %d: %lld\n", ++cas, solve());
    }
    return 0;
}

UVA 11983 - Weird Advertisement(线段树)

时间: 2024-11-11 06:48:57

UVA 11983 - Weird Advertisement(线段树)的相关文章

UVA 11983 Weird Advertisement 线段树+离散化+扫描线

有点像HDU 3642的强化版.给你N个矩形的坐标,问题平面上被k个不同的矩形覆盖的面积是多少. 当初HDU 3642 是直接一个一个手写的,这里的k虽然说只有10,合并过成一个一个手写也是相当蛋疼的,不过仔细想一下,不难推出一般性的关系,然后直接用循环搞就好了.不过我还是因为有个地方忘记初始化WA了2发,真是弱o(╯□╰)o 注意每个房子代表一个点,而我们要把他转化成线段,对坐标进行一些简单的变换即可. #include <cstdio> #include <cstring> #

uva 11983 - Weird Advertisement(线段树)

题目链接:uva 11983 - Weird Advertisement 题目大意:给定n个矩形,问说有多少区域的面积被覆盖k次以上. 解题思路:将每个矩形差分成两条线段,一段为添加覆盖值1,一段为减少覆盖值1,同时记录两段的高度(横坐标).然后对纵坐标离散化建立线段树,然后对线段按照高度排序,维护整段区间中覆盖度大于K的长度,乘上高度上的范围即可. #include <cstdio> #include <cstring> #include <vector> #incl

UVA 11983 Weird Advertisement --线段树求矩形问题

题意:给出n个矩形,求矩形中被覆盖K次以上的面积的和. 解法:整体与求矩形面积并差不多,不过在更新pushup改变len的时候,要有一层循环,来更新tree[rt].len[i],其中tree[rt].len[i]表示覆盖次数大于等于i的线段长度,以便求面积,最后只要每次都用tree[1].len[K]来算面积即可. 代码: #include <iostream> #include <cmath> #include <iostream> #include <cst

UVA - 11983 Weird Advertisement (线段树求并面积)

Description G Weird Advertisement Renat Mullakhanov (rem), one of the most talented programmers in the world, passed away on March 11, 2011. This is very sad news for all of us. His team went to ACM ICPC World Finals - 2004, placed 4th and won gold m

UVA 11983 Weird Advertisement(线段树求矩形并的面积)

UVA 11983 题目大意是说给你N个矩形,让你求被覆盖k次以上的点的总个数(x,y<1e9) 首先这个题有一个转化,吧每个矩形的x2,y2+1这样就转化为了求N个矩形被覆盖k次以上的区域的面积 由于坐标很大,首先考虑的就是将坐标离散化,然后建立线段树tree[][K],表示x的某个区间被覆盖了K次(大于K次算K次)的实际长度,在计算时把矩形转化为一系列横线段(就是说将一个矩形拆开为两条线段,下面的标记为1,上面的标记为-1(这里的标记很有技巧)),然后将这些线段按照y值的从小到达排序(y值相

uva 11983 Weird Advertisement 扫描线

Weird Advertisement Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=18802 Description 2DPlaneLand is a land just like a huge 2D plane. The range of X axis is 0 to 109 and the range ofY axis

UVA 11235 Frequent values 线段树/RMQ

vjudge 上题目链接:UVA 11235 *******************************************************大白书上解释************************************************************ 题目大意:给出一个非降序排列的整数数组 a1,a2,a3,...,an,你的任务是对于一系列询问 (i, j),回答 ai,ai+1,...,aj 中出现次数最多的值所出现的次数. 输入格式:包含多组数据.每组

UVA - 11402 Ahoy, Pirates! (线段树)

In the ancient pirate ages, the Pirate Land was divided into two teams ofpirates, namely, the Buccaneer and the Barbary pirates.Each pirate's team was not fixed, sometimes the opponent pirates attacked andhe was taken away to the other pirate team. A

UVA 11525 Permutation ——(线段树,脑筋急转弯)

只要注意到对于譬如:S1*(k-1)! 因为后面k-1个数字的全排列个数刚好是(k-1)!,那么第一个数字就是没有取过的数字的第(S1+1)个即可.取走这个数字以后这个数字就不能再用了,依次类推即可得到整个排列了. 那么随便用线段树维护一下即可. 代码如下: 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 #define t_mid (l+r>>1) 5 #define