BNU-12873-generations line segment tree

Source: Internet
Author: User

After a long time on the Line Segment tree, I finally got another line segment tree.

Question:

A mother dragon keeps giving birth to children. Ask what is the difference between the minimum descendant and his algebra that each dragon has.

This is the interval modification, and the interval is the greatest value. I made a few calls and wrote a note in the class. Finally. Push_up and push_dn are incorrect.

Others are convenient graph writing methods, such as unique and lowerbound.

#define maxn 141001#define lson l, m , rt << 1#define rson m + 1, r, rt << 1 |1#define ls rt << 1#define rs rt << 1 | 1int X[maxn],n, mmax[maxn << 2], T, num[maxn], all,xsub, cover[maxn << 2];struct Node{    int s; int e;}p[maxn];#include <cstdio>#include <vector>#include <cmath>#include <algorithm>using namespace std;vector <int> vec[maxn];void build(int l, int r, int rt){    mmax[rt] = 0;    cover[rt] = -1;    if(l == r) return ;    int m = (l + r) >> 1;    build(lson); build(rson);}void push_up(int rt){    mmax[rt] = max(mmax[ls],mmax[rs]);    if(cover[ls] == cover[rs]){        cover[rt] = cover[ls];    }else{        cover[rt] = -1;    }}void push_dn(int rt){    if(cover[rt] != -1){        mmax[ls] = mmax[rs] = mmax[rt];        cover[ls] = cover[rs] = cover[rt];        cover[rt] = -1;    }}void update(int L, int R, int val, int l ,int r, int rt){    if(L <= l && r <= R && cover[rt] >= 0){        cover[rt] = max(val, cover[rt]);        mmax[rt] = cover[rt];        return ;    }    if(l == r) {        mmax[rt] = cover[rt] = val;        return ;    }    int m = (l + r) >> 1;    push_dn(rt);    if(L <= m) update(L,R,val,lson);    if(R > m) update(L, R, val, rson);    push_up(rt);}int query(int L, int R, int l ,int r, int rt){    if(L <= l && r <= R){        return mmax[rt];    }int m = (l + r) >> 1;    push_dn(rt);    int ans = 0;    if(L <= m) ans = query(L,R,lson);    if(R > m) ans = max(ans, query(L,R,rson));    return ans;}void solve(int pa, int level){    for(int i = 0; i < vec[pa].size(); i ++){        solve(vec[pa][i], level + 1);    }    int aa = lower_bound(X, X + xsub,p[pa].s) - X;    int bb = lower_bound(X, X + xsub,p[pa].e) - X;    update(aa, bb, level, 0, all, 1);    num[pa] = query(aa, bb, 0, all, 1) - level;}#include <cstring>int main(){    scanf("%d",&T);    while(T --){        scanf("%d",&n);        int a, b,pp;        xsub = 0;        for(int i = 0; i < n; i++)   vec[i].clear();        for(int i = 0; i < n; i++) {            scanf("%d%d%d",&pp, & a, & b);            if(pp != -1)                vec[pp].push_back(i);            p[i].s = a, p[i].e = b;            X[xsub ++] = a, X[xsub ++] = b;        }        sort(X, X + xsub);        all = xsub;        build(0, all, 1);        solve(0, 0);        for(int i = 0; i < n - 1; i ++){            printf("%d ", num[i]);        }printf("%d\n",num[n - 1]);    }    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.