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\)就是在搞笑的. 那么对于每一行,我们显然可以处理出来最左和最右的点,那么等价于我们在横着 ...
随机推荐
- linux 静态库 ar命令用法
当我们的程序中有经常使用的模块,而且这种模块在其他程序中也会用到,这时按照软件重用的思想,我们应该将它们生成库,使得以后编程可以减少开发代码量.这里介绍命令ar,用来对库操作. 1.ar基本用法 ar ...
- 【Python】【基础知识】【内置常量】
Python的内置常量有: False.True.None.NotImplemented.Ellipsis.__debug__ 由 site 模块添加的常量:quit.exit.copyright.c ...
- 【LOJ】#2983. 「WC2019」数树
LOJ2983. 「WC2019」数树 task0 有\(i\)条边一样答案就是\(y^{n - i}\) task1 这里有个避免容斥的方法,如果有\(i\)条边重复我们要算的是\(y^{n - i ...
- Ugly Numbers UVA - 136(优先队列+vector)
Problem Description Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, ...
- springboot2.0整合freemarker快速入门
目录 1. 快速入门 1.1 创建工程pom.xml文件如下 1.2 编辑application.yml 1.3 创建模型类 1.4 创建模板 1.5 创建controller 1.6 测试 2. F ...
- 定义vue目录别名
- ubuntu下npm全局安装包报错的解决方案
大概就是 npm WARN registry Unexpected warning for https://registry.npmjs.org/: Miscellaneous Warning ERR ...
- 并不对劲的复健训练-bzoj5339:loj2578:p4593:[TJOI2018]教科书般的亵渎
题目大意 题目链接 题解 先将\(a\)排序. \(k\)看上去等于怪的血量连续段的个数,但是要注意当存在\(a_i+1=a_{i+1}\)时,虽然它们之间的连续段为空,但是还要算上:而当\(a_m= ...
- Codeforces 1178E. Archaeology
传送门 首先一定有解,考虑归纳法证明 首先 $n<=3$ 时显然 考虑 $n=4$ 时,那么因为 $s[1]!=s[2],s[3]!=s[4]$ ,并且 $s[i] \in {a,b,c}$ 由 ...
- 美团2017年CodeM大赛-初赛B轮 黑白树 (树形dp)
大意: 给定树, 初始每个点全为白色, 点$i$有权值$k_i$, 表示选择$i$后, 所有距离$i$小于$k_i$的祖先(包括i)会变为黑色, 求最少选多少个点能使所有点变为黑色. 链上情况的话, ...