IOI1998 hdu1828 poj1177 Picture
写了一发扫描线竟然狂WA不止,hdu死活过不了,poj和当时IOI的数据(还花了我1dsdn积分。。)都过了。
然后看到谋篇blog里有评论,把数据拿下来发现WA了。
数据是
2
0 0 1 1
1 0 2 1
就是有一条边贴着了,这个时候应该先加入新的边再删除,否则会算重。
另外,线段树不用清零哦。
//#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm> using namespace std; typedef int data_type;
const int N = + ; data_type disc[N];
int disc_tot; int get_hash(data_type a[], int n, data_type x) {
return lower_bound(a, a + n, x) - a;
} struct Rect {
data_type x1, y1, x2, y2;
void input() {
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
disc[disc_tot++] = y1, disc[disc_tot++] = y2;
}
} rect[N]; struct Event {
int id, tp;
data_type key() const {
if(tp == ) return rect[id].x1;
else return rect[id].x2;
}
bool operator < (const Event &rhs) const {
if(key() != rhs.key()) return key() < rhs.key();
return tp > rhs.tp; // 键值相同先加入再删除
}
Event() {}
Event(int id, int tp) : id(id), tp(tp) {}
} eve[N]; struct SegmentTree {
data_type sum[N * ];
int cnt[N * ], lp[N * ], rp[N * ], num[N * ];
#define lc s << 1
#define rc s << 1 | 1
void update(int s, int l, int r) {
if(cnt[s]) sum[s] = disc[r+] - disc[l], lp[s] = rp[s] = num[s] = ;
else if(l == r) sum[s] = , lp[s] = rp[s] = num[s] = ;
else {
sum[s] = sum[lc] + sum[rc];
lp[s] = lp[lc], rp[s] = rp[rc];
num[s] = num[lc] + num[rc] - (rp[lc] && lp[rc]);
}
}
void modify(int s, int l, int r, int L, int R, int d) {
if(L <= l && r <= R) cnt[s] += d;
else {
int mid = (l + r) >> ;
if(L <= mid) modify(lc, l, mid, L, R, d);
if(mid < R) modify(rc, mid + , r, L, R, d);
}
update(s, l, r);
}
#undef lc
#undef rc
} seg; bool solve() {
int n; if(scanf("%d", &n) == EOF) return ;
disc_tot = ;
for(int i = ; i < n; i++) {
rect[i].input();
eve[i << ] = Event(i, );
eve[i << | ] = Event(i, -);
}
sort(disc, disc + disc_tot);
disc_tot = unique(disc, disc + disc_tot) - disc;
sort(eve, eve + (n << ));
data_type res = , last = ;
for(int i = ; i < (n << ); i++) {
const Event &e = eve[i];
if(i) res += (seg.num[] << ) * (e.key() - eve[i-].key());
int l = get_hash(disc, disc_tot, rect[e.id].y1);
int r = get_hash(disc, disc_tot, rect[e.id].y2) - ;
if(l <= r) seg.modify(, , disc_tot - , l, r, e.tp);
res += abs(seg.sum[] - last), last = seg.sum[];
}
return printf("%d\n", res), ;
} int main() {
#ifdef DEBUG
freopen("in.txt", "r", stdin);
#endif
while(solve()); return ;
}
IOI1998 hdu1828 poj1177 Picture的更多相关文章
- [POJ1177]Picture
[POJ1177]Picture 试题描述 A number of rectangular posters, photographs and other pictures of the same sh ...
- poj1177 Picture 矩形周长并
地址:http://poj.org/problem?id=1177 题目: Picture Time Limit: 2000MS Memory Limit: 10000K Total Submis ...
- POJ1177 Picture —— 求矩形并的周长 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/POJ-1177 A number of rectangular posters, photographs and other pict ...
- CQOI2005 三角形面积并 和 POJ1177 Picture
1845: [Cqoi2005] 三角形面积并 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 1664 Solved: 443[Submit][Stat ...
- POJ1177 Picture 线段树+离散化+扫描线
求最终的覆盖图形周长,写这种代码应该短而精确,差的比较远 /* Problem: 1177 User: 96655 Memory: 348K Time: 32MS Language: C++ Resu ...
- POJ-1177 Picture 矩形覆盖周长并
题目链接:http://poj.org/problem?id=1177 比矩形面积并麻烦点,需要更新竖边的条数(平行于x轴扫描)..求横边的时候,保存上一个结果,加上当前长度与上一个结果差的绝对值就行 ...
- 【hdu1828/poj1177】线段树求矩形周长并
题意如图 题解:这题非常类似与矩形面积并,也是维护一个被覆盖了一次以上的线段总长. 但是周长要算新出现的,所以每次都要和上一次做差求绝对值. x轴做一遍,y轴做一遍. 但是有个问题:矩形边界重合的时候 ...
- HDU1828:Picture
浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:http://acm.hdu.edu.cn/showproblem.php? ...
- 【poj1177】 Picture
http://poj.org/problem?id=1177 (题目链接) 题意 求矩形周长并. Solution 转自:http://www.cnblogs.com/Booble/archive/2 ...
随机推荐
- 在Unity中高效工作(下)
原地址:http://www.unity蛮牛.com/thread-20005-1-1.html Tips for Creating Better Games and Working More Eff ...
- android 服务service开启和关闭
startService()方法开启一个服务. 服务只会开启一次,如果服务已经创建,并且没有销毁,多次调用startService方法只会执行onStartCommand方法和onStart方法. 服 ...
- 使用div+css制作简单导航 以及要注意问题
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- linux如何关闭防火墙
1) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重启后失效 开启: service iptables sta ...
- 【HDOJ】1026 Ignatius and the Princess I
这道题搞了很久啊.搜索非常好的一道题.昨天想了2小时,以为是深搜,但后来发现深搜怎么也没法输出正确路径.今天拿宽搜试了一下,问题就是普通的队列宽搜没法得到当前时间最小值.看了一下讨论区,发现优先级队列 ...
- 关于app transfer之后的开发
原文 http://blog.csdn.net/donghong2008/article/details/38020855 网络上有很多开发者提问怎么转让App并想知道具体的流程.实际上Appsto ...
- HTTP2.0那些事
1. HTTP2.0的前世 http2.0的前世是http1.0和http1.1这两兄弟.虽然之前仅仅只有两个版本,但这两个版本所包含的协议规范之庞大,足以让任何一个有经验的工程师为之头疼.http1 ...
- wpa_supplicant 和 802.11g WPA 认证的配置
# cd /etc/init.d# ln -s net.lo net.eth0 默认的接口名是 wlan0,让它开机时自动 up:cp /etc/init.d/net.lo /etc/init.d/n ...
- Failed to load unit 'PATM' (VERR_SSM_FIELD_NOT_CONSECUTIVE)
今天打开虚拟机启动的时候报错:Failed to load unit 'PATM' (VERR_SSM_FIELD_NOT_CONSECUTIVE) 后来发现虚机处于休眠状态,所以在虚机上右键,然后清 ...
- 使用curl下载文件
curl是一个非常好的网络传输库,使用也很简单.常用的使用方式是用它来下载资源文件,以下提供一个下载方法 #include <stdio.h> #include <iostream. ...