Codeforces 1197E Count The Rectangles(树状数组+扫描线)
题意:
给你n条平行于坐标轴的线,问你能组成多少个矩形,坐标绝对值均小于5000
保证线之间不会重合或者退化
思路:
从下到上扫描每一条纵坐标为y的水平的线,然后扫描所有竖直的线并标记与它相交的线,保证上端至少多出1
并用树状数组维护它们
然后从y+1网上扫描纵坐标为yy的水平的线,查询y到yy中同时与他们相交的竖直的线的条数算贡献即可
每查询完一个yy后,要在树状数组内删除上端点为yy的竖直的线,因为以后的yy与它不会再相交了
代码:
几乎是照着官方题解来的。。
#include<iostream>
#include<cstdio>
#include<algorithm>
//#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1 using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 1e4+;
const int maxm = 2e6+;
const int inf = 0x3f3f3f3f;
//const db pi = acos(-1.0); int n;
struct node{
int l,r;
int x;
node(){}
node(int a,int b,int c):l(a),r(b),x(c){}
};
vector<node>vt[maxn],hr[maxn];
vector<int>tmp[maxn];
int tree[maxn*];
int lowbit(int x){
return x&-x;
}
void add(int x,int C){
for(int i=x;i<maxn-;i+=lowbit(i)){
tree[i]+=C;
}
}
int sum(int x){
int ans=;
for(int i=x;i;i-=lowbit(i)){
ans+=tree[i];
}
return ans;
} int main(){
scanf("%d", &n);
for(int i = ; i <= n; i++){
int x1,y1,x2,y2;
scanf("%d %d %d %d" ,&x1, &y1, &x2, &y2);
x1+=;x2+=;
y1+=;y2+=;
if(x1==x2){
vt[x1].pb(node(min(y1,y2),max(y1,y2),x1));
}
else{
hr[y1].pb(node(min(x1,x2),max(x1,x2),y1));
//printf("===%d\n",y1-5000);
}
}
ll ans = ;
for(int y = ; y < maxn-; y++){
for(int i = ; i < hr[y].size(); i++){
//for(int j = 0; j < maxn-10; j++)tmp[j].clear();
//mem(tree,0);
node lne = hr[y][i];
int l = lne.l, r = lne.r;
for(int x = l; x <= r; x++){
for(int j = ; j < (int)vt[x].size(); j++){
if(vt[x][j].l<=y&&vt[x][j].r>=y+){
add(x,);
tmp[vt[x][j].r].pb(x);
} }
}
for(int yy = y+; yy < maxn-; yy++){
for(int j = ; j < (int)hr[yy].size(); j++){
ll res = sum(hr[yy][j].r)-sum(hr[yy][j].l-);
ans+=res*(res-)/;
}
for(int j = ; j < (int)tmp[yy].size(); j++){
add(tmp[yy][j],-);
}
tmp[yy].clear();
} }
}
printf("%lld",ans);
return ;
}
/* */
Codeforces 1197E Count The Rectangles(树状数组+扫描线)的更多相关文章
- Codeforces 786C Till I Collapse(树状数组+扫描线+倍增)
[题目链接] http://codeforces.com/contest/786/problem/C [题目大意] 给出一个数列,问对于不同的k,将区间划分为几个, 每个区间出现不同元素个数不超过k时 ...
- BZOJ 1452: [JSOI2009]Count 二维树状数组
1452: [JSOI2009]Count Description Input Output Sample Input Sample Output 1 2 HINT Source 题解:设定C[101 ...
- Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)
[题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...
- CodeForces 828E DNA Evolution(树状数组)题解
题意:给你一个串k,进行两个操作: “1 a b”:把a位置的字母换成b “2 l r s”:求l到r有多少个字母和s匹配,匹配的条件是这样:从l开始无限循环s形成一个串ss,然后匹配ss和指定区间的 ...
- Codeforces 909C Python Indentation:树状数组优化dp
题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...
- Count(二维树状数组)
[bzoj1452][JSOI2009]Count Description Input Output Sample Input Sample Output 12 HINT 题解:对于每一个颜色建一 ...
- CodeForces - 597C Subsequences 【DP + 树状数组】
题目链接 http://codeforces.com/problemset/problem/597/C 题意 给出一个n 一个 k 求 n 个数中 长度为k的上升子序列 有多少个 思路 刚开始就是想用 ...
- Codeforces 635D Factory Repairs【树状数组】
又是看了很久的题目... 题目链接: http://codeforces.com/contest/635/problem/D 题意: 一家工厂生产维修之前每天生产b个,维修了k天之后每天生产a个,维修 ...
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
随机推荐
- 组件与props简解
一.创建组件 1.函数式创建 特点: 组件不能访问this对象 组件无法访问生命周期的方法 无状态组件只能访问输入的props,无副作用 function Title() { return <h ...
- 单机Web后端接口服务压力测试
单机Web后端接口服务压力测试 工具:Apache jmeter 环境:Window 10 语言:Kotlin + java 架构:SpringBoot + + Mysql + redis + Spr ...
- spring boot学习笔记(1)
一.构建springboot项目 可以通过idea的springboot initialization来创建, idea的功能很强大,可以自己选择需要集成的插件. 完整的项目结构是这样的: DemoA ...
- vue报错 [Intervention] Ignored attempt to cancel a touchmove event with cancelable
在vue开发中使用vue-awesome-swiper制作轮播图,手动拖动时会报错,解决方案: 需要滑动的标签 { touch-action: none; } -------------------- ...
- PHP-FPM 远程代码执行漏洞(CVE-2019-11043)的简单复现学习
1.概述 漏洞主要由于 PHP-FPM 中 sapi/ fpm/ fpm/ fpm_main.c 文件内的 env_path_info 下溢导致,攻击者可以使用换行符 %0a 破坏 Nginx 中 f ...
- Ansible配合Virtualenv安装配置
Ansible的两种安装模式(Centos7) 1.Yum包管理安装 #yum -y install ansible 2.Git源代码安装[推荐] git clone https://github.c ...
- LeetCode 第15题-三数之和
1. 题目 2.题目分析与思路 3.思路 1. 题目 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且 ...
- 三分钟网络基础-IP地址分类
IP 地址的编址方法共经过了三个历史阶段: 分类的 IP 地址 子网的划分 超网 这篇文章首先介绍,最初始的 IP 地址分类方法. 分类的 IP 将 IP 地址划分为若干个固定类,每一类地址都由两个固 ...
- Java.Json模板.省市区三级JSON
[ { "name": "北京市", "city": [ { "name": "北京市", &quo ...
- for in 和 for i 十月 javascript 第一弹 记录
for in 里面的 i 不仅有 数字还有这些鬼