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 ...
随机推荐
- C#如何控制方法的执行时间,超时则强制退出方法执行
转自:http://outofmemory.cn/code-snippet/1762/C-how-control-method-zhixingshijian-chaoshi-ze-force-quit ...
- Burp Suite教程(英文版)
In this article, we are going to see another powerful framework that is used widely in pen-testing. ...
- codeforces 395B2 iwiwi
#include<cstdio> #include<cstring> using namespace std; ]="iiiiiiiiiiiiiiiiiiiiii&q ...
- IDEA如何打包可运行jar的一个问题。
转自http://bglmmz.iteye.com/blog/2058785 背景: 有时候,我们会用IDEA来开发一些小工具,需要打成可运行的JAR包:或者某些项目不是WEB应用,纯粹是后台应用,发 ...
- Cocos2d-x 3.2编译Android程序错误的解决方案
最近的升级Cocos2d-x 3.2正式版.iOS不管是什么程序编译问题,使用结果cocos compile -p android编译APK计划.结果悲剧,出现以下错误. Android NDK: I ...
- tbody添加垂直滚动条
法一: 用2个table: <table width="300" border="0" cellpadding="0" cellspa ...
- input checkbox问题和li里面包含checkbox
<input type="checkbox" id="checkbox1"/> $("input#checkbox1").cli ...
- ruby 模块 的引入
module My NA="China" def My.set_name(name) @name=name end def My.get_name return @name end ...
- x+2y+3z=n的非负整数解数
题目:给一个正整数n,范围是[1,10^6],对于方程:x+2y+3z = n,其中x,y,z为非负整数,求有多少个这样的三元组 (x,y,z)满足此等式. 分析:先看x+2y=m,很明显这个等式的非 ...
- WCF - Windows Service Hosting
WCF - Windows Service Hosting The operation of Windows service hosting is a simple one. Given below ...