Hdu 3804 树链剖分 第5遍

不能原谅自己的错误。。还怀疑人家的数据错了,瞬间感觉自己弱爆了

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstring>
#include<algorithm>
#define lson id << 1
#define rson id << 1|1
#include<cstdio>
using namespace std;
const int M = 100008;
int father[M],dep[M],top[M],siz[M],ti[M],son[M];
int idx,tp = 0;
const int inf = 0x3ffff;
struct line_tree{
    int l,r,Max,Min,mark;
    int mid(){
        return (l+r)/2;
    }
}node[M*4];
struct {
    int head;
}H[M];
struct{
    int v,next;
}E[M*4];
void add(int u,int v){
    E[tp].v =  v;
    E[tp].next = H[u].head;
    H[u].head = tp++;
}
void init(){
    memset(E,-1,sizeof(E));
    memset(H,-1,sizeof(H));
    tp = 0;idx = 0;
}
void dfs_1(int u,int fa){
    son[u] = 0;siz[u] = 1;dep[u] = dep[fa] + 1; father[u] = fa;
    for(int i=H[u].head;i!=-1;i=E[i].next){
        int v = E[i].v;
        if(v == fa)continue;
        dfs_1(v,u);
        siz[u] += siz[v];
        if(siz[v] > siz[son[u]]) son[u] = v;
    }
}

void dfs_2(int u,int fa){
    top[u] = fa;
    ti[u] = idx++;
    if(son[u])dfs_2(son[u],fa);
    for(int i=H[u].head;i!=-1;i=E[i].next){
        int v = E[i].v;
        if(v == father[u]||v == son[u])continue;
        dfs_2(v,v);
    }
}
/* 线段树*/

void  build_tree(int id,int l,int r){
    node[id].l = l;node[id].r = r;
    node[id].Max  = -1;
    if(l == r)return;
    int mid = node[id].mid();
    build_tree(lson,l,mid);
    build_tree(rson,mid+1,r);
}

void push_up(int id){
    node[id].Max = max(node[lson].Max,node[rson].Max);
}
void  update(int id,int k,int w){

    if(node[id].l == k&&node[id].r == k){
       node[id].Max = w;
       return;
    }
    int mid = node[id].mid();
    if(k <= mid)update(lson,k,w);
    else update(rson,k,w);
    push_up(id);
}
int query(int id,int l,int r){
    if( node[id].l == l&&node[id].r == r)return node[id].Max;
    int mid= node[id].mid();
    if(r <=mid)return query(lson,l,r);
    else if(l > mid)return query(rson,l,r);
    else return max( query(lson,l,mid),query(rson,mid+1,r));
    push_up(id);
}
int findMax(int u,int v){
    int f1 = top[u];
    int f2 = top[v];
    int mmax = -1;
    while(f1 !=f2){
        if(dep[f1] < dep[f2]){
            swap(f1,f2);
            swap(u,v);
        }
        mmax = max(mmax,query(1,ti[f1],ti[u]));
        u = father[f1];f1 = top[u];
    }

    if(u == v)return mmax;
    if(dep[u] > dep[v])swap(u,v);
    mmax =  max(mmax,query(1,ti[son[u]],ti[v]));
    return mmax;
}
struct Node{
    int x,y,val;
}e[M];
struct xx{
    int x,y,i,d;
}h[M];
bool cmp(xx a,xx b){
    return a.y < b.y;
}
bool cmp1(Node a,Node b){
    return a.val < b.val;
}
bool cmp2(xx a,xx b){
    return a.i < b.i;
}
int main(){
    //freopen("input.txt","r",stdin);
    int n,m;
    int T;
    scanf("%d",&T);
    while(T--){
        init();
        scanf("%d",&n);
        for(int i=0;i<n-1;i++){
            scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].val);
            add(e[i].x,e[i].y);
            add(e[i].y,e[i].x);
        }
        dfs_1(1,1);
        dfs_2(1,1);
        build_tree(1,1,idx-1);
        scanf("%d",&m);
        for(int i=0;i<m;i++){
            scanf("%d%d",&h[i].x,&h[i].y);
            h[i].i = i;
        }
         sort(e,e+n-1,cmp1);
         sort(h,h+m,cmp);
         int  t = 0,x = 0;;
         while(t < m){
            while(h[t].y >= e[x].val &&x < n-1 ){
                if(dep[e[x].x] > dep[e[x].y])
                    swap(e[x].x,e[x].y);
                    update(1,ti[e[x].y],e[x].val);
                    x++;
            }
            h[t].d = findMax(h[t].x,1);
            t++;
         }
        sort(h,h+m,cmp2);
        for(int i=0;i<m;i++){
            printf("%d\n",h[i].d);
        }

    }

}

疑人家的数据错了。。瞬间感觉自己简直弱爆了

时间: 2024-10-12 12:21:43

Hdu 3804 树链剖分 第5遍的相关文章

hdu 3804树链剖分+离线操作

/* 树链刨分+离线操作 题意:给你一棵树,和询问x,y 从节点x--节点1的小于等于y的最大值. 解:先建一个空树,将树的边权值从小到大排序,将询问y按从小到大排序 对于每次询问y将小于等于y的边权值的边加入,在进行询问将结果储存最后输出即可 易错点:要考虑到节点1到节点1的情况需特判. */ #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #include<string

hdu 3966 树链剖分第3遍

真心不好意思说话,写的越多,各种问题就暴漏出来了,这次更离谱,什么错误都有...不过还是过了.也明白了代码以后要写规范性,不能想当然... #include<cstdio> #include<cstring> #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<algorithm> using namespace std;

hdu 5893 (树链剖分+合并)

List wants to travel Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 429    Accepted Submission(s): 92 Problem Description A boy named List who is perfect in English. Now he wants to travel an

hdu 5052 树链剖分

Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 982    Accepted Submission(s): 274 Problem Description Yaoge likes to eat chicken chops late at night. Yaoge has eaten too

hdu 5242 树链剖分找权值最大的前k条链

http://acm.hdu.edu.cn/showproblem.php?pid=5242 Problem Description It is well known that Keima Katsuragi is The Capturing God because of his exceptional skills and experience in ''capturing'' virtual girls in gal games. He is able to play k games sim

hdu 5274 树链剖分

Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1484    Accepted Submission(s): 347 Problem Description Dylans is given a tree with N nodes. All nodes have a value A[i].Nodes

hdu 4897 树链剖分(重轻链)

Little Devil I Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 894    Accepted Submission(s): 296 Problem Description There is an old country and the king fell in love with a devil. The devil

HDU 3966 (树链剖分+线段树)

Problem Aragorn's Story (HDU 3966) 题目大意 给定一颗树,有点权. 要求支持两种操作,将一条路径上的所有点权值增加或减少ai,询问某点的权值. 解题分析 树链剖分模板题. 实质上树链剖分进行了点对点的一次映射,保证了重链上的点在线段树上的位置是连续的. 树链剖分的两个性质(转): 性质1:如果(v,u)为轻边,则siz[u] * 2 < siz[v]: 性质2:从根到某一点的路径上轻链.重链的个数都不大于logn. 保证了一个区间的时间复杂度是log2(n).

HDU 5274(树链剖分)

树链剖分第一题QAQ,纪念下 #pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <cstdio> #include <cstring> using namespace std; typedef long long ll; const ll mod = 1e9 + 7; const int maxn = 1e5 + 10; #define