The diameter of the week tree of hiho11

Source: Internet
Author: User

 

Find any point as the root, then find the farthest point from this root, and then use this point as the root, and then find the farthest point from this root. The two distance and are the diameter of the tree.

#include<iostream>#include<cstdio>#include<cstring>#include<map>#include<vector>#include<stdlib.h>using namespace std;typedef long long LL;#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1const int maxn = 1000000 + 1000;int sum[maxn << 2];const int INF = 1<<30;int ncnt;int val[maxn];int pos[maxn];void up(int rt){    sum[rt] = min(sum[rt << 1], sum[rt << 1 | 1]);}void build(int l, int r, int rt){    sum[rt] = INF;    if (l == r){        return;    }    int mid = (l + r) >> 1;    build(lson);    build(rson);    up(rt);}void update(int key, int l, int r, int rt){    if (l == r){        sum[rt] = key; return;    }    int mid = (l + r) >> 1;    if (key <= mid) update(key, lson);    else update(key, rson);    up(rt);}int ask(int L, int R, int l, int r, int rt){    if (L <= l&&r <= R) return sum[rt];    int ans = INF;    int mid = (l + r) >> 1;    if (L <= mid) ans = min(ans, ask(L, R, lson));    if (R > mid) ans = min(ans, ask(L, R, rson));    return ans;}void gao(int x){    int Min = INF; int ans=INF;    for (int i = ncnt - 1; i >= 1; i--){        if (val[i] % x == 0) {            printf("%d\n", i); return ;        }        if (val[i] % x < Min){            Min = val[i] % x;            ans = i;        }    }    printf("%d\n", ans);}void gao1(int x){    int l = 0; int r = x - 1; int ans = -1;    while (l <= maxn){        if (r > maxn) r = maxn;        int temp = ask(l, r, 0, maxn, 1);        if(temp!=INF){        if (ans==-1||temp%x < ans%x) ans = temp;        else if (temp%x == ans%x&&pos[temp]>pos[ans])            ans = temp;        }        l += x; r += x;    }    printf("%d\n", pos[ans]);}int main(){    char str[100];    int k;    int cnt = 0;    int T;    while (scanf("%d",&T),T){        if(cnt) puts("");        ncnt = 1;        build(0, maxn, 1);        printf("Case %d:\n", ++cnt);        while (T--){            scanf("%s%d", str, &k);            if (str[0] == ‘B‘){                val[ncnt] = k;                pos[k] = ncnt++;                update(k, 0, maxn, 1);            }            else{                if(ncnt==1){                    printf("-1\n");                }                else                if (k < 10000) gao(k);                else gao1(k);            }        }    }    return 0;}

 

The diameter of the week tree of hiho11

Related Article

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.