2187 回家种地

Accept: 56    Submit: 230
Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem Description

小A毕业后,跑回家种地了。可是种地也不是简单的活,小A在播种的时候碰到难题了。


A承包了一块无限大的农田,他每次选取一块矩形区域播种。可是小A是个糊涂虫,他每次都忘记之前播种过的区域是哪一块,因此他就随机选择一个矩形区域播
种。如果一个区域被播种两次或者两次以上,因为种子之间的竞争,导致每个种子得到的资源都不够,无法顺利成长,因此长不出果实。

现在小A希望你帮他算算秋天的时候,能够长出庄稼的区域的面积。

Input

第一行给出一个整数T,表示测试样例的组数。

每个样例第一行有一个整数n,表示小A播种的矩形的个数。

接下来n行,每行有4个整数x1,y1,x2,y2,表示矩形的左下角和右上角。

1<=n<=10^5

0<=x1<x2<=10^8

0<=y1<y2<=10^8

Output

输出题目所求的面积,格式见样例。

Sample Input

2
2
0 0 10 10
1 1 2 2
2
0 0 1 1
1 1 2 2

Sample Output

Case 1: 99
Case 2: 2

离散后用map卡时 , 手写一个map然后注意一下结果爆 long long ~

做法是用覆盖大于1次减去 覆盖大于0次的 就求得等于1次的~

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <iostream>
using namespace std;
#define root 1,(n<<1)+10,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lr rt<<1
#define rr rt<<1|1 const int N = ;
const int M = ; struct HASHMAP {
int head[M] , id[N] , next[N] , key[N] , tot ;
void init() {
memset( head , - , sizeof head );
tot = ;
}
void insert( int x , int idx ) {
int u = x % M ;
key[tot] = x ; id[tot] = idx ; next[tot] = head[u] ; head[u] = tot++ ;
}
int find( int x ) {
int u = x % M ;
for( int i = head[u] ; ~i ; i = next[i] ) {
if( key[i] == x ) return id[i] ;
}
}
} mp ; struct Point {
int x , y1 , y2 , v ;
bool operator < ( const Point &a ) const {
return x < a.x ;
}
}p[N]; int n , tot , tt , e[N] , c[N] ; void addpoint( int x , int y1 ,int y2 , int v ) {
p[tot].x = x , p[tot].y1 = y1 , p[tot].y2 = y2 , p[tot].v = v , tot++ ;
} void lisan() {
mp.init();
sort( e , e + tt );
int ntt = ;
for( int i = ; i < tt ; ++i ) {
if( e[i] != e[i-] ) e[ntt++] = e[i] ;
}
tt = ntt ;
for( int i = ; i < tt ; ++i ) {
c[i+] = e[i] ;
mp.insert( e[i] , i + ) ;
}
} int cnt[N<<] , lazy[N<<] , date[N<<] ; void build( int l , int r , int rt ) {
cnt[rt] = lazy[rt] = ;
if( l == r ) {
date[rt] = c[l+] - c[l];
return ;
}
int mid = (l+r)>>;
build(lson) , build(rson) ;
date[rt] = date[lr] + date[rr];
} void Down( int rt ) {
if( lazy[rt] ) {
cnt[lr] += lazy[rt] ;
cnt[rr] += lazy[rt] ;
lazy[lr] += lazy[rt] ;
lazy[rr] += lazy[rt] ;
lazy[rt] = ;
}
} void Up( int rt ) {
cnt[rt] = min( cnt[lr] , cnt[rr] );
} void update( int l , int r , int rt , int L , int R , int v ) {
if( l == L && r == R ) {
cnt[rt] += v ; lazy[rt] += v ;
return ;
}
int mid = (l+r)>> ;
Down(rt) ;
if( R <= mid ) update(lson,L,R,v) ;
else if( L > mid ) update( rson,L,R,v);
else update(lson,L,mid,v),update(rson,mid+,R,v);
Up(rt);
} int query1( int l , int r , int rt , int L , int R ) {
if( cnt[rt] > ) return date[rt] ;
if( l == r ) return ;
Down(rt);
int mid = (l+r)>>;
if( R <= mid ) return query1(lson,L,R);
else if( L > mid ) return query1(rson,L,R);
else return query1(lson,L,mid) + query1(rson,mid+,R);
} int query0( int l , int r , int rt , int L , int R ) {
if( cnt[rt] > ) return date[rt] ;
if( l == r ) return ;
Down(rt);
int mid = (l+r)>>;
if( R <= mid ) return query0(lson,L,R);
else if( L > mid ) return query0(rson,L,R);
else return query0(lson,L,mid) + query0(rson,mid+,R);
} int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int _ , cas = ; scanf("%d",&_);
while( _-- ) {
printf("Case %d: ",cas++);
scanf("%d",&n);
tt = tot = ;
for( int i = ; i < n ; ++i ) {
int x1 , y1 , x2 , y2 ;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
addpoint( x1 , y1 , y2 , );
addpoint( x2 , y1 , y2 , - );
e[tt++] = y1 ;
e[tt++] = y2 ;
}
lisan();
sort( p , p + tot ) ;
build(,tt-,);
long long ans = ;
update(,tt-,,mp.find(p[].y1),mp.find(p[].y2)-,p[].v);
for( int i = ; i < tot ; ++i ) {
if( p[i].x != p[i-].x ) {
ans += ( p[i].x - p[i-].x ) * 1LL * ( query0(,tt-,,,tt-) - query1(,tt-,,,tt-) ) ;
}
update(,tt-,,mp.find(p[i].y1),mp.find(p[i].y2)-,p[i].v);
}
cout << ans << endl ;
}
return ;
}

FZU 2187 回家种地 ( 扫描线 + 离散 求矩阵单次覆盖面积 )的更多相关文章

  1. HDU 1255 覆盖的面积 ( 扫描线 + 离散 求矩阵大于k次面积并 )

    覆盖的面积 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  2. HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)

    做这道题之前,建议先做POJ 1151  Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...

  3. POJ 1151 Atlantis(经典的线段树扫描线,求矩阵面积并)

    求矩阵的面积并 采用的是区间更新 #include <iostream> #include <stdio.h> #include <string.h> #inclu ...

  4. POJ 1151 Atlantis 求矩阵面积并 扫描线 具体解释

    题意: 给定n个矩阵的左下角和右上角坐标,求矩阵面积并(矩阵总是正放的,即与x轴y轴都平行) 思路: 扫描线裸题 http://www.cnblogs.com/fenshen371/p/3214092 ...

  5. MATLAB中求矩阵非零元的坐标

    MATLAB中求矩阵非零元的坐标: 方法1: index=find(a); [i,j]=ind2sub(size(a),index); disp([i,j]) 方法2: [i,j]=find(a> ...

  6. 求矩阵中各列数字的和 Exercise08_01

    import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年12月 * 题目:求矩阵中各列数字的和 * */ public class Exercise ...

  7. 求矩阵主对角线元素的和 Exercise08_02

    import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年12月 * 题目:求矩阵主对角线元素的和 * */ public class Exercis ...

  8. matlab求矩阵、向量的模

    求矩阵的模: function count = juZhenDeMo(a,b) [r,c] = size(a);%求a的行列 [r1,c1] = size(b);%求b的行列 count = 0; f ...

  9. CUDA -- 规约求矩阵的行和

    求矩阵每行的和? 可以把每行放入一个不同线程块,这样行与行之间进行粗粒度的并行.而对于每行,其对应的线程块中分配n个线程(对应行宽),使用共享存储器,让每个线程从显存中读取一个数至shared mem ...

随机推荐

  1. linux里面以指定用户运行命令

    一.chroot方式 [root@localhost ~]# chroot --userspec "nginx:nginx" "/" sh -c "w ...

  2. python的type和object

    在python中一切皆对象,这是个用python的人都知道的概念,以int举例,比如a=2,type下: 发现他的type是int,在python中type就是类,所以a是类int的一个对象,实例是类 ...

  3. 07http基础

    1.http协议 1.1 概念 是对浏览器和服务器端数据传输格式的规范! 1.2 http协议内容 请求 GET /bbs/hello HTTP/1.1 # 请求行 Host: localhost:8 ...

  4. python如何查看内存占用空间

    我们如何查看变量占用了多少内存空间呢 首先我们引用sys模块,在使用getsizeof()方法 import sys L = [x for x in range(10000)] print(sys.g ...

  5. js undefined三目运算

    js ajax传值中 "id":$('#id').val(), 如果#id不存在,使用$('#id').val()||‘’,可避免向后台传入undefined

  6. 每次当浏览到网页,点击tab标签又回到顶部去了!

    通常tab的标签使用a链接,而a链接的href值为#,这是一个锚点的属性,因此他会跳转到网页的顶端.如果你的tab包含一个id=tab,也可以设置为href="#tab"这样他就会 ...

  7. WinForm、WPF、ASP.NET窗口生命周期

    https://blog.csdn.net/s_521_h/article/details/73826928

  8. 解决 canvas 在高清屏中绘制模糊的问题

    主要代码部分: <canvas id="my_canvas" width="540" heihgt="180"></can ...

  9. 配置自己的CocoaPods库

    序 默认安装的cocoapods确实很好用,可是毕竟自己会写一些库和修改一些第三方库来用.所幸cocoapods确实是一个神器.他可以定义自己的库来用. 如何安装Cocoapods,请参考这篇 从头来 ...

  10. 简单Spring整合JdbcTemplate

    实体类: public class User implements Serializable{ private Integer id; private String username; private ...