L - Ch’s gift HDU - 6162 树链剖分(离线)

博客围绕Ch’s gift题目展开,给出了题目链接,包含Problem Description、Input等内容,阐述了题意和题解,还提供了AC代码以及TLE代码(树链剖分+主席树),涉及数据结构与算法相关知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ch’s gift

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

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=6162

 

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

 

题意

给你一棵树,每个点有点权,每次求一条路径点权大于等于a小于等于b的点权和,即求vi的和(a<=vi<=b)
 

题解

我在上次南昌网络赛遇到几乎一模一样的题,当时直接树链剖分+主席数过了,结果这次T了,而且这题数据暴力都能过,我还很不服气的拿暴力对拍,结果真的真的比暴力慢了三四倍吧,想哭了。
在这题上耗了两小时,终于选择向大佬低头,去看题解了,果然我还是太弱了。

以上来自以为蒟蒻的内心独白,接下来假装自己想出来的,讲波题解:
 
我们令 sum(x)表示小于等于x的权值和,那么ans=sum(b)-sum(a-1)。当然对于每条路径,sum(x)都不一样 ,真的吗
对于一条路径 (l,r,a,b)先拆成两个路径,即(l,r,a-1,-1)和(l,r,b,1),并统称为 (l,r,val,id),分别求答案,最后相减即可。
然后我们问题转化成了求路径小于等于val的权值和。
对两条路径(l1,r1,val1,id1) 和(l2,r2,val2,id2) ,如果val1<val2那么第一条路径的答案被第二条路径的答案包含。
接下来定义 get_sum(u,v)为点u到v路径的权值和,一开始每个点都等于零。
这样不难想到将所有路径按val 从小到大排序,然后扫描路径数组,每次将树上节点权值小于等于val的所有点加上该点的权值,此路径的ans=get_sum(l,r)。
 

AC代码

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 #define ll long long
  4 #define N 100050
  5 #define INF 123456789
  6 int n,m;
  7 int tot,last[N];
  8 ll ans[N];
  9 int cnt,fa[N],dp[N],size[N],son[N],rk[N],kth[N],top[N];
 10 struct Query
 11 {
 12   int l,r,id; ll val;
 13   bool operator <(const Query&b)const
 14   {return val<b.val;}
 15 }a[N],que[N<<1];
 16 struct Edge{int from,to,s;}edges[N<<1];
 17 struct Tree{int l,r;ll sum;}tr[N<<2];
 18 template<typename T>void read(T&x)
 19 {
 20   ll k=0; char c=getchar();
 21   x=0;
 22   while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
 23   if (c==EOF)exit(0);
 24   while(isdigit(c))x=x*10+c-'0',c=getchar();
 25   x=k?-x:x;
 26 }
 27 void read_char(char &c)
 28 {while(!isalpha(c=getchar())&&c!=EOF);}
 29 void AddEdge(int x,int y)
 30 {
 31   edges[++tot]=Edge{x,y,last[x]};
 32   last[x]=tot;
 33 }
 34 void dfs1(int x,int pre)
 35 {
 36   fa[x]=pre;
 37   dp[x]=dp[pre]+1;
 38   size[x]=1;
 39   son[x]=0;
 40   for(int i=last[x];i;i=edges[i].s)
 41     {
 42       Edge &e=edges[i];
 43       if (e.to==pre)continue;
 44       dfs1(e.to,x);
 45       size[x]+=size[e.to];
 46       if (size[e.to]>size[son[x]])son[x]=e.to;
 47     }
 48 }
 49 void dfs2(int x,int y)
 50 {
 51   rk[x]=++cnt;
 52   kth[cnt]=x;
 53   top[x]=y;
 54   if (son[x]==0)return;
 55   dfs2(son[x],y);
 56   for(int i=last[x];i;i=edges[i].s)
 57     {
 58       Edge &e=edges[i];
 59       if (e.to==fa[x]||e.to==son[x])continue;
 60       dfs2(e.to,e.to);
 61     }
 62 }
 63 void bt(int x,int l,int r)
 64   {
 65     tr[x].l=l; tr[x].r=r; tr[x].sum=0;
 66     if (l==r)return;
 67     int mid=(l+r)>>1;
 68     bt(x<<1,l,mid);
 69     bt(x<<1|1,mid+1,r);
 70   }
 71 void update(int x,int p,ll tt)
 72   {
 73     if (p<=tr[x].l&&tr[x].r<=p)
 74       {
 75     tr[x].sum+=tt;
 76     return;
 77       }
 78     int mid=(tr[x].l+tr[x].r)>>1;
 79     if (p<=mid)update(x<<1,p,tt);
 80     if (mid<p)update(x<<1|1,p,tt);
 81     tr[x].sum=tr[x<<1].sum+tr[x<<1|1].sum;
 82   }
 83 ll query(int x,int l,int r)
 84   {
 85     if (l<=tr[x].l&&tr[x].r<=r)
 86       return tr[x].sum;
 87     int mid=(tr[x].l+tr[x].r)>>1; ll ans=0;
 88     if (l<=mid)ans+=query(x<<1,l,r);
 89     if (mid<r)ans+=query(x<<1|1,l,r);
 90     return ans;
 91   }
 92 ll get_sum(int x,int y)
 93 {
 94   int fx=top[x],fy=top[y];ll ans=0;
 95   while(fx!=fy)
 96     {
 97       if (dp[fx]<dp[fy])swap(x,y),swap(fx,fy);
 98       ans+=query(1,rk[fx],rk[x]);
 99       x=fa[fx]; fx=top[x];
100     }
101   if (dp[x]<dp[y])swap(x,y);
102   ans+=query(1,rk[y],rk[x]);
103   return ans;
104 }
105 void work()
106 {
107   read(n); read(m);
108   for(int i=1;i<=n;i++)read(a[i].val),a[i].id=i;
109   for(int i=1;i<=n-1;i++)
110     {
111       int x,y;
112       read(x); read(y);
113       AddEdge(x,y);
114       AddEdge(y,x); 
115     }
116   int num=0;
117   for(int i=1;i<=m;i++)
118     {
119       int l,r,x,y;
120       read(l); read(r); read(x);read(y);
121       que[++num]=Query{l,r,-i,x-1};
122       que[++num]=Query{l,r,i,y};
123     }
124   sort(a+1,a+n+1);
125   sort(que+1,que+num+1);
126   dfs1(1,0);
127   dfs2(1,1);
128   bt(1,1,n);
129   int ds=1;
130   for(int i=1;i<=num;i++)
131     {
132       while(ds<=n&&a[ds].val<=que[i].val)
133     {
134       update(1,rk[a[ds].id],a[ds].val);
135       ds++;
136     }
137       ll sum=get_sum(que[i].l,que[i].r);
138       if (que[i].id<0) ans[-que[i].id]-=sum;
139       else ans[que[i].id]+=sum;
140     }
141   printf("%lld",ans[1]);
142   for(int i=2;i<=m;i++)printf(" %lld",ans[i]);
143   printf("\n");
144 }
145 void clear()
146 {
147   tot=0; cnt=0; 
148   memset(last,0,sizeof(last));
149   memset(ans,0,sizeof(ans));
150 }
151 int main()
152 {
153 #ifndef ONLINE_JUDGE
154   freopen("aa.in","r",stdin);
155   //freopen("my.out","w",stdout);
156 #endif
157   while(1)
158   {
159     clear();
160     work();
161   }
162 }
View Code

 

TLE代码(树链剖分+主席树)

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 #define ll long long
  4 #define N 100050
  5 #define INF 123456789
  6 int n,m,w[N];ll b[N];
  7 int tot,last[N];
  8 int tree_num,root[N];
  9 int cnt,fa[N],dp[N],size[N],son[N],rk[N],kth[N],top[N];
 10 struct Edge{int from,to,s;}edges[N<<1];
 11 struct Tree{int l,r,ls,rs;ll sum;}tr[2500000];
 12 template<typename T>void read(T&x)
 13 {
 14   ll k=0; char c=getchar();
 15   x=0;
 16   while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
 17   if (c==EOF)exit(0);
 18   while(isdigit(c))x=x*10+c-'0',c=getchar();
 19   x=k?-x:x;
 20 }
 21 void read_char(char &c)
 22 {while(!isalpha(c=getchar())&&c!=EOF);}
 23 void AddEdge(int x,int y)
 24 {
 25   edges[++tot]=Edge{x,y,last[x]};
 26   last[x]=tot;
 27 }
 28 void dfs1(int x,int pre)
 29 {
 30   fa[x]=pre;
 31   dp[x]=dp[pre]+1;
 32   size[x]=1;
 33   son[x]=0;
 34   for(int i=last[x];i;i=edges[i].s)
 35     {
 36       Edge &e=edges[i];
 37       if (e.to==pre)continue;
 38       dfs1(e.to,x);
 39       size[x]+=size[e.to];
 40       if (size[e.to]>size[son[x]])son[x]=e.to;
 41     }
 42 }
 43 void dfs2(int x,int y)
 44 {
 45   rk[x]=++cnt;
 46   kth[cnt]=x;
 47   top[x]=y;
 48   if (son[x]==0)return;
 49   dfs2(son[x],y);
 50   for(int i=last[x];i;i=edges[i].s)
 51     {
 52       Edge &e=edges[i];
 53       if (e.to==fa[x]||e.to==son[x])continue;
 54       dfs2(e.to,e.to);
 55     }
 56 }
 57 void bt(int &x,int l,int r)
 58 {
 59   x=++tree_num;
 60   tr[x].l=l; tr[x].r=r; tr[x].sum=0;
 61   if (l==r)return;
 62   int mid=(l+r)>>1;
 63   bt(tr[x].ls,l,mid);
 64   bt(tr[x].rs,mid+1,r);
 65 }
 66 void add(int &x,int last,int p)
 67 {
 68   x=++tree_num;
 69   tr[x]=tr[last];
 70   tr[x].sum+=b[p];
 71   if (tr[x].l==tr[x].r)return;
 72   int mid=(tr[x].l+tr[x].r)>>1;
 73   if(p<=mid)add(tr[x].ls,tr[last].ls,p);
 74   else add(tr[x].rs,tr[last].rs,p);
 75 }
 76 ll ask(int x,int y,int p)
 77 {
 78   if (tr[x].r<=p)return tr[y].sum-tr[x].sum;
 79   int mid=(tr[x].l+tr[x].r)>>1;ll ans=0;
 80   if (1<=mid)ans+=ask(tr[x].ls,tr[y].ls,p);
 81   if (mid<p)ans+=ask(tr[x].rs,tr[y].rs,p);
 82   return ans;
 83 }
 84 ll get_sum(int x,int y,int tt)
 85 {
 86   int fx=top[x],fy=top[y];ll ans=0;
 87   while(fx!=fy)
 88     {
 89       if (dp[fx]<dp[fy])swap(x,y),swap(fx,fy);
 90       ans+=ask(root[rk[fx]-1],root[rk[x]],tt);
 91       x=fa[fx]; fx=top[x];
 92     }
 93   if (dp[x]<dp[y])swap(x,y);
 94   ans+=ask(root[rk[y]-1],root[rk[x]],tt);
 95   return ans;
 96 }
 97 void work()
 98 {
 99   read(n); read(m);
100   int num=0;
101   for(int i=1;i<=n;i++)read(w[i]),b[++num]=w[i];
102   b[++num]=INF;
103   for(int i=1;i<=n-1;i++)
104     {
105       int x,y;
106       read(x); read(y);
107       AddEdge(x,y);
108       AddEdge(y,x); 
109     }
110   sort(b+1,b+num+1);
111   num=unique(b+1,b+num+1)-b-1;
112   dfs1(1,0);
113   dfs2(1,1);
114   bt(root[0],1,num);
115   for(int i=1;i<=n;i++)
116     {
117       int tt=lower_bound(b+1,b+num+1,w[kth[i]])-b;
118       add(root[i],root[i-1],tt);
119     }
120   for(int i=1;i<=m;i++)
121     {
122       if (i>1)printf(" ");
123       int x,y,l,r;
124       read(x); read(y); read(l); read(r);
125       l=lower_bound(b+1,b+num+1,l)-b-1;
126       r=upper_bound(b+1,b+num+1,r)-b-1;
127       ll ans=get_sum(x,y,r);
128       ans-=get_sum(x,y,l);
129       printf("%lld",ans);
130     }
131   printf("\n");
132 }
133 void clear()
134 {
135   tot=0; cnt=0; tree_num=0;
136   memset(last,0,sizeof(last));
137 }
138 int main()
139 {
140 #ifndef ONLINE_JUDGE
141   freopen("aa.in","r",stdin);
142 #endif
143   while(1)
144   {
145     clear();
146     work();
147   }
148 }
View Code

 

转载于:https://www.cnblogs.com/mmmqqdd/p/10799395.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值