USACO 5.5 Picture(周长并)
POJ最近做过的原题。
/*
ID: cuizhe
LANG: C++
TASK: picture
*/
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
#define maxn 10000
#define lson l , m, rt<<1
#define rson m+1, r,rt<<1|1
int que[maxn*];
int sum[maxn*];
int cnt[maxn*];
bool lbd[maxn*] , rbd[maxn*];
int numseg[maxn*];
struct node
{
int lx,rx,y;
int s;
node() {}
node(int a,int b,int c,int d):lx(a),rx(b),y(c),s(d) {}
bool operator < (const node &S) const
{
if(y == S.y) return s > S.s;
return y < S.y;
}
} mat[maxn];
int bin(int x,int n)
{
int str,end,mid;
str = ,end = n;
while(str <= end)
{
mid = (str+end)/;
if(que[mid] == x)
return mid;
else if(que[mid] > x)
end = mid - ;
else
str = mid + ;
}
return mid;
}
void pushup(int rt,int l,int r)
{
if(cnt[rt])
{
lbd[rt] = rbd[rt] = ;
sum[rt] = que[r+] - que[l];
numseg[rt] = ;
}
else if (l == r)
{
sum[rt] = numseg[rt] = lbd[rt] = rbd[rt] = ;
}
else
{
lbd[rt] = lbd[rt<<];
rbd[rt] = rbd[rt<<|];
sum[rt] = sum[rt<<] + sum[rt<<|];
numseg[rt] = numseg[rt<<] + numseg[rt<<|];
if (lbd[rt<<|] && rbd[rt<<]) numseg[rt] -= ;
}
}
void update(int L,int R,int c,int l,int r,int rt)
{
if(L <= l&&r <= R)
{
cnt[rt] += c;
pushup(rt,l,r);
return ;
}
int m = (l+r)>>;
if(L <= m)
update(L,R,c,lson);
if(R > m)
update(L,R,c,rson);
pushup(rt,l,r);
}
int main()
{
int n,num,i,l,r;
int a,b,c,d;
freopen("picture.in","r",stdin);
freopen("picture.out","w",stdout);
while(scanf("%d",&n)!=EOF)
{
if(!n) break;
num = ;
for(i = ; i < n; i ++)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
que[num] = a;
mat[num ++] = node(a,c,b,);
que[num] = c;
mat[num ++] = node(a,c,d,-);
}
sort(que,que+num);
sort(mat,mat+num);
int k = ;
for(i = ; i < num; i ++)
{
if(que[i] != que[i-])
que[k++] = que[i];
}
int ans = ,last = ;
memset(cnt,,sizeof(cnt));
memset(sum,,sizeof(sum));
memset(lbd,,sizeof(lbd));
memset(rbd,,sizeof(rbd));
for(i = ; i < num; i ++)
{
l = bin(mat[i].lx,k-);
r = bin(mat[i].rx,k-)-;
if(l <= r)
update(l,r,mat[i].s,,k-,);
ans += numseg[] * (mat[i+].y - mat[i].y);
ans += abs(sum[] - last);
last = sum[];
}
printf("%d\n",ans);
}
return ;
}
USACO 5.5 Picture(周长并)的更多相关文章
- USACO 5.5 Picture
PictureIOI 1998 A number, N (1 <= N < 5000), of rectangular posters, photographs and other pic ...
- HDU 1828 Picture(线段树扫描线求周长)
Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- 【HDU 1828】 Picture (矩阵周长并,线段树,扫描法)
[题目] Picture Problem Description A number of rectangular posters, photographs and other pictures of ...
- HDU 1828 Picture(长方形的周长和)
HDU 1828 Picture 题目链接 题意:给定n个矩形,输出矩形周长并 思路:利用线段树去维护,分别从4个方向扫一次,每次多一段的时候,就查询该段未被覆盖的区间长度,然后周长就加上这个长度,4 ...
- 51nod 1206 && hdu 1828 Picture (扫描线+离散化+线段树 矩阵周长并)
1206 Picture 题目来源: IOI 1998 基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 收藏 关注 给出平面上的N个矩形(矩形的边平行于X轴 ...
- hdu 1828 Picture 切割线求周长
Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- 51nod 1206 Picture 矩形周长求并 | 线段树 扫描线
51nod 1206 Picture 矩形周长求并 | 线段树 扫描线 #include <cstdio> #include <cmath> #include <cstr ...
- P1856 [USACO5.5]矩形周长Picture
P1856 [USACO5.5]矩形周长Picture $len$ $sum$ $num$ $flag\_l$ $flage\_ ...
- 51nod 1206:Picture 求覆盖周长
1206 Picture 题目来源: IOI 1998 基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 收藏 关注 给出平面上的N个矩形(矩形的边平行于X轴和 ...
随机推荐
- Java 8新特性
Java 8版本最大的改进就是Lambda表达式,其目的是使Java更易于为多核处理器编写代码:其次,新加入的Nashorn引擎也使得Java程序可以和JavaScript代码互操作:再者,新的日期时 ...
- centos6.4下安装php的imagick和imagemagick扩展教程
imagick在centos6.4的安装方法: .安装ImageMagick 代码如下: wget http://soft.vpser.net/web/imagemagick/ImageMagick- ...
- linux c学习笔记----互斥锁属性
转自:http://lobert.iteye.com/blog/1762844 互斥锁属性 使用互斥锁(互斥)可以使线程按顺序执行.通常,互斥锁通过确保一次只有一个线程执行代码的临界段来同步多个线程. ...
- UML 类关系及画法
1 泛化 [泛化关系]:是一种继承关系,表示一般与特殊的关系,它指定了子类如何特化父类的所有特征和行为.例如:老虎是动物的一种,即有老虎的特性也有动物的共性. [箭头指向]:带三角箭头的实线,箭头指向 ...
- SPI-软件开发注意事项
01 PD ,设置数据库前一定把模板设置号,命名规则规划清楚.
- sp_executesql的用法
之前做项目的时候负责一个成绩分析的模块儿,写存储过程的时候因为考试的科目是不固定的,所以导致查找成绩的sql语句也是动态的,就用到了sp_executesql,下面就来说一说它的用法 需求:表名是动态 ...
- loj 1037(状压dp)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25914 思路:dp[state]表示当前状态下要消耗的最小的sho ...
- 智能车学习(八)——菜单的实现
一.代码分享 1.头文件 #ifndef __MENU_H #define __MENU_H /***********宏定义************/ //页面声明 typedef enum Menu ...
- 分布式文件系统FastDFS设计原理(转)
FastDFS是一个开源的轻量级分布式文件系统,由跟踪服务器(tracker server).存储服务器(storage server)和客户端(client)三个部分组成,主要解决了海量数据存储问题 ...
- getTickCount()函数 VS GetTickCount()函数
这俩函数看上去长得太像了,笔者曾经马大哈地把两者当成一个函数了(确实长得很像),知道有一天发现返回的值离预期值差很远差仔细查了下. 1. getTickCount() 实际上,该函数为opencv中的 ...