矩阵面积并

裸裸的题目

但是按之前的搞法 重叠的边(cnt >= 2)在线段树里面有没有更新到子树

所以在线段树更新里面重了两个update

时间复杂度退化了 时间有点慢

太菜了。。。

  1 #include <bits/stdc++.h>
  2 #include <string.h>
  3 #include <iostream>
  4 #include <stdio.h>
  5 #define pb push_back
  6 #define fi first
  7 #define se second
  8 #define lson(r) (r<<1)
  9 #define rson(r) ((r<<1)|1)
 10 
 11 using namespace std;
 12 
 13 typedef long long ll;
 14 
 15 const int MAXN = 2e3+7;
 16 const int MAXV = 507;
 17 const int MAXE = 507;
 18 
 19 struct Seg
 20 {
 21     int cnt;
 22     double len;
 23 }seg[MAXN << 2];
 24 struct Line
 25 {
 26     double px1, px2, h;
 27     int x1, x2, flag;
 28     Line() {}
 29     Line(double px1, double px2, double h, int flag) : px1(px1), px2(px2), h(h), flag(flag) {}
 30     bool operator < (Line l) const
 31     {
 32         return h < l.h;
 33     }
 34 }line[MAXN];
 35 double hor[MAXN];
 36 int n;
 37 
 38 void push_up(int root, int l, int r)
 39 {
 40     if (seg[root].cnt >= 2) seg[root].len = hor[r+1] - hor[l];
 41     else if (l == r) seg[root].len = 0;
 42     else seg[root].len = seg[lson(root)].len + seg[rson(root)].len;
 43 }
 44 void update(int root, int l, int r, int ul, int ur, int addval)
 45 {
 46     if (l > ur || r < ul) return ;
 47     if (l >= ul && r <= ur)
 48     {
 49         seg[root].cnt += addval;
 50         //cout << l << " " << r << " " << seg[root].cnt << endl;
 51         int mid = (l+r) >> 1;
 52         if (l == r)
 53         {
 54             push_up(root, l, r);
 55             return ;
 56         }
 57                 //在区间内也更新
 58         update(lson(root), l, mid, ul, ur, addval); 
 59         update(rson(root), mid+1, r, ul, ur, addval);
 60         push_up(root, l, r);
 61         return;
 62     }
 63     int mid = (l+r) >> 1;
 64     update(lson(root), l, mid, ul, ur, addval);
 65     update(rson(root), mid+1, r, ul, ur, addval);
 66     push_up(root, l, r);
 67 }
 68 double query()
 69 {
 70     return seg[1].len;
 71 }
 72 int main()
 73 {
 74     //freopen("in.txt", "r", stdin);
 75     int T;
 76     scanf("%d", &T);
 77     while (T--)
 78     {
 79         scanf("%d", &n);
 80         int num = 0;
 81         memset(line, 0, sizeof(line));
 82         memset(hor, 0, sizeof(hor));
 83         memset(seg, 0, sizeof(seg));
 84         for (int i = 0; i < n; i++)
 85         {
 86             double x1, x2, y1, y2;
 87             scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
 88             hor[num] = x1;
 89             line[num++] = Line(x1, x2, y1, 1);
 90             hor[num] = x2;
 91             line[num++] = Line(x1, x2, y2, -1);
 92         }
 93         sort(line, line+num);
 94         sort(hor, hor+num);
 95         int m = unique(hor, hor+num) - hor;
 96         for (int i = 0; i < num; i++)
 97         {
 98             line[i].x1 = lower_bound(hor, hor+m, line[i].px1) - hor;
 99             line[i].x2 = lower_bound(hor, hor+m, line[i].px2) - hor;
100         }
101         double ans = 0, prey = -1;
102         for (int i = 0; i < num; i++)
103         {
104             if (prey == -1)
105             {
106                 prey = line[i].h;
107                 update(1, 0, m-2, line[i].x1, line[i].x2-1, line[i].flag);
108                 //cout << line[i].
109                 //x1 << " " << line[i].x2 << endl;
110                 continue;
111             }
112             double det = line[i].h - prey; 
113             double len = query();
114             ans += len * det;
115             prey = line[i].h;
116                 //cout << line[i].x1 << " " << line[i].x2 << endl;
117             update(1, 0, m-2, line[i].x1, line[i].x2-1, line[i].flag);
118             //return 0;
119         }
120         printf("%.2f\n", ans);
121     }
122     return 0;
123 }
View Code

 

posted @ 2018-03-29 16:03  Lorazepam  阅读(263)  评论(0编辑  收藏  举报