Poj1436horizontally visible segments line segment tree

Source: Internet
Author: User
#include <cstdio>#include <cstring>#include <algorithm>#include <climits>#include <string>#include <iostream>#include <map>#include <cstdlib>#include <list>#include <set>#include <queue>#include <stack>#include<math.h>using namespace std;#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1const int maxn = 8000 + 10;bool Hash[maxn][maxn];struct Node{    int a; int b; int c;}node[maxn];int color[maxn * 10];int cmp(const Node &a, const Node &b){    return a.c < b.c;}void down(int rt){    if (~color[rt]){        color[rt << 1] = color[rt << 1 | 1] = color[rt];        color[rt] = -1;    }}void build(int l, int r, int rt){    color[rt] = -1;    if (l == r) return;    int mid = (l + r) >> 1;    build(lson);    build(rson);}void update(int L, int R, int add, int l, int r, int rt){    if (L <= l&&r <= R){        color[rt] = add; return;    }    down(rt);    int mid = (l + r) >> 1;    if (L <= mid) update(L, R, add, lson);    if (R > mid) update(L, R, add, rson);}void ask(int L, int R, int add, int l, int r, int rt){    if (~color[rt]){        Hash[add][color[rt]] = Hash[color[rt]][add] = add;        return;    }    if (l == r) return;    int mid = (l + r) >> 1;    if (L <= mid) ask(L, R, add, lson);    if (R > mid) ask(L, R, add, rson);}int main(){    int Icase, n;    cin >> Icase;    while (Icase--){        cin >> n;        int ans = 0;        int Max = 0;        memset(Hash, 0, sizeof(Hash));        for (int i = 0; i < n; i++){            int a; int b; int c;            scanf("%d%d%d", &a, &b, &c);            node[i].a = a * 2; node[i].b = b * 2; node[i].c = c;            if (node[i].b>Max) Max = node[i].b;        }        sort(node, node + n, cmp);        build(0, Max, 1);        for (int i = 0; i < n; i++){            ask(node[i].a, node[i].b, i, 0, Max, 1);            update(node[i].a, node[i].b, i, 0, Max, 1);        }        for (int i = 0; i < n; i++){            for (int j = i + 1; j < n; j++){                if (!Hash[i][j]) continue;                for (int k = j + 1; k < n; k++){                    if (Hash[i][k] && Hash[j][k])ans++;                }            }        }        cout << ans << endl;    }    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.