BZOJ 1818: [Cqoi2010]内部白点 (BIT + 扫描线)
就是求多条线段的交点数,直接BIT+扫描线就行了. 注意不要算重最初存在的点.
CODE
#include<bits/stdc++.h>
using namespace std;
char cb[1<<15],*cs=cb,*ct=cb;
#define getc() (cs==ct&&(ct=(cs=cb)+fread(cb,1,1<<15,stdin),cs==ct)?0:*cs++)
template<class T>inline void read(T &res) {
char ch; int flg = 1; for(;!isdigit(ch=getc());)if(ch=='-')flg=-flg;
for(res=ch-'0';isdigit(ch=getc());res=res*10+ch-'0'); res*=flg;
}
const int MAXN = 100005;
struct node { int x, y; }a[MAXN];
struct Node { int x, y, v; inline bool operator <(const Node &o)const { return y < o.y; } }q[MAXN];
inline bool cmpx(const node &a, const node &b) { return a.x == b.x ? a.y < b.y : a.x < b.x; }
inline bool cmpy(const node &a, const node &b) { return a.y == b.y ? a.x < b.x : a.y < b.y; }
int n, T[MAXN], b[MAXN], tot, N;
inline void upd(int x, int val) { while(x <= N) T[x] += val, x += x&-x; }
inline int qsum(int x) { int re = 0; while(x) re += T[x], x -= x&-x; return re; }
int main() {
read(n);
for(int i = 1; i <= n; ++i)
read(a[i].x), read(a[i].y), b[++N] = a[i].x;
sort(b + 1, b + N + 1);
N = unique(b + 1, b + N + 1) - b - 1;
for(int i = 1; i <= n; ++i) //离散化x坐标
a[i].x = lower_bound(b + 1, b + N + 1, a[i].x) - b;
sort(a + 1, a + n + 1, cmpx);
for(int i = 1, j; i <= n; i = j) {
int L = a[i].y, R;
for(j = i; j <= n && a[j].x == a[i].x; ++j)
R = a[j].y;
if(R-L > 1) { //差分
++tot, q[tot].x = a[i].x, q[tot].y = L+1, q[tot].v = 1;
++tot, q[tot].x = a[i].x, q[tot].y = R, q[tot].v = -1;
}
}
sort(a + 1, a + n + 1, cmpy);
sort(q + 1, q + tot + 1);
int cur = 1, ans = n;
for(int i = 1, j; i <= n; i = j) {
while(cur <= tot && q[cur].y <= a[i].y)
upd(q[cur].x, q[cur].v), ++cur;
for(j = i+1; j <= n && a[j].y == a[i].y; ++j)
if(a[j-1].x+1 <= a[j].x-1)
ans += qsum(a[j].x-1) - qsum(a[j-1].x); //差分
//注意这里不能只用最靠左和最靠右的点形成的线段在BIT里查找
//因为这段里面可能有最初就存在的黑点
}
printf("%d\n", ans);
}
BZOJ 1818: [Cqoi2010]内部白点 (BIT + 扫描线)的更多相关文章
- bzoj 1818 Cqoi2010 内部白点 扫描线
[Cqoi2010]内部白点 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1126 Solved: 530[Submit][Status][Disc ...
- BZOJ 1818: [Cqoi2010]内部白点 扫描线+树状数组
问题转化为求每一个极长横线段与极长纵线段的交点个数. 这个东西用扫描线+树状数组维护一下就可以了. code: #include <cstdio> #include <algorit ...
- bzoj 1818: [Cqoi2010]内部白点
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; struc ...
- BZOJ 1818: [Cqoi2010]内部白点(树状数组)
传送门 解题思路 首先一定不可能有\(-1\)的情况,因为新产生的黑点不会造成任何贡献,它的各个方面都是不优的.那么只需要统计一遍答案,首先要将横坐标相同的两个点看成一条竖线,纵坐标相同的点看成一条横 ...
- BZOJ_1818_[Cqoi2010]内部白点 _扫描线+树状数组
BZOJ_1818_[Cqoi2010]内部白点 _扫描线+树状数组 Description 无限大正方形网格里有n个黑色的顶点,所有其他顶点都是白色的(网格的顶点即坐标为整数的点,又称整点).每秒钟 ...
- 【BZOJ】1818: [Cqoi2010]内部白点(树状数组+离散+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1818 这一题一开始我就看错了,bzoj的那个绝对值109简直坑人,应该是10^9,我直接写了个暴力. ...
- 1818: [Cqoi2010]内部白点
Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1394 Solved: 625 [Submit][Status][Discuss] Descriptio ...
- Bzoj1818: [Cqoi2010]内部白点 && Tyvj P2637 内部白点 扫描线,树状数组,离散化
1818: [Cqoi2010]内部白点 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 704 Solved: 344[Submit][Status] ...
- 【BZOJ1818】[CQOI2010]内部白点(树状数组,扫描线)
[BZOJ1818][CQOI2010]内部白点(树状数组,扫描线) 题面 BZOJ 题解 不难发现\(-1\)就是在搞笑的. 那么对于每一行,我们显然可以处理出来最左和最右的点,那么等价于我们在横着 ...
随机推荐
- [AcWing303/304]任务安排2/3
[AcWing303]任务安排2 有 \(N\) 个任务排成一个序列在一台机器上等待执行,它们的顺序不得改变.机器会把这 \(N\) 个任务分成若干批,每一批包含连续的若干个任务.从时刻 \(0\) ...
- opendir 、readdir 小结
转载于:http://blog.csdn.net/lin_fs/article/details/7335573 1. opendir() 头文件 #include<sys/types.h> ...
- 基于SpringBoot从零构建博客网站 - 整合ehcache和开发注册登录功能
对于程序中一些字典信息.配置信息应该在程序启动时加载到缓存中,用时先到缓存中取,如果没有命中,再到数据库中获取同时放到缓存中,这样做可以减轻数据库层的压力.目前暂时先整合ehcache缓存,同时预留了 ...
- 【转帖】Linux的桌面环境gnome、kde、xfce、lxde 等等使用比较
Linux的桌面环境gnome.kde.xfce.lxde 等等使用比较 https://www.cnblogs.com/chenmingjun/p/8506995.html 文章目录 图形界面架起用 ...
- Java实现链队
一.分析 队列是一种先进先出的线性表,它只允许在表的一端进行插入,而在另一端删除元素.允许插入的一端称为队尾,允许删除的一端称为队头. 链队是指采用链式存储结构实现的队列,它的基本操作如下: 1.初始 ...
- Linux就该这么学——新手必须掌握的命令之文件编辑命令组
cat 命令 用途 : 用于查看纯文本文件 格式 : cat [选项] [文件] 示例 : more 命令 用途 : 用于查看纯文本文件(内容较多的),可以用”Enter” 键或者”Space”键向下 ...
- curl post请求封装
/* POST /servlet/ICBCCMPAPIReqServlet?userID=jyi.y.1001&PackageID=201807311347539185&SendTim ...
- postpreSQL和oracle数据库的递归
oracle: --包含自身 select * from sec_org start with org_id ='9767FA56D52680AEE043C0A8670580AE' --开始节点 co ...
- winfrom 界面时间动态加载
Timer time1 = new Timer(); private void time1_Tick(object sender, EventArgs e) { lTime.Text = DateTi ...
- LeetCode:181.超过经理收入的员工
题目链接:https://leetcode-cn.com/problems/employees-earning-more-than-their-managers/ 题目 Employee 表包含所有员 ...