HDU 5862 Counting Intersections
来源:互联网 发布:新浪云绑定独立域名 编辑:程序博客网 时间:2024/05/18 01:12
题意: 问与坐标轴平行的这些线段有多少个交点。
思路: 线扫描的思想,这种做法也是很好想的。
由于坐标的范围较大,可将其中一个坐标离散化(我是离散化X坐标)。
由于题目中有声明:The input data guarantee that no two segments share the same endpoint, no covered segments, and no segments with length 0.
所有没什么坑点。
具体的: 我是将x坐标进行离散化。对于每条边,如果与y轴平行,我分别记录该点的上点和下点,上点标记为-1,下点标记为1,如果是与x平行的边,就直接记录改变,并标记为0;然后对这些点按照y坐标进行排序。 接着就该是加点(边)的操作了。当该点标记为0,就算出(x1,x2)s的和.如果不是0,则在s的x1点加上它的标记。
代码如下:
#include<stdio.h>#include<string.h>#include<math.h>#include<queue>#include<vector>#include<iostream>#include<complex>#include<string>#include<set>#include<map>#include<algorithm>//#include<bits/stdc++.h>using namespace std;#pragma comment(linker, "/STACK:1024000000,1024000000")#define nn 100100#define mm 2000200#define ll long long#define ULL unsiged long long#define pb push_back#define mod 1000000007#define inf 0xfffffffffff#define eps 0.00000001struct node{int x1, y, x2;int idx;}ax[mm];int posx[mm], s[mm];bool cmp(node a, node b){if(a.y!=b.y) return a.y < b.y;else return a.idx > b.idx;}int lowbit(int x){return x&(-x);}void add(int x,int val,int n){while (x <= n){s[x]+=val;x += lowbit(x);}}int sum(int x){int ans = 0;while (x){ans += s[x];x -= lowbit(x);}return ans;}int main(){int t;//t = io.xint();scanf("%d", &t);while (t--){int n;//n = io.xint();scanf("%d", &n);int nx = 0, ny = 0;for (int i = 0; i < n; i++){int x1, y1, x2, y2;scanf("%d%d%d%d", &x1, &y1, &x2, &y2);//x1 = io.xint(); y1 = io.xint();//x2 = io.xint(); y2 = io.xint();if (x1 > x2) swap(x1, x2);if (y1 > y2) swap(y1, y2);posx[ny++] = x1;posx[ny++] = x2;if (x1 == x2){ax[nx].x1 = x1; ax[nx].y = y1;ax[nx++].idx = 1;ax[nx].x1 = x1; ax[nx].y = y2;ax[nx++].idx = -1;}else{ax[nx].x1 = x1; ax[nx].x2 = x2; ax[nx].y = y1;ax[nx++].idx = 0;}}sort(posx, posx+ny);int lenx = unique(posx, posx + ny) - posx;sort(ax, ax + nx, cmp);memset(s, 0, sizeof(s));ll ans = 0;for (int i = 0; i < nx; i++){if (ax[i].idx == 0){int l = lower_bound(posx, posx + lenx, ax[i].x1) - posx + 1;int r = lower_bound(posx, posx + lenx, ax[i].x2) - posx + 1;ans +=(ll) sum(r) - sum(l - 1);}else{int l = lower_bound(posx, posx + lenx, ax[i].x1) - posx + 1;add(l, ax[i].idx, lenx + 1);}}printf("%lld\n", ans);}return 0;}记住:要用ll 答案是会超int的。
0 0
- HDU 5862 Counting Intersections
- HDU - 5862 Counting Intersections
- HDU 5862 Counting Intersections
- HDU 5862 Counting Intersections
- HDU 5862 Counting Intersections 解题报告
- HDU 5862 Counting Intersections 扫描线
- 统计,优化(Counting Intersections,HDU 5862)
- HDU 5862 Counting Intersections(树状数组)
- Counting Intersections HDU
- hdu 5862 Counting Intersections多校第十场
- HDU 5862Counting Intersections (思维+树状数组)
- HDU 5862 Counting Intersections (扫描线+树状数组)
- hdu 5862 Counting Intersections 坐标离散化+树状数组
- hdu 5862 Counting Intersections 【线段树/树状数组+离散化】
- HDU 5862 Counting Intersections(离散化+树状数组)
- [未完]2016多校10 HDU 5862 Counting Intersections
- HDU 5862 Counting Intersections(BIT+扫描线)
- hdu 5862 Counting Intersections 扫描线+树状数组
- Qt QLabel 播放GIF动画
- Android Studio 简单介绍和使用问题小结
- C++动态内存分配
- FZU Problem 2238 Daxia & Wzc's problem(Lucas定理求组合数)
- PAT甲级.1019. General Palindromic Number (20)
- HDU 5862 Counting Intersections
- [CSAPP9]虚拟存储器/虚拟内存/VM学习笔记
- 点图层叠加与事件响应
- HDU 1049(贪心)
- Pandas 初探(一)
- 运行时栈帧之局部变量表
- android Eclipse安装SVN插件
- 数据结构实验之排序五:归并求逆序数
- 黑马Android:pull解析