HDU 6162 Ch’s gift (2017多校9 - 树链剖分 + 线段树)

27 篇文章 0 订阅
4 篇文章 0 订阅

题意:

给一棵树, q 个查询,每个查询 输出 一条链中  点权 在指定范围内的  加和。

思路:

链中的操作,显然是用树链剖分。

但是有个很难处理的问题, 如和求一个链中的 点权 在制定范围内的加和。

用线段树就可以了。

维护区间内最小值,最大值, 和 总和。

如果区间最小值, 最大值 都在范围内,那么肯定整个区间都在制定范围内了, 就不用在向下找了。

如果区间 最大值 小于制定范围  或者最小值大于制定范围 ,也不用找了。

否则就看哪个合适 就找下去。

感觉会TLE的, 没想到还挺快的,800多ms就过了。。(maybe 数据水了?)


吐槽:

注意 :树链剖分 点权 和边权 写法稍微有些出入, 这是点权的, wa了好几次,看了板子才知道哪儿错了= =, 就是在树链剖分中查找最后有点小问题。。

其实点权和边权一样的, 只不过边权 把权值压在儿子上。 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;

const int maxn = 100000 + 10;
int a[maxn];
vector<long long> ans;
vector<int>g[maxn];
int pre[maxn], son[maxn], top[maxn], dep[maxn], siz[maxn], mp[maxn], fmp[maxn];
int cnt;

void dfs(int cur, int fa,int d){
    dep[cur] = d;
    siz[cur] = 1;
    pre[cur] = fa;
    for (int i = 0; i < g[cur].size(); ++i){
        int v = g[cur][i];
        if (v != fa){
            dfs(v, cur, d + 1);
            siz[cur] += siz[v];
            if (son[cur] == -1 || siz[v] > siz[son[cur] ]){
                son[cur] = v;
            }
        }
    }
}

void getpos(int cur, int sp){
    mp[cur] = ++cnt;
    fmp[cnt] = cur;
    top[cur] = sp;
    if (son[cur] == -1) return;
    getpos(son[cur], sp);
    for (int i = 0; i < g[cur].size(); ++i){
        int v = g[cur][i];
        if (v != pre[cur] && v != son[cur]){
            getpos(v, v);
        }
    }
}

struct node{
    int Min;
    int Max;
    long long sum;

}nod[maxn<<2];
void build(int l,int r,int o){

    int m = l + r >> 1;
    if (l == r){
        nod[o].Min = nod[o].Max = nod[o].sum = a[fmp[l] ];
        return;

    }
    int lson = o <<1;
    int rson = o << 1| 1;
    build(l, m, lson);
    build(m+1, r, rson);
    nod[o].Min = min(nod[lson].Min, nod[rson].Min);
    nod[o].Max = max(nod[lson].Max, nod[rson].Max);
    nod[o].sum = nod[lson].sum + nod[rson].sum;
}

long long query(int L,int R, int a,int b, int l,int r,int o){
    if (L <= l && r <= R){
        if (nod[o].Min >= a && nod[o].Max <= b){
            return nod[o].sum;
        }
        if (nod[o].Min > b || nod[o].Max < a) return 0LL;
        int m = l + r >> 1;
        int lson = o << 1;
        int rson = o << 1 | 1;
        return query(L, R, a, b, l, m, lson) + query(L, R, a, b, m+1, r, rson);
    }

    int m = l + r >> 1;
    long long ans = 0;
    if (m >= L){
        ans += query(L, R, a, b, l, m, o<<1);
    }
    if (m < R){
        ans += query(L, R, a, b, m+1, r, o<<1|1);
    }
    return ans;
}
long long solve(int u, int v,int a,int b,int n){
    long long ans = 0;


    int f1 = top[u];
    int f2 = top[v];
    while(f1 != f2){
        if (dep[f1] < dep[f2]){
            swap(f1, f2);
            swap(u, v);
        }
        ans += query(mp[f1], mp[u], a, b, 1, n, 1);
        u = pre[f1];
        f1 = top[u];
    }
    if (dep[u] > dep[v]){
        swap(u, v);
    }
    ans += query(mp[u], mp[v], a, b, 1, n, 1); /// 点权不用在分u != v了,这里wa了几次。。。
    return ans;
}

int main(){
    int n, m;
    while(~scanf("%d %d",&n, &m)){
        ans.clear();
        for (int i = 1; i <= n; ++i){
            scanf("%d",&a[i]);
            g[i].clear();
            son[i] = -1;
            cnt = 0;
        }

        for (int i = 1; i < n; ++i){
            int x, y;
            scanf("%d %d",&x, &y);
            g[x].push_back(y);
            g[y].push_back(x);
        }

        dfs(1, -1, 0);
        getpos(1, 1);


        build(1, n, 1);
        for (int i = 0; i < m; ++i){
            int s, t, aa, b;
            scanf("%d %d %d %d", &s, &t, &aa, &b);
            long long tmp;
            tmp = solve(s, t, aa, b, n);
            ans.push_back(tmp);
        }

        for (int i = 0; i < ans.size(); ++i){
            if (i) putchar(' ');
            printf("%I64d", ans[i]);
        }
        putchar('\n');
    }
    return 0;
}

Ch’s gift

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


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
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6170  6169  6168  6167  6166 
 

Statistic |  Submit |  Discuss |  Note

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值