给定一棵完全二叉树,对每个询问i,Hi,求所有距离 i 点在Hi以内的点 j 的sigma(Hi-Dij)
利用完全二叉树的性质:层数最多为logn层。
先预处理每个点的每个子树节点到自己的距离,并排序。
查询的时候,从 i 号点开始一层层向上爬,在每一层统计多出来的节点对答案的贡献。
由于在完全二叉树向上爬的过程中,当前节点总有一个儿子已经被统计过,所以只要统计当前节点和它未统计到的儿子的子树对答案的贡献。而对于未统计到的儿子的子树,我们可以利用预处理的结果,lower_bound求出符合要求的点数有多少个,再利用前缀和统计答案。
时间复杂度O(nlogn+mlogn)
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=1000005,maxm=100005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
ll dist[maxn],ans[maxm];
vector<ll> d[maxn],qd[maxn],id[maxn],sum[maxn];
int num=0;
int main() {
int n,m,i,j,x;
ll y;
scanf("%d%d",&n,&m);
dist[1]=0;
for (i=2;i<=n;i++) scanf("%I64d",&dist[i]);
for (i=1;i<=m;i++) {
scanf("%d%I64d",&x,&y);
qd[x].push_back(y);
id[x].push_back(i);
}
d[1].push_back(0);
for (i=2;i<=n;i++) {
int now;ll pre;
now=i;pre=0;
while (now) {
d[now].push_back(pre);
pre+=dist[now];now/=2;
}
}
for (i=1;i<=n;i++) {
sort(d[i].begin(),d[i].end());
sum[i].push_back(d[i][0]);
for (j=1;j<d[i].size();j++)
sum[i].push_back(sum[i][j-1]+d[i][j]);
}
mem0(ans);
for (i=1;i<=n;i++) {
for (j=0;j<qd[i].size();j++) {
int now=i,last,q;ll rem=qd[i][j];
ll pos=(ll)(lower_bound(d[now].begin(),d[now].end(),rem)-d[now].begin());
if (pos!=0)
ans[id[i][j]]+=rem*pos-sum[now][pos-1];
last=i;rem-=dist[now];now/=2;
while (now) {
if (rem>=0) ans[id[i][j]]+=rem;
if (last==now*2) q=now*2+1; else q=now*2;
if (q<=n) {
ll pos=(ll)(lower_bound(d[q].begin(),d[q].end(),rem-dist[q])-d[q].begin());
if (pos!=0)
ans[id[i][j]]+=(rem-dist[q])*pos-sum[q][pos-1];
}
rem-=dist[now];last=now;now/=2;
}
}
}
for (i=1;i<=m;i++) printf("%I64d\n",ans[i]);
return 0;
}
Ralph is going to collect mushrooms in the Mushroom Forest.
There are m directed paths connecting n trees in the Mushroom Forest. On each path grow some mushrooms. When Ralph passes a path, he collects all the mushrooms on the path. The Mushroom Forest has a magical fertile ground where mushrooms grow at a fantastic speed. New mushrooms regrow as soon as Ralph finishes mushroom collection on a path. More specifically, after Ralph passes a path the i-th time, there regrow i mushrooms less than there was before this pass. That is, if there is initially xmushrooms on a path, then Ralph will collect x mushrooms for the first time, x - 1 mushrooms the second time, x - 1 - 2 mushrooms the third time, and so on. However, the number of mushrooms can never be less than 0.
For example, let there be 9 mushrooms on a path initially. The number of mushrooms that can be collected from the path is 9, 8, 6 and 3 when Ralph passes by from first to fourth time. From the fifth time and later Ralph can't collect any mushrooms from the path (but still can pass it).
Ralph decided to start from the tree s. How many mushrooms can he collect using only described paths?
The first line contains two integers n and m (1 ≤ n ≤ 106, 0 ≤ m ≤ 106), representing the number of trees and the number of directed paths in the Mushroom Forest, respectively.
Each of the following m lines contains three integers x, y and w (1 ≤ x, y ≤ n, 0 ≤ w ≤ 108), denoting a path that leads from tree x to tree y with w mushrooms initially. There can be paths that lead from a tree to itself, and multiple paths between the same pair of trees.
The last line contains a single integer s (1 ≤ s ≤ n) — the starting position of Ralph.
Print an integer denoting the maximum number of the mushrooms Ralph can collect during his route.
2 2 1 2 4 2 1 4 1
16
3 3 1 2 4 2 3 3 1 3 8 1
8
In the first sample Ralph can pass three times on the circle and collect 4 + 4 + 3 + 3 + 1 + 1 = 16mushrooms. After that there will be no mushrooms for Ralph to collect.
In the second sample, Ralph can go to tree 3 and collect 8 mushrooms on the path from tree 1 to tree 3.
给一个有向图,每条边有d个蘑菇。走某条路时,第一次可以得到d个,第二次d-1个,第三次d-1-2个。。。问从点s出发,最多得到多少蘑菇。
首先用tarjan把图缩点成一个新的DAG,再在DAG上把边反向,拓扑排序进行DP。
每个强连通分量之内的边,走多少次都没有关系。那么,计算出每条这类边最多走多少次,然后由此计算走这么多次能获得多少个蘑菇,把这个值加到新建的DAG上的点权上。这些可以预处理前缀和完成。而连接两个不同连通分量之间的边,最多只能走一次,最大收益就为这条边初始的d。
对于新图,我们按拓扑序DP就好了。因为我们已经把边反向,所以有以下DP方程:
dp[to]=max(dp[to],dp[from]+edge.dist)
注:DAG=有向无环图
这题并不难,只是写起来比较费时间
#include <cstdio>
#include <iostream>
#include <string.h>
#include <queue>
#include <algorithm>
#include <stack>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=1000005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
int head[maxn],col[maxn],dfn[maxn],low[maxn],x[maxn],y[maxn];
ll d[maxn],sum[maxn],su[maxn],dp[maxn],val[maxn];
bool inst[maxn];
stack<int> st;
int num=0,cnum=0;
struct Edge {
int from,to,pre;
ll dist;
};
Edge edge[maxn*2];
void addedge(int from,int to,ll dist) {
edge[num]=(Edge){from,to,head[from],dist};
head[from]=num++;
}
void tarjan(int now) {
num++;
dfn[now]=low[now]=num;
inst[now]=1;
st.push(now);
for (int i=head[now];i!=-1;i=edge[i].pre) {
int to=edge[i].to;
if (!dfn[to]) {
tarjan(to);
low[now]=min(low[now],low[to]);
}
else if (inst[to])
low[now]=min(low[now],dfn[to]);
}
if (dfn[now]==low[now]) {
inst[now]=0;
col[now]=++cnum;
while (st.top()!=now) {
col[st.top()]=cnum;
inst[st.top()]=0;
st.pop();
}
st.pop();
}
}
int main() {
int n,m,i,j,s,cnt=0;
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
su[0]=sum[0]=0;
for (i=1;su[i-1]<=1e8;i++) {
cnt++;
su[i]=su[i-1]+i;
sum[i]=sum[i-1]+su[i];
}
for (i=1;i<=m;i++) {
scanf("%d%d%I64d",&x[i],&y[i],&d[i]);
addedge(x[i],y[i],d[i]);
}
scanf("%d",&s);
num=0;
for (i=1;i<=n;i++) {
if (!dfn[i]) tarjan(i);
}
memset(head,-1,sizeof(head));
num=0;mem0(dfn);
ll hk;
for (i=1;i<=m;i++) {
if (col[x[i]]==col[y[i]]) {
ll pos=(ll)(lower_bound(su,su+cnt+1,d[i])-su);
if (pos<=0) hk=0; else hk=pos*d[i]-sum[pos-1];
val[col[x[i]]]+=hk;
} else {
addedge(col[y[i]],col[x[i]],d[i]);
dfn[col[x[i]]]++;
}
}
queue<int> q;
mem0(inst);
for (i=1;i<=cnum;i++) {
if (dfn[i]==0) q.push(i),inst[i]=1;
}
while (!q.empty()) {
int now=q.front();
q.pop();
dp[now]+=val[now];
for (i=head[now];i!=-1;i=edge[i].pre) {
int to=edge[i].to;
dfn[to]--;
if (dfn[to]==0) {
q.push(to);inst[to]=1;
}
dp[to]=max(dp[to],dp[now]+edge[i].dist);
}
}
printf("%I64d\n",dp[col[s]]);
return 0;
}