zoukankan      html  css  js  c++  java
  • hdu 1264【Counting Squares】

    一道线段树的题目,由于之前没怎么做过线段树的题目,这道题我觉得还是有点小难的

    首先将x坐标排序,然后找相邻的x坐标之间的最大y值和最小y值,这些矩形是要覆盖x,x+1坐标的,然后将yMax,yMin分散到线段树上,并用一个值标记哪些节点被分布了,最后查找一遍就可以了。

    代码如下:
      1 #include <iostream>
      2 #include <algorithm>
      3 #include <cstring>
      4 using namespace std;
      5 
      6 int X_axle[20000];
      7 
      8 struct node
      9 {
     10     int xLow,xHigh,yLow,yHigh;
     11 }Point[10010];
     12 struct node1
     13 {
     14     int low,high;
     15     int value;
     16 }seg[300];
     17 
     18 int xCount;
     19 int pCount;
     20 int num,ans;
     21 
     22 void init1(int r,int l,int h)
     23 {
     24     seg[r].value = 0;
     25     seg[r].low = l;
     26     seg[r].high = h;
     27 
     28     if(l == h)
     29     {
     30         return;
     31     }
     32     int m = (l+h) >> 1;
     33     init1(r*2,l,m);
     34     init1(r*2+1,m+1,h);
     35 }
     36 
     37 void init(int& x1,int& y1,int& x2,int& y2)
     38 {
     39     if(x1 > x2) swap(x1,x2);
     40     if(y1 > y2) swap(y1,y2);
     41     X_axle[xCount ++] = x1;
     42     X_axle[xCount ++] = x2;
     43     Point[pCount].xLow = x1;
     44     Point[pCount].xHigh = x2;
     45     Point[pCount].yLow = y1;
     46     Point[pCount ++].yHigh = y2;
     47 }
     48 
     49 bool cmp(const int& a,const int& b)
     50 {
     51     return a < b;
     52 }
     53 
     54 void seg_tree(int root,int low,int high)
     55 {
     56     if(seg[root].low == low && seg[root].high == high)
     57     {
     58         seg[root].value = num;
     59         return;
     60     }
     61     int mid = (seg[root].low + seg[root].high) >> 1;
     62     if(high <= mid )
     63     {
     64         seg_tree(root*2,low,high);
     65     }else if(low > mid)
     66     {
     67         seg_tree(root*2+1,low,high);
     68     }else
     69     {
     70         seg_tree(root*2,low,mid);
     71         seg_tree(root*2+1,mid+1,high);
     72     }
     73 }
     74 
     75 void get_ans(int root,int low,int high)
     76 {
     77     if(seg[root].value == num)
     78     {
     79         ans += (seg[root].high - seg[root].low +1);
     80         return;
     81     }
     82      if(seg[root].low == seg[root].high) return;
     83     int mid = (low + high) >> 1;
     84     get_ans(root*2,low,mid);
     85     get_ans(root*2+1,mid+1,high);
     86 }
     87 int main()
     88 {
     89     int x1,y1,x2,y2;
     90     init1(1,0,100);
     91     num = 0;
     92 
     93     while(cin >> x1 >> y1 >> x2 >> y2)
     94     {
     95         xCount = 0;
     96         pCount = 0;
     97         init(x1,y1,x2,y2);
     98 
     99         while(cin >> x1 >> y1 >> x2 >> y2)
    100         {
    101             if(x1 == -1 || x2 == -2)
    102             {
    103                 break;
    104             }
    105              init(x1,y1,x2,y2);
    106         }
    107         sort(X_axle,X_axle+xCount,cmp);
    108 
    109         int ans_value = 0;
    110         for(int i = 0;i < xCount-1;i ++)
    111         {
    112             if(X_axle[i] == X_axle[i+1]) continue;
    113             num ++;
    114 
    115             for(int j = 0;j < pCount;j ++)
    116             {
    117                 if(Point[j].xLow <= X_axle[i] && Point[j].xHigh >= X_axle[i+1])
    118                 {
    119                     seg_tree(1,Point[j].yLow+1,Point[j].yHigh);
    120                 }
    121             }
    122             ans = 0;
    123             get_ans(1,0,100);
    124             ans_value += ans * (X_axle[i+1] - X_axle[i]);
    125         }
    126         cout << ans_value << endl;
    127         if(x1 == -2)
    128         {
    129             break;
    130         }
    131     }
    132     return 0;
    133 }
  • 相关阅读:
    python 高阶函数
    python 列表
    python 模块
    浅谈HashMap原理,记录entrySet中的一些疑问
    extjs Tree中避免连续单击会连续请求服务器
    Java类的加载顺序
    mybatis中集成sharing-jdbc采坑
    tomcat采坑
    AQS原理
    Redis分布式锁原理
  • 原文地址:https://www.cnblogs.com/Shirlies/p/2794567.html
Copyright © 2011-2022 走看看