HDU 6162 Ch’s gift (树剖 + 离线线段树)

Ch’s gift

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 662    Accepted Submission(s): 229

Problem Description

Mr. Cui is working off-campus and he misses his girl friend very much. After a whole night tossing and turning, he decides to get to his girl friend‘s city and of course, with well-chosen gifts. He knows neither too low the price could a gift be since his girl friend won‘t like it, nor too high of it since he might consider not worth to do. So he will only buy gifts whose price is between [a,b].
There are n cities in the country and (n-1) bi-directional roads. Each city can be reached from any other city. In the ith city, there is a specialty of price ci Cui could buy as a gift. Cui buy at most 1 gift in a city. Cui starts his trip from city s and his girl friend is in city t. As mentioned above, Cui is so hurry that he will choose the quickest way to his girl friend(in other words, he won‘t pass a city twice) and of course, buy as many as gifts as possible. Now he wants to know, how much money does he need to prepare for all the gifts?

Input

There are multiple cases.

For each case:
The first line contains tow integers n,m(1≤n,m≤10^5), representing the number of cities and the number of situations.
The second line contains n integers c1,c2,...,cn(1≤ci≤10^9), indicating the price of city i‘s specialty.
Then n-1 lines follows. Each line has two integers x,y(1≤x,y≤n), meaning there is road between city x and city y.
Next m line follows. In each line there are four integers s,t,a,b(1≤s,t≤n;1≤a≤b≤10^9), which indicates start city, end city, lower bound of the price, upper bound of the price, respectively, as the exact meaning mentioned in the description above

Output

Output m space-separated integers in one line, and the ith number should be the answer to the ith situation.

Sample Input

5 3
1 2 1 3 2
1 2
2 4
3 1
2 5
4 5 1 3
1 1 1 1
3 5 2 3

Sample Output

7 1 4

【题意】给你一棵树,每个节点有一个权值,M次询问,给出u,v,a,b,求权值在区间[a,b]中的和。

【分析】数据实在是太水了,暴力都能过,这里说一下正确的树剖,离线往线段树插值得写法。将每个询问分成两部分,[1,b]-[1,a-1],放进一个      集合排序,从小到大取出,将节点权值同样 排序,若当前权值<=询问中的权值,则插入线段树。

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define lson(x) ((x<<1))
#define rson(x) ((x<<1)+1)
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 1e5+50;;
const int M = 17;
const int mod = 2520;
const int mo=123;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int dep[N],siz[N],fa[N],id[N],son[N],val[N],top[N];
int num,n,m;
ll ans[N];
vector<int>edg[N];
vector<pii>vec;
struct query{
    int u,v,x,id;
    bool operator <(const query &d)const{
        return x<d.x;
    }
}q[N*2];
void dfs1(int u, int f, int d) {
    dep[u] = d;
    siz[u] = 1;
    son[u] = 0;
    fa[u] = f;
    for (int v : edg[u]) {
        if (v == f) continue;
        dfs1(v, u, d + 1);
        siz[u] += siz[v];
        if (siz[son[u]] < siz[v])
            son[u] = v;
    }
}
void dfs2(int u, int tp) {
    top[u] = tp;
    id[u] = ++num;
    if (son[u]) dfs2(son[u], tp);
    for (int v : edg[u]) {
        if (v == fa[u] || v == son[u]) continue;
        dfs2(v, v);
    }
}

struct Tree {
    int l,r;
    ll sum;
};
Tree tree[4*N];
void pushup(int x) {
    tree[x].sum=tree[lson(x)].sum+tree[rson(x)].sum;
}
void build(int l,int r,int v) {
    tree[v].l=l;
    tree[v].r=r;
    if(l==r) {
        tree[v].sum=0;
        return ;
    }
    int mid=(l+r)>>1;
    build(l,mid,v*2);
    build(mid+1,r,v*2+1);
    pushup(v);
}
void update(int o,int v,int val) {
    if(tree[o].l==tree[o].r) {
        tree[o].sum= val;
        return ;
    }
    int mid = (tree[o].l+tree[o].r)/2;
    if(v<=mid)
        update(o*2,v,val);
    else
        update(o*2+1,v,val);
    pushup(o);
}
ll querySum(int x,int l,int r) {
    if (tree[x].l >= l && tree[x].r <= r) {
        return tree[x].sum;
    }
    int mid = (tree[x].l + tree[x].r) / 2;
    ll ans = 0;
    if (l <= mid) ans += querySum(lson(x),l,r);
    if (r > mid) ans += querySum(rson(x),l,r);
    return ans;
}
ll Qsum(int u,int v) {
    int tp1 = top[u], tp2 = top[v];
    ll ans = 0;
    while (tp1 != tp2) {
        if (dep[tp1] < dep[tp2]) {
            swap(tp1, tp2);
            swap(u, v);
        }
        ans +=querySum(1,id[tp1], id[u]);
        u = fa[tp1];
        tp1 = top[u];
    }
    if (dep[u] > dep[v])swap(u, v);
    ans +=querySum(1,id[u], id[v]);
    return ans;
}
void init(){
    for(int i=0;i<N;i++)edg[i].clear();
    met(tree,0);met(son,0);vec.clear();met(ans,0);
}
int main() {
    while(~scanf("%d%d",&n,&m)) {
        init();
        for(int i=1,x; i<=n; i++)scanf("%d",&x),vec.pb(mp(x,i));
        for(int i=1,u,v; i<n; i++) {
            scanf("%d%d",&u,&v);
            edg[u].pb(v);edg[v].pb(u);
        }
        num = 0;
        dfs1(1,0,1);
        dfs2(1,1);
        sort(vec.begin(),vec.end());
        for(int i=1;i<=m;i++){
            int x,y,a,b;
            scanf("%d%d%d%d",&x,&y,&a,&b);
            q[i]=query{x,y,a-1,-i};
            q[i+m]=query{x,y,b,i};
        }
        sort(q+1,q+2*m+1);
        int now=0;
        build(1,num,1);
        for(int i=1;i<=2*m;i++){
            while(now<n&&vec[now].first<=q[i].x){
                update(1,id[vec[now].second],vec[now].first);
                now++;
            }
            ans[abs(q[i].id)]+=Qsum(q[i].u,q[i].v)*(q[i].id>0?1:-1);
        }
        for(int i=1;i<=m;i++)printf("%lld%c",ans[i],i==m?‘\n‘:‘ ‘);
    }
    return 0;
}
时间: 2024-10-23 13:53:52

HDU 6162 Ch’s gift (树剖 + 离线线段树)的相关文章

hdu 6162 Ch’s gift(树链剖分+主席树)

题目链接:hdu 6162 Ch's gift 题意: 给你一棵树,树上每个点有一个权值,现在有m个询问,每次询问给你一个s,t,L,R,问你从s到t的路径上,权值在[L,R]内的总和为多少. 题解: 我感觉我写复杂了,用树链剖分来维护路径,然后用主席树来建立权值线段树乱搞. 1 #include<bits/stdc++.h> 2 #define mst(a,b) memset(a,b,sizeof(a)) 3 #define F(i,a,b) for(int i=(a);i<=(b);

HDU 6162 Ch’s gift (线段树+树链剖分)

题意:给定上一棵树,每个树的结点有一个权值,有 m 个询问,每次询问 s, t ,  a, b,问你从 s 到 t 这条路上,权值在 a 和 b 之间的和.(闭区间). 析:很明显的树链剖分,但是要用线段树来维护,首先先离线,然后按询问的 a 排序,每次把小于 a 的权值先更新上,然后再查询,这样就是区间求和了,算完小于a的,再算b的,最答案相减就好了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #inc

HDU - 4630 No Pain No Game(离线线段树)

No Pain No Game 题意:给出一个长度为n的1到n的排列 求区间两点gcd最大 思路: 因为题目没有更新 我们可以离线求解 对于每个查询按r排序 因为两点gcd一定会是两个数的约数 那么可以暴力插入a[i]的约数(当a[x]含有这个约数时 我们就能插入这个约数(x<i)) 我们使用last数组维护上一次出现这个约数的位置即可 查询:我们就只需要查询l~r出现过最大的约数就行 #include<bits/stdc++.h> using namespace std; #defin

hdu 4638 Group(莫队算法|离线线段树)

Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1323    Accepted Submission(s): 703 Problem Description There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is i and i-

HDU 4417 Super Mario(离线线段树or树状数组)

Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover. We regard the road to the boss's castle as a l

HDU ACM 4417 Super Mario 离线线段树

分析:离线线段树,把所有询问离线读入,然后按H从小到大排序.对于所有结点也按从小到大排序,然后根据查询的H,将比H小的点加入到线段树,最后就是一个区间求和.这题貌似也可以用划分树,树状数组等方法做. #include<iostream> #include<algorithm> using namespace std; #define N 100005 struct Tree { int left,right,cnt; } TREE[N<<2]; struct Query

hdu 5023 A Corrupt Mayor&#39;s Performance Art(线段树区间更新)

#include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> using namespace std; int tree[5001000],add[5001000]; int color[50]; int n,m; void pushup(int pos) { tree[pos]=tree[pos<<1]|tree[pos<<1|1]; //更新

hdu 5023 A Corrupt Mayor&#39;s Performance Art (线段树)

把求和操作改为或操作,就可以了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #define lson l,m,rt<<1 7 #define rson m+1,r,rt<<1|1 8 using namespace std; 9 const int ma

hdu 5023 A Corrupt Mayor&#39;s Performance Art(线段树)

题目链接 题意:有一个长度 n 的序列,初始染色2,有两种操作,P x ,y ,z,区间x---y染色为z,另一种Q x,y,查询区间 x -- y 有几种颜色,并输出,会覆盖 分析:lz[]为0,表示下面颜色不统一,统一是>0; f[]表示该节点下有多少种颜色,是30种颜色的二进制表示. 刚开始做时,用1<<i 不对,不知道为什么,int的范围按理不会超的.. 1 #include <iostream> 2 #include <cstdio> 3 #includ