HDU 5862 Counting Intersections (离散化+扫描线+树状数组)
题意:给你若干个平行于坐标轴的,长度大于0的线段,且任意两个线段没有公共点,不会重合覆盖。问有多少个交点。
析:题意很明确,可是并不好做,可以先把平行与x轴和y轴的分开,然后把平行y轴的按y坐标从小到大进行排序,然后我们可以枚举每一个平行x轴的线段,
我们可以把平行于x轴的线段当做扫描线,只不过有了一个范围,每次要高效的求出相交的线段数目,可以用一个优先队列来维护平行y轴的线段的上坐标,
如果在该平行于x轴的范围就给相应的横坐标加1,这样就很容易想到是用树状数组来维护,然后每次求出最左边和最右边,相减即可,但是由于数据范围太大,
所以我们考虑离散化横坐标。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 200000 + 10;
const int mod = 1000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Node{
int s, e, pos;
Node(){ }
Node(int ss, int ee, int p) : s(ss), e(ee), pos(p) { }
};
bool cmp1(const Node &lhs, const Node &rhs){ return lhs.s < rhs.s; }
bool cmp2(const Node &lhs, const Node &rhs){ return lhs.pos < rhs.pos; } vector<Node> row, col;
vector<int> all; struct node{
int id, val;
node(int i, int v) : id(i), val(v) { }
bool operator < (const node &p) const{
return val > p.val;
}
}; int sum[maxn<<2]; int lowbit(int x){ return -x&x; }
void add(int x, int val){
while(x <= n){
sum[x] += val;
x += lowbit(x);
}
} int getSum(int x){
int ans = 0;
while(x){
ans += sum[x];
x -= lowbit(x);
}
return ans;
} int main(){
int T; cin >> T;
while(T--){
scanf("%d", &n);
row.clear(); col.clear(); all.clear();
for(int i = 0; i < n; ++i){
int x1, x2, y1, y2;
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
if(x1 == x2){
if(y1 > y2) swap(y1, y2);
col.push_back(Node(y1, y2, x1));
}
else{
if(x1 > x2) swap(x1, x2);
row.push_back(Node(x1, x2, y1));
}
all.push_back(x1);
all.push_back(x2);
}
sort(all.begin(), all.end());
all.erase(unique(all.begin(), all.end()), all.end());
sort(col.begin(), col.end(), cmp1);
sort(row.begin(), row.end(), cmp2);
memset(sum, 0, sizeof sum);
n <<= 1;
LL ans = 0;
int cnt = 0;
priority_queue<node> pq;
for(int i = 0; i < row.size(); ++i){
while(cnt < col.size() && row[i].pos >= col[cnt].s){
int id = lower_bound(all.begin(), all.end(), col[cnt].pos) - all.begin() + 1;
pq.push(node(id, col[cnt++].e));
add(id, 1);
}
while(!pq.empty() && row[i].pos > pq.top().val){
add(pq.top().id, -1);
pq.pop();
}
int l = lower_bound(all.begin(), all.end(), row[i].s) - all.begin() + 1;
int r = lower_bound(all.begin(), all.end(), row[i].e) - all.begin() + 1;
ans += getSum(r) - getSum(l-1);
}
printf("%lld\n", ans);
}
return 0;
}
HDU 5862 Counting Intersections (离散化+扫描线+树状数组)的更多相关文章
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- hdu 3887 Counting Offspring dfs序+树状数组
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU6447(离散化扫描线+树状数组)
一眼看过去就x排序扫描一下,y是1e9的离散化一下,每层用树状数组维护一下,然后像dp倒着循环似的树状数组就用y倒着插就可行了. 类似题目练习:BZOJ4653.BZOJ1218 #pragma co ...
- HDU 5862 Counting Intersections 扫描线+树状数组
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Counting Intersections Time Limit: 12000/ ...
- Hdu 5862 Counting Intersections(有n条线段,每一条线段都是平行于x轴或者y轴,问有多少个交点+树状数组区间求和单点跟新)
传送门:Hdu 5862 Counting Intersections 题意:有n条线段,每一条线段都是平行于x轴或者y轴,问有多少个交点 分析: 基本的操作流程是:先将所有的线段按照横树坐标x按小的 ...
- hdu 5862 Counting Intersections
传送门:hdu 5862 Counting Intersections 题意:对于平行于坐标轴的n条线段,求两两相交的线段对有多少个,包括十,T型 官方题解:由于数据限制,只有竖向与横向的线段才会产生 ...
- 【BZOJ1818】[Cqoi2010]内部白点 扫描线+树状数组
[BZOJ1818][Cqoi2010]内部白点 Description 无限大正方形网格里有n个黑色的顶点,所有其他顶点都是白色的(网格的顶点即坐标为整数的点,又称整点).每秒钟,所有内部白点同时变 ...
- [BZOJ4822][CQOI2017]老C的任务(扫描线+树状数组)
4822: [Cqoi2017]老C的任务 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 379 Solved: 203[Submit][Statu ...
- FZU 2225 小茗的魔法阵 扫描线+树状数组
这个题和一个CF上的找"Z"的题差不多,都是扫描线+树状数组 从右上角的主对角线开始扫描,一直扫到左下角,每次更新,右延伸等于该扫描线的点,注意在其所在的树状数组更新就好了 时间复 ...
随机推荐
- 什么是DDOS攻击?怎么防御?
一.什么是DDOS? DDOS是英文Distributed Denial of Service的缩写,意即"分布式拒绝服务",那么什么又是拒绝服务(Denial of Servic ...
- 算法(Algorithms)第4版 练习 1.3.42
After copy Left on Stack r: be to not or be to Left on Stack copy: be to not or be to After r pop Le ...
- phalcon:官方多模块支models层,mode数据库配置(二)
phalcon:官方多模块支models层,mode数据库配置(二) 利用:\pahlcon\mvc\model\Manager::registerNamespaceAlias()方法获取多模块下的m ...
- 分享知识-快乐自己:Caused by: org.hibernate.tool.schema.extract.spi.SchemaExtractionException: More than one table found in namespace (, ) : Dept (XXX)
在命名空间(,)中找到多个表 - SchemaExtractionException? 问题: 尝试在Java应用程序中使用Hibernate将一些值保存到表中时,我一直面临着这个奇怪的异常. 但是, ...
- utc时间、本地时间及时间戳转化
1.时间戳的概念 时间戳的定义请看百科unix时间戳,需要注意的时间戳为当前时刻减去UTC时间(1970.1.1)零点时刻的秒数差,与当前系统所处的时区无关,同一时刻不管在任何时区下得到的时间戳都是一 ...
- Java读取文件的时候,如何让指针重新回到文件的开头
今天在测试IO流的使用的时候发现在reader读取文件之后,再向文件添加内容,再继续读文件,打印出的结果只能读取追加的文件. 如何才能重新读取呢?试了mark和reset,似乎会报异常.记在这以后看是 ...
- Smali文件添加try/catch语句,出现“invalid use of move-exception”异常
插入代码如下: 捕获到以下异常: 2019-03-18 21:09:35.431 8272-8272/com.xxxx.xxxx E/AndroidRuntime: FATAL EXCEPTION: ...
- luoguP1941福赖皮波德
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
- NOIp2018集训test-10-17 (bike day3)
发现自己gradully get moodier and moodier了 负面情绪爆发地越来越频繁,根本out of control,莫名其妙地就像着了魔一样 为什么用英语大概是因为今天早上早自习因 ...
- Poj 1860 Currency Exchange(Bellman-Ford,SPFA解单源最短路径问题)
一.题意 有多个货币交易点,每个只能互换两种货币,兑换的汇率不同,并收取相应的手续费.有N种货币,假定你拥有第S中,数量为V,有M个兑换点.问你能不能通过兑换操作使你最后拥有的S币比起始的时候多. 二 ...