2017 Multi-University Training Contest - Team 9 1002&&HDU 6162 Ch’s gift【树链部分+线段树】

时间:2022-05-07
本文章向大家介绍2017 Multi-University Training Contest - Team 9 1002&&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): 1354    Accepted Submission(s): 496

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

2017 Multi-University Training Contest - Team 9

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6162

分析:由于没有修改操作,一个显然的想法是离线处理所有问题 将询问拆成1-x,1-y,1-LCA(x,y),则处理的问题转化为从根到节点的链上的问题。 解决这个问题,我们可以在dfs时向treap插入当前的数,在退出时删除这个数,并且每次维护在该点上的答案。 当然也可以将所有的查询和点权排序,用树链剖分做这个题,在线段树上面插入就ok。

下面给出AC代码:

  1 #include<stdio.h>
  2 #include<vector>
  3 #include<string.h>
  4 #include<algorithm>
  5 using namespace std;
  6 #define LL long long
  7 vector<LL> G[100005];
  8 typedef struct
  9 {
 10     LL id;
 11     LL x, y;
 12     LL l, r;
 13 }Quer;
 14 Quer s[100005], cnt[100005];
 15 LL tre[445005];
 16 LL n, val[100005], son[100005], fa[100005], siz[100005], dep[100005];
 17 LL k, top[100005], rak[100005], id[100005], anl[100005], anr[100005];
 18 bool comp1(Quer a, Quer b)
 19 {
 20     if(a.l<b.l)
 21         return 1;
 22     return 0;
 23 }
 24 bool compr(Quer a, Quer b)
 25 {
 26     if(a.r<b.r)
 27         return 1;
 28     return 0;
 29 }
 30 void Sechs(LL u, LL p)
 31 {
 32     LL v, i;
 33     fa[u] = p;
 34     dep[u] = dep[p]+1;
 35     for(i=0;i<G[u].size();i++)
 36     {
 37         v = G[u][i];
 38         if(v==p)
 39             continue;
 40         Sechs(v, u);
 41         siz[u] += siz[v]+1;
 42         if(son[u]==0 || siz[v]>siz[son[u]])
 43             son[u] = v;
 44     }
 45 }
 46 void Sechr(LL u, LL p)
 47 {
 48     LL v, i;
 49     top[u] = p;
 50     rak[u] = ++k, id[k] = u;
 51     if(son[u]==0)
 52         return;
 53     Sechr(son[u], p);
 54     for(i=0;i<G[u].size();i++)
 55     {
 56         v = G[u][i];
 57         if(v==son[u] || v==fa[u])
 58             continue;
 59         Sechr(v, v);
 60     }
 61 }
 62 
 63 void Update(LL l, LL r, LL x, LL a, LL b)
 64 {
 65     LL m;
 66     if(l==r)
 67     {
 68         tre[x] += b;
 69         return;
 70     }
 71     m = (l+r)/2;
 72     if(a<=m)
 73         Update(l, m, x*2, a, b);
 74     else
 75         Update(m+1, r, x*2+1, a, b);
 76     tre[x] = tre[x*2]+tre[x*2+1];
 77 }
 78 LL Query(LL l, LL r, LL x, LL a, LL b)
 79 {
 80     LL m, sum = 0;
 81     if(l>=a && r<=b)
 82         return tre[x];
 83     m = (l+r)/2;
 84     if(a<=m)
 85         sum += Query(l, m, x*2, a, b);
 86     if(b>=m+1)
 87         sum += Query(m+1, r, x*2+1, a, b);
 88     return sum;
 89 }
 90 LL TreQuery(LL x, LL y)
 91 {
 92     LL sum, p1, p2;
 93     p1 = top[x], p2 = top[y], sum = 0;
 94     while(p1!=p2)
 95     {
 96         if(dep[p1]<dep[p2])
 97             swap(p1, p2), swap(x, y);
 98         sum += Query(1, n, 1, rak[p1], rak[x]);
 99         x = fa[p1], p1 = top[x];
100     }
101     if(dep[x]>dep[y])
102         swap(x, y);
103     sum += Query(1, n, 1, rak[x], rak[y]);
104     return sum;
105 }
106 
107 int main(void)
108 {
109     LL i, j, x, y, q;
110     while(scanf("%lld%lld", &n, &q)!=EOF)
111     {
112         for(i=1;i<=n;i++)
113             G[i].clear();
114         for(i=1;i<=n;i++)
115         {
116             scanf("%lld", &val[i]);
117             cnt[i].id = i;
118             cnt[i].r = val[i];
119         }
120         sort(cnt+1, cnt+n+1, compr);
121         for(i=1;i<=n-1;i++)
122         {
123             scanf("%lld%lld", &x, &y);
124             G[x].push_back(y);
125             G[y].push_back(x);
126         }
127         memset(siz, 0, sizeof(siz));
128         memset(son, 0, sizeof(son));
129         k = 0;
130         Sechs(1, 0);
131         Sechr(1, 1);
132         for(i=1;i<=q;i++)
133         {
134             scanf("%lld%lld%lld%lld", &s[i].x, &s[i].y, &s[i].l, &s[i].r);
135             s[i].id = i;
136         }
137         
138         sort(s+1, s+q+1, comp1);
139         memset(tre, 0, sizeof(tre));
140         j = 1;
141         for(i=1;i<=q;i++)
142         {
143             while(cnt[j].r<s[i].l && j<=n)
144             {
145                 Update(1, n, 1, rak[cnt[j].id], cnt[j].r);
146                 j += 1;
147             }
148             anl[s[i].id] = TreQuery(s[i].x, s[i].y);
149         }
150 
151         sort(s+1, s+q+1, compr);
152         memset(tre, 0, sizeof(tre));
153         j = 1;
154         for(i=1;i<=q;i++)
155         {
156             while(cnt[j].r<=s[i].r && j<=n)
157             {
158                 Update(1, n, 1, rak[cnt[j].id], cnt[j].r);
159                 j += 1;
160             }
161             anr[s[i].id] = TreQuery(s[i].x, s[i].y);
162         }
163 
164         printf("%lld", anr[1]-anl[1]);
165         for(i=2;i<=q;i++)
166             printf(" %lld", anr[i]-anl[i]);
167         printf("n");
168     }
169     return 0;
170 }