Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)

时间:2023-01-16 22:18:59

题目链接: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 (线段树+扫描线+离散化)的更多相关文章

  1. Codeforces Round &num;337 &lpar;Div&period; 2&rpar; D&period; Vika and Segments 线段树扫描线

    D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...

  2. Codeforces Round &num;337 &lpar;Div&period; 2&rpar; D&period; Vika and Segments 线段树 矩阵面积并

    D. Vika and Segments     Vika has an infinite sheet of squared paper. Initially all squares are whit ...

  3. Codeforces Round &num;292 &lpar;Div&period; 1&rpar; C&period; Drazil and Park 线段树

    C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...

  4. Codeforces Round &num;254 &lpar;Div&period; 1&rpar; C&period; DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

  5. Codeforces Round &num;149 &lpar;Div&period; 2&rpar; E&period; XOR on Segment &lpar;线段树成段更新&plus;二进制&rpar;

    题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...

  6. Codeforces Round &num;321 &lpar;Div&period; 2&rpar; E&period; Kefa and Watch 线段树hash

    E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...

  7. Codeforces Round &num;271 &lpar;Div&period; 2&rpar; E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  8. Codeforces Round &num;207 &lpar;Div&period; 1&rpar; A&period; Knight Tournament (线段树离线)

    题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所 ...

  9. Codeforces Round &num;312 &lpar;Div&period; 2&rpar; E&period; A Simple Task 线段树

    E. A Simple Task 题目连接: http://www.codeforces.com/contest/558/problem/E Description This task is very ...

随机推荐

  1. MySQL AHI 实现解析

    版权声明:本文由musazhang原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/904925001482373849 来源 ...

  2. UITabBarController底层实现

    1.首先要了解:任何控制器,都能添加子控制器      UIViewController里面有一个方法:     - (void)addChildViewController:(UIViewContr ...

  3. windows xp 无法连接wpa无线网络

    其实以前一直是可以的,不知为什么前几天忽然就不能加入原有的无线网了.我的无线网是WPA加密的,采用DHCP分配IP(但针对特定MAC地址分配静态IP). 在网上找了许久,有的网友认为应该把无线网卡(那 ...

  4. css兼容性问题

    其实做网页最大的问题还是兼容性吧,要调试IE的各种浏览器. DIV+CSS设计IE6.IE7.FF 兼容性  DIV+CSS网页布局这是一种趋势,我也开始顺应这股趋势了,不过在使用DIV+CSS网站设 ...

  5. &lbrack;OC笔记&rsqb; static 关键字

    在变量声明前加上static关键字,可以使局部变量保留多次方法调用所得到的值.当多个方法对一个静态变量进行操作时,多个方法共享同一个静态变量的值.

  6. jquery validate&period;addMethod 正则表达式 &lpar;自定义验证方法&rpar;

    项目中使用的jQuery添加的校验的方法 $(document).ready(function(){         5           6/* 设置默认属性 */         7$.vali ...

  7. java&lowbar;JFrame&lowbar;demo

    不要见笑,cs基本入行很少做 留个demo备忘 /* * Copyright (c) 2014-2024 . All Rights Reserved. * * This software is the ...

  8. 阿里云配置安全组(配置入口port)

    访问:www.aliyun.com 登录后,左上角点击: 点击[云服务器] 点击右下角[配置规则] 入口 出口

  9. MATLAB 均方根误差MSE、两图像的信噪比SNR、峰值信噪比PSNR、结构相似性SSIM

    今天的作业是求两幅图像的MSE.SNR.PSNR.SSIM.代码如下: clc; close all; X = imread('q1.tif');% 读取图像 Y=imread('q2.tif'); ...

  10. ul li data-&ast; 数据的读取

    <ul class="questions"> <li> <div class="question">1.您的年龄是?< ...