题意

有\(n\)(\(n\leq 5000\))个平行于x轴或平行于y轴的线段。求这些线段围成了多少个长方形。由多个长方形拼成的也算。

题解

考虑暴力的做法:先分别计算每条横着的线与哪些竖着的线有交点,再枚举两条横着的线,求与它们都有交点的线的个数,在这些线中选两条和这两条横着的线拼成长方形。

发现与同一条竖着的线有交点的横线一定是把所有横线按纵坐标排序后连续的一段。

感谢伟大的YSF!!!

代码

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define rep(i,x,y) for(register int i=(x);i<=(y);++i)
#define dwn(i,x,y) for(register int i=(x);i>=(y);--i)
#define view(u,k) for(int k=fir[u];~k;k=nxt[k])
#define LL long long
#define maxn 5007
using namespace std;
int read()
{
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)&&ch!='-')ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x*f;
}
void write(LL x)
{
if(x==0){putchar('0'),putchar('\n');return;}
int f=0;char ch[20];
if(x<0)putchar('-'),x=-x;
while(x)ch[++f]=x%10+'0',x/=10;
while(f)putchar(ch[f--]);
putchar('\n');
return;
}
struct node{int x,l,r;}a[maxn],b[maxn];
int n,tr[maxn+maxn],maxk=5001,sz=10002,cnta,cntb;
LL ans;
vector<int>ed[maxn];
node maken(int x,int l,int r){node tmp;tmp.x=x,tmp.l=l,tmp.r=r;return tmp;}
LL c(int x){return (LL)x*(x-1ll)/2ll;}
int lt(int x){return x&(-x);}
void add(int x,int k){for(;x<=sz;x+=lt(x))tr[x]+=k;return;}
int ask(int x){int k=0;for(;x;x-=lt(x))k+=tr[x];return k;}
int yes(node x,node y)
{
if(y.l<=x.x&&x.x<=y.r&&x.l<=y.x&&y.x<=x.r)return 1;
return 0;
}
bool cmpx(node x,node y){return x.x<y.x;}
bool cmpr(int x,int y){return b[x].r<b[y].r;}
int main()
{
n=read();
rep(i,1,n)
{
int x1=read(),y1=read(),x2=read(),y2=read();
if(x1>x2)swap(x1,x2);
if(y1>y2)swap(y1,y2);
if(y1==y2)a[++cnta]=maken(y1,x1,x2);
else b[++cntb]=maken(x1,y1,y2);
}
sort(a+1,a+cnta+1,cmpx);
rep(i,1,cnta)
{
rep(j,1,cntb)if(yes(a[i],b[j]))
{
add(b[j].x+maxk,1);
ed[i].push_back(j);
}
int li=ed[i].size(),now=-1;
sort(ed[i].begin(),ed[i].end(),cmpr);
rep(j,i+1,cnta)
{
while((now+1)<li&&b[ed[i][now+1]].r<a[j].x)add(b[ed[i][now+1]].x+maxk,-1),now++;
int num=ask(a[j].r+maxk)-ask(a[j].l+maxk-1);
ans+=c(num);
}
while((now+1)<li)add(b[ed[i][now+1]].x+maxk,-1),now++;
}
write(ans);
return 0;
}

并不对劲的CF1194E:Count The Rectangles的更多相关文章

  1. Educational Codeforces Round 68 E. Count The Rectangles

    Educational Codeforces Round 68 E. Count The Rectangles 传送门 题意: 给出不超过\(n,n\leq 5000\)条直线,问共形成多少个矩形. ...

  2. 【EDU68 E】 Count The Rectangles 数据结构算几何

    CF # 题意 总共有5000条线段,这些线段要么水平,要么垂直,问这些线段组成了多少矩形. # 思路 这是一个n*n*(log)的思路 自己一开始想着枚举两条垂直边,想着怎么把水平的边插入,再进行冗 ...

  3. Codeforces 1194E. Count The Rectangles

    传送门 看到 $n<=5000$,直接暴力枚举左右两条竖线 然后考虑怎么计算高度在某个范围内,左端点小于等于某个值,右端点大于等于某个值的横线数量 直接用权值树状数组维护当前高度在某个区间内的横 ...

  4. Codeforces - 1194E - Count The Rectangles - 扫描线

    https://codeforc.es/contest/1194/problem/E 给5000条正常的(同方向不会重叠,也不会退化成点的)线段,他们都是平行坐标轴方向的,求能组成多少个矩形. 先进行 ...

  5. Codeforces 1197E Count The Rectangles(树状数组+扫描线)

    题意: 给你n条平行于坐标轴的线,问你能组成多少个矩形,坐标绝对值均小于5000 保证线之间不会重合或者退化 思路: 从下到上扫描每一条纵坐标为y的水平的线,然后扫描所有竖直的线并标记与它相交的线,保 ...

  6. 面向对象设计模式_享元模式(Flyweight Pattern)解读

    场景:程序需要不断创建大量相似的细粒度对象,会造成严重的内存负载.我们可以选择享元模式解决该问题. 享元抽象:Flyweight 描述享元的抽象结构.它包含内蕴和外蕴部分(别被术语迷惑,这是一种比较深 ...

  7. Educational Codeforces Round 68

    目录 Contest Info Solutions A.Remove a Progression B.Yet Another Crosses Problem C.From S To T D.1-2-K ...

  8. nodejs api 中文文档

    文档首页 英文版文档 本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可. Node.js v0.10.18 手册 & 文档 索引 | 在单一页面中浏览 | JSON格 ...

  9. [ACM_暴力][ACM_几何] ZOJ 1426 Counting Rectangles (水平竖直线段组成的矩形个数,暴力)

    Description We are given a figure consisting of only horizontal and vertical line segments. Our goal ...

随机推荐

  1. CF1214D

    CF1214D 题意: 给你一个 $ n \times m $ 的矩阵,求最少用多少个障碍,将 $ (1,1) $ 到 $ (n,m) $ 的路径堵死. 题意: 因为可以将起点两边堵死,所以答案最多是 ...

  2. OpenResty之ngx_lua模块的加密接口

    原文: ngx_Lua模块中的加密api接口 ngx.crc32_short digest = ngx.crc32_short(str) 该方法主要是计算给定字符串 str 的循环校验码(Cyclic ...

  3. python中文显示乱码,已经在开头有了coding: utf-8

    乱码原因:因为你的文件声明为 utf-8,并且也应该是用 utf-8 的编码保存的源文件.但是 windows 的本地默认编码是 cp936,也就是 gbk 编码,所以在控制台直接打印 utf-8 的 ...

  4. Flink简介

    Flink简介 Flink的核心是一个流式的数据流执行引擎,其针对数据流的分布式计算提供了数据分布,数据通信以及容错机制等功能.基于流执行引擎,Flink提供了诸多更高抽象层的API以方便用户编写分布 ...

  5. Google Python Style Guide

    https://google.github.io/styleguide/pyguide.html

  6. Mac 平台安装MySQL

    Mac 平台安装MySQL   一.下载MySQL MySQL官网上https://dev.mysql.com/downloads/mysql/,下载Community Server版 出现如下界面, ...

  7. vue问题五:element ui组件的开始时间-结束时间验证

    <el-date-picker v-model="seach.before" type="date" placeholder="开始时间&quo ...

  8. VBA 格式化excel数据表 (数据分列)

    Sub ImportData() ' ' Copy Data from one workbook to the Current Workbook ' Place the macro file in t ...

  9. natively 在本地机器

    ​For JavaScript-based projects, it makes sense to have a command line task runner that executes Java ...

  10. C++ STL copy copy_backward

    #include <iostream>#include <algorithm>#include <vector>#include <functional> ...