zoukankan      html  css  js  c++  java
  • 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
    ", ans);
      }
      return 0;
    }
    

      

  • 相关阅读:
    「题解」洛谷 P3487 [POI2009]ARC-Architects
    「题解」洛谷 P3512 [POI2010]PIL-Pilots
    CentOS中安装的Gitlab忘记管理员密码怎样重置密码
    Jenkins中安装Credentials Binding插件时提示:Failed to load: SSH Credentials Plugin (1.18.2),Jenkins(2.282)or higher required
    Jenkins中安装Role-based Authorization Strategy插件来实现用户角色权限管理
    信息系统项目管理师-项目范围管理考点笔记
    Jenkins修改插件下载地址为国内下载地址与安装中文插件
    CentOS中Jenkins的下载、安装、配置与启动(图文教程)
    Jenkins启动时提示:Starting Jenkins Jenkins requires Java8 or later, but you are running 1.7.0
    Gitlab怎样添加组、创建用户、创建项目与推送代码
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6724160.html
Copyright © 2011-2022 走看看