POJ 1436 Horizontally Visible Segments(线段树区间修改)

题目链接:点击打开链接

题意:n条竖直线段,如果两条线段之间可见(即可以用一条水平线段连接而不触碰其他线段),则称它们可见。   如果三条线段任意两条都可见, 则称它们为a triangle of segments, 求a triangle of segments的个数

思路: 一开始真没想到n^3的复杂度可以过。。。  如果这样的话, 问题的关键就是怎样判断任意两个线段是否可见。

那么如果对y坐标建立区间, 这就成了线段树的区间覆盖问题。

另外, 由于是用点代替线段, 所以对于”可见“这个问题, 就会出现问题, 比如  有三条线段[1,4]、[1,2]、[3,4], 则在[1,4]区间中可以看见3条线段, 即可以通过[2,3]的空隙看见第一条线段。   解决方法很简单, 把坐标扩大2倍就行了,这样[1,2]变成[2,4],[3,4]变成[6,8],空出了一个5。

细节参见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int mod = 1000000000 + 7;
const int INF = 1000000000;
const int maxn = 8000 + 10;
int T,n,t,q,l,r,c,col[maxn<<3],y[maxn<<1];
bool vis[maxn][maxn];
struct node {
    int ly, ry, x;
    node(int l=0, int r=0, int xx=0):ly(l),ry(r),x(xx) {}
    bool operator < (const node& rhs) const {
        return x < rhs.x || (x == rhs.x && ly < rhs.ly);
    }
}a[maxn];
void pushdown(int o) {
    if(col[o] >= 0) {
        col[o<<1] = col[o<<1|1] = col[o];
        col[o] = -1;
    }
}
void update(int L, int R, int c, int l, int r, int o) {
    int m = (l + r) >> 1;
    if(L <= l && r <= R) {
        col[o] = c;
        return ;
    }
    pushdown(o);
    if(L <= m) update(L, R, c, l, m, o<<1);
    if(m < R) update(L, R, c, m+1, r, o<<1|1);
}
void query(int L, int R, int id, int l, int r, int o) {
    int m = (l + r) >> 1;
    if(col[o] >= 0) {
        vis[id][col[o]] = true; return ;
    }
    if(l == r) return ;
    if(L <= m) query(L, R, id, l, m, o<<1);
    if(m < R) query(L, R, id, m+1, r, o<<1|1);
}
int main() {
    scanf("%d",&T);
    while(T--) {
        scanf("%d",&n);
        int m = 0;
        for(int i=0;i<n;i++) {
            scanf("%d%d%d",&a[i].ly, &a[i].ry, &a[i].x);
            m = max(m, max(a[i].ly, a[i].ry));
        }
        sort(a, a+n);
        memset(col, -1, sizeof(col));
        memset(vis, false, sizeof(vis));
        for(int i=0;i<n;i++) {
            int l = a[i].ly;
            int r = a[i].ry;
            query(2*l, 2*r, i, 0, 2*m, 1);
            update(2*l, 2*r, i, 0, 2*m, 1);
        }
        int ans = 0;
        for(int i=n-1;i>=0;i--) {
            for(int j=i-1;j>=0;j--) {
                if(vis[i][j]) {
                    for(int k=j-1;k>=0;k--) {
                        if(vis[i][k] && vis[j][k]) ++ans;
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-06 10:36:42

POJ 1436 Horizontally Visible Segments(线段树区间修改)的相关文章

(中等) POJ 1436 Horizontally Visible Segments , 线段树+区间更新。

Description There is a number of disjoint vertical line segments in the plane. We say that two segments are horizontally visible if they can be connected by a horizontal line segment that does not have any common points with other vertical segments.

POJ 1436 Horizontally Visible Segments (线段树&amp;#183;区间染色)

题意   在坐标系中有n条平行于y轴的线段  当一条线段与还有一条线段之间能够连一条平行与x轴的线不与其他线段相交  就视为它们是可见的  问有多少组三条线段两两相互可见 先把全部线段存下来  并按x坐标排序  线段树记录相应区间从右往左当前可见的线段编号(1...n)  超过一条就为0  然后从左往右对每条线段  先查询左边哪些线段和它是可见的  把可见关系存到数组中  然后把这条线段相应区间的最右端可见编号更新为这条线段的编号  最后暴力统计有多少组即可了 #include <cstdio>

POJ 1436 Horizontally Visible Segments(线段树建图+枚举)

题目连接:http://poj.org/problem?id=1436 题意:给一些线段,每个线段有三个值y1, y2, x代表起点为(x, y1),终点为(x, y2)的线段.当从一个线段可以作水平线到另一个线段并且不穿过其他线段时,就称这两个线段时水平可见的.当三个线段可以两两水平可见,就称为形成一个线段三角.问:在这些线段中有多少个这样的线段三角? 分析:可以把每条线段看做是一个点,如果它和其他线段是水平可见的,就将这两个点相连,由于是无向图,就是你能看到我,我也能看到你,所以需要连接两次

POJ 1436 Horizontally Visible Segments(线段树)

POJ 1436 Horizontally Visible Segments 题目链接 线段树处理染色问题,把线段排序,从左往右扫描处理出每个线段能看到的右边的线段,然后利用bitset维护枚举两个线段,找出另一个两个都有的线段 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <bitset> #include <vector> using namesp

POJ - 1436 Horizontally Visible Segments

Description There is a number of disjoint vertical line segments in the plane. We say that two segments are horizontally visible if they can be connected by a horizontal line segment that does not have any common points with other vertical segments.

poj 2777 Count Color(线段树区间修改)

题目链接:http://poj.org/problem?id=2777 题目意思:就是问你在询问的区间里有几种不同的颜色 思路:这题和一般的区间修改差不多,但是唯一不同的就是我们要怎么计算有种颜色,所以这时候我们就需要把延时标记赋予不同的意义,当某段区间有多种颜色时就赋值为-1,当为一种颜色时就把它赋值为这个颜色的号数.这儿我们要怎么统计询问区间不同的颜色数叻,为了不重复计算同一种颜色,那么我们就需要用一个数组来标记计算过的颜色,当我们下次遇到时就不需要再次计算了.... 代码核心处就在计数那儿

poj 1436 Horizontally Visible Segments(线段树、区间覆盖)

Horizontally Visible Segments Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4645   Accepted: 1706 Description There is a number of disjoint vertical line segments in the plane. We say that two segments are horizontally visible if they

POJ 1436——Horizontally Visible Segments(线段树,区间染色+暴力)

Horizontally Visible Segments Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4130   Accepted: 1511 Description There is a number of disjoint vertical line segments in the plane. We say that two segments are horizontally visible if they

poj 2528 Mayor&#39;s posters 线段树区间更新

Mayor's posters Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at al