您的位置:首页 > 大数据 > 人工智能

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

2017-09-02 17:50 661 查看

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


[align=left]Problem Description[/align]
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?

[align=left]Input[/align]
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

[align=left]Output[/align]
Output m space-separated integers in one line, and the ith number should be the answer to the ith situation.

[align=left]Sample Input[/align]
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


[align=left]Sample Output[/align]

7 1 4

[align=left]Source[/align]
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代码:

#include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL long long
vector<LL> G[100005];
typedef struct
{
LL id;
LL x, y;
LL l, r;
}Quer;
Quer s[100005], cnt[100005];
LL tre[445005];
LL n, val[100005], son[100005], fa[100005], siz[100005], dep[100005];
LL k, top[100005], rak[100005], id[100005], anl[100005], anr[100005];
bool comp1(Quer a, Quer b)
{
if(a.l<b.l)
return 1;
return 0;
}
bool compr(Quer a, Quer b)
{
if(a.r<b.r)
return 1;
return 0;
}
void Sechs(LL u, LL p)
{
LL v, i;
fa[u] = p;
dep[u] = dep[p]+1;
for(i=0;i<G[u].size();i++)
{
v = G[u][i];
if(v==p)
continue;
Sechs(v, u);
siz[u] += siz[v]+1;
if(son[u]==0 || siz[v]>siz[son[u]])
son[u] = v;
}
}
void Sechr(LL u, LL p)
{
LL v, i;
top[u] = p;
rak[u] = ++k, id[k] = u;
if(son[u]==0)
return;
Sechr(son[u], p);
for(i=0;i<G[u].size();i++)
{
v = G[u][i];
if(v==son[u] || v==fa[u])
continue;
Sechr(v, v);
}
}

void Update(LL l, LL r, LL x, LL a, LL b)
{
LL m;
if(l==r)
{
tre[x] += b;
return;
}
m = (l+r)/2;
if(a<=m)
Update(l, m, x*2, a, b);
else
Update(m+1, r, x*2+1, a, b);
tre[x] = tre[x*2]+tre[x*2+1];
}
LL Query(LL l, LL r, LL x, LL a, LL b)
{
LL m, sum = 0;
if(l>=a && r<=b)
return tre[x];
m = (l+r)/2;
if(a<=m)
sum += Query(l, m, x*2, a, b);
if(b>=m+1)
sum += Query(m+1, r, x*2+1, a, b);
return sum;
}
LL TreQuery(LL x, LL y)
{
LL sum, p1, p2;
p1 = top[x], p2 = top[y], sum = 0;
while(p1!=p2)
{
if(dep[p1]<dep[p2])
swap(p1, p2), swap(x, y);
sum += Query(1, n, 1, rak[p1], rak[x]);
x = fa[p1], p1 = top[x];
}
if(dep[x]>dep[y])
swap(x, y);
sum += Query(1, n, 1, rak[x], rak[y]);
return sum;
}

int main(void)
{
LL i, j, x, y, q;
while(scanf("%lld%lld", &n, &q)!=EOF)
{
for(i=1;i<=n;i++)
G[i].clear();
for(i=1;i<=n;i++)
{
scanf("%lld", &val[i]);
cnt[i].id = i;
cnt[i].r = val[i];
}
sort(cnt+1, cnt+n+1, compr);
for(i=1;i<=n-1;i++)
{
scanf("%lld%lld", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
memset(siz, 0, sizeof(siz));
memset(son, 0, sizeof(son));
k = 0;
Sechs(1, 0);
Sechr(1, 1);
for(i=1;i<=q;i++)
{
scanf("%lld%lld%lld%lld", &s[i].x, &s[i].y, &s[i].l, &s[i].r);
s[i].id = i;
}

sort(s+1, s+q+1, comp1);
memset(tre, 0, sizeof(tre));
j = 1;
for(i=1;i<=q;i++)
{
while(cnt[j].r<s[i].l && j<=n)
{
Update(1, n, 1, rak[cnt[j].id], cnt[j].r);
j += 1;
}
anl[s[i].id] = TreQuery(s[i].x, s[i].y);
}

sort(s+1, s+q+1, compr);
memset(tre, 0, sizeof(tre));
j = 1;
for(i=1;i<=q;i++)
{
while(cnt[j].r<=s[i].r && j<=n)
{
Update(1, n, 1, rak[cnt[j].id], cnt[j].r);
j += 1;
}
anr[s[i].id] = TreQuery(s[i].x, s[i].y);
}

printf("%lld", anr[1]-anl[1]);
for(i=2;i<=q;i++)
printf(" %lld", anr[i]-anl[i]);
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐