Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
题目链接:http://codeforces.com/contest/610/problem/D
就是给你宽度为1的n个线段,然你求总共有多少单位的长度。
相当于用线段树求面积并,只不过宽为1,注意y和x的最大都要+1,这样才相当于求面积。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
const int MAXN = 4e5 + ;
typedef __int64 LL;
struct data {
LL x1 , x2 , y , flag;
bool operator <(const data& cmp) const {
return y < cmp.y;
}
}a[MAXN];
struct segtree {
LL l , r , lazy , val;
}T[MAXN << ];
map <LL , LL> mp;
LL x[MAXN]; void build(int p , int l , int r) {
int mid = (l + r) >> ;
T[p].l = l , T[p].r = r , T[p].val = T[p].lazy = ;
if(r - l == ) {
return ;
}
build(p << , l , mid);
build((p << )| , mid , r);
} void pushup(int p) {
if(T[p].lazy) {
T[p].val = (x[T[p].r] - x[T[p].l]);
}
else if(T[p].r - T[p].l == ) {
T[p].val = ;
}
else {
T[p].val = T[p << ].val + T[(p << )|].val;
}
} void updata(int p , int l , int r , int val) {
int mid = (T[p].l + T[p].r) >> ;
if(T[p].l == l && T[p].r == r) {
T[p].lazy += val;
pushup(p);
return ;
}
if(r <= mid) {
updata(p << , l , r , val);
}
else if(l >= mid) {
updata((p << )| , l , r , val);
}
else {
updata(p << , l , mid , val);
updata((p << )| , mid , r , val);
}
pushup(p);
} int main()
{
int n , f = , cnt = ;
LL x1 , x2 , y1 , y2;
scanf("%d" , &n);
for(int i = ; i < n ; i++) {
scanf("%I64d %I64d %I64d %I64d" , &x1 , &y1 , &x2 , &y2);
if(x1 > x2)
swap(x1 , x2);
x2++;
if(y1 > y2)
swap(y1 , y2);
y2++;
a[f].x1 = x1 , a[f].x2 = x2 , a[f].y = y1 , a[f].flag = ;
f++;
a[f].x1 = x1 , a[f].x2 = x2 , a[f].y = y2 , a[f].flag = -;
f++;
if(!mp[x1]) {
mp[x1] = ;
x[++cnt] = x1;
}
if(!mp[x2]) {
mp[x2] = ;
x[++cnt] = x2;
}
}
build( , , cnt);
sort(a , a + f);
sort(x + , x + cnt + );
for(int i = ; i < f ; i++) {
int pos = lower_bound(x + , x + cnt + , a[i].x1) - x;
a[i].x1 = pos;
pos = lower_bound(x + , x + cnt + , a[i].x2) - x;
a[i].x2 = pos;
}
LL res = ;
updata( , a[].x1 , a[].x2 , a[].flag);
for(int i = ; i < f ; i++) {
res += (LL)(a[i].y - a[i - ].y) * T[].val;
updata( , a[i].x1 , a[i].x2 , a[i].flag);
}
printf("%I64d\n" , res);
}
Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)的更多相关文章
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线
D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并
D. Vika and Segments Vika has an infinite sheet of squared paper. Initially all squares are whit ...
- Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树
C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树
题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
- Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash
E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)
题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所 ...
- Codeforces Round #312 (Div. 2) E. A Simple Task 线段树
E. A Simple Task 题目连接: http://www.codeforces.com/contest/558/problem/E Description This task is very ...
随机推荐
- HDU 4870 Rating (2014 Multi-University Training Contest 1)
Rating Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- 类Item_field
class Item_field :public Item_ident { protected: void set_field(Field *field); public: Field *field, ...
- return File
public ActionResult DownloadMessage() { string strExportData = "无数据!"; byte[] data = Syste ...
- UVa 10129 (并查集 + 欧拉路径) Play on Words
题意: 有n个由小写字母的单词,要求判断是否存在某种排列使得相邻的两个单词,前一个单词末字母与后一个单词首字母相同. 分析: 将单词的两个字母看做节点,则一个单词可以看做一条有向边.那么题中所求的排列 ...
- bzoj1797: [Ahoi2009]Mincut 最小割
最大流+tarjan.然后因为原来那样写如果图不连通的话就会出错,WA了很久. jcvb: 在残余网络上跑tarjan求出所有SCC,记id[u]为点u所在SCC的编号.显然有id[s]!=id[t] ...
- WIN7开无线
可以的,WIN7自身就带有这个功能的,不过默认是不开启的,也可以下载一个软件来弄.1.请却仍您使用的操作系统是微软的Windows 7或者Windows server 2008 R2,正版盗版皆可.( ...
- Java Web编程的主要组件技术——Struts的高级功能
参考书籍:<J2EE开源编程精要15讲> Struts对国际化的支持 "国际化"(I18N)指一个应用程序在运行时能根据客户端请求所来的国家/地区.语言的不同显示不同的 ...
- macro names must be identifiers
1.错把 #include 写成了 #define 会报这个错 2.定义一个不存在的宏业会报这个错,如加了-DANDRO 而ANDRO不存在
- I.MX6 Power off register hacking
/*********************************************************************** * I.MX6 Power off register ...
- I.MX6 Goodix GT9xx touchscreen driver porting
/************************************************************************ * I.MX6 Goodix GT9xx touch ...