hdu6162 Ch’s gift

来源:互联网 发布:汤姆大叔 javascript 编辑:程序博客网 时间:2024/06/06 01:30

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这条链上权值在ab之间的权值和。
思路:还是菜啊。这么多人做的题目比赛的时候还是没做出来QWQ。比赛的时候考虑了离线查询,但是还是在考虑树剖在维护u,v这条链的同时去维护在ab之间的权值和,这踏马好像并不行。。官方题解给了用考虑lca每个答案由三个部分组成1->u,1->v,1->lca(u,v)。官方说用treap维护。。。我只会splay。一统瞎维护之后炸了QWQ,想了想splay维护这个感觉不大合适。于是用了棵权值线段树。权值1e9所以我们离散化一下。用了线段树之后。。代码变短了,还A了,可棒了

#include<bits/stdc++.h>using namespace std;//thanks to pyf ...//thanks to qhl ...const int N = 1e5 + 7;struct Edge{    int u, v, next;} edge[N * 2];struct Query{    int l, r, u, t;};struct Tree{    int l, r, step;    long long sum;} t[N * 3 * 4];long long vv[N], ac[N * 3];int head[N], dep[N], fa[N][21];int len = 0, tot = 0;vector<Query> Q[N];long long ans[N];void init(){    len = 0, tot = 0;    memset(head, -1, sizeof(head));    for (int i = 0; i < N; i++)        Q[i].clear();}void add_edge(int u, int v){    edge[tot] = {u, v, head[u]};    head[u] = tot ++;}void dfs_lca(int u, int Fa, int d){    dep[u] = d, fa[u][0] = Fa;    for (int i = 1; i < 21; i++)        fa[u][i] = fa[fa[u][i - 1]][i - 1];    for (int i = head[u]; ~i; i = edge[i].next)    {        int v = edge[i].v;        if (v == Fa)            continue;        dfs_lca(v, u, d + 1);    }}int lca(int u, int v){    if (dep[u] < dep[v])        swap(u, v);    for (int i = 20; i >= 0; i--)        if (dep[fa[u][i]] >= dep[v])            u = fa[u][i];    if (u == v)        return u;    for (int i = 20; i >= 0; i--)        if (fa[u][i] != fa[v][i])            u = fa[u][i], v = fa[v][i];    return fa[u][0];}//treevoid debug(int step){    if (t[step].l == t[step].r)    {        cout << t[step].sum << " ";        return ;    }    debug(step * 2);    debug(step * 2 + 1);}void push_up(int step){    t[step].sum = t[step * 2].sum + t[step * 2 + 1].sum;}void build(int l, int r, int step){    t[step].l = l , t[step].r = r, t[step].sum = 0;    if (l == r)        return;    int mid = (l + r) / 2;    build(l, mid, step * 2);    build(mid + 1, r, step * 2 + 1);    push_up(step);}void update(int x, long long val, int step){    if (t[step].l == t[step].r)    {        t[step].sum += val;        return;    }    int mid = (t[step].l + t[step].r) / 2;    if (x <= mid)        update(x, val, step * 2);    else        update(x, val, step * 2 + 1);    push_up(step);}long long query(int l, int r, int step){    if (t[step].l == l && t[step].r == r)        return t[step].sum;    int mid = (t[step].l + t[step].r) / 2;    if (r <= mid)        return query(l, r, step * 2);    else if (l > mid)        return query(l, r, step * 2 + 1);    else        return query(l, mid, step * 2) + query(mid + 1, r, step * 2 + 1);}int get_id(long long val, int len){    return lower_bound(ac, ac + len, val) - ac + 1;}void get_ans(int u, int Fa){    update(get_id(vv[u], len), vv[u], 1);    for (int i = 0; i < Q[u].size(); i++)    {        Query temp = Q[u][i];        int l = temp.l , r = temp.r , from = temp.u, t = temp.t;        if (t == 2)            ans[from] -= 2 * query(get_id(l, len), get_id(r, len), 1);        else            ans[from] += query(get_id(l, len), get_id(r, len), 1);    }    for (int i = head[u]; ~i; i = edge[i].next)    {        int v = edge[i].v;        if (v == Fa)            continue;        get_ans(v, u);    }    update(get_id(vv[u], len), -vv[u], 1);}int main(){    int n, m;    while (scanf("%d%d", &n, &m) == 2)    {        init();        for (int i = 1; i <= n; i++)        {            scanf("%lld", vv + i);            ac[len ++ ]  = vv[i];        }        for (int i = 1; i < n; i++)        {            int u, v;            scanf("%d%d", &u, &v);            add_edge(u, v);            add_edge(v, u);        }        dfs_lca(1, 1, 1);        for (int i = 0; i < m; i++)        {            int u, v, a, b;            scanf("%d%d%d%d", &u, &v, &a, &b);            ac[len ++] = a, ac[len++] = b;            int anc = lca(u, v);            Q[u].push_back((Query) {a, b, i, 0});            Q[v].push_back((Query) {a, b, i, 1});            Q[anc].push_back((Query) {a, b, i, 2});            if (vv[anc] >= a && vv[anc] <= b)                ans[i] = vv[anc];            else                ans[i] = 0;        }        sort(ac, ac + len);        len = unique(ac, ac + len) - ac;        build(1, len + 1, 1);        get_ans(1, 1);        for (int i = 0; i < m; i++)        {            if (i)                printf(" ");            printf("%lld", ans[i]);        }        printf("\n");    }}
原创粉丝点击