QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#78025 | #4839. Smaller LCA | s8194272 | RE | 3ms | 13608kb | C++14 | 3.6kb | 2023-02-16 14:38:34 | 2023-02-16 14:38:46 |
Judging History
answer
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<bitset>
#include<random>
#include<cmath>
#include<ctime>
#include<queue>
#include<map>
#include<set>
#define int long long
#define fi first
#define se second
#define max Max
#define min Min
#define abs Abs
#define lc (x<<1)
#define rc (x<<1|1)
#define mid ((l+r)>>1)
#define pb(x) push_back(x)
#define lowbit(x) ((x)&(-(x)))
#define fan(x) ((((x)-1)^1)+1)
#define mp(x,y) make_pair(x,y)
#define clr(f,n) memset(f,0,sizeof(int)*(n))
#define cpy(f,g,n) memcpy(f,g,sizeof(int)*(n))
#define SZ(x) ((int)(x.size()))
#define INF 0x3f3f3f3f
using namespace std;
inline int read()
{
int ans=0,f=1;
char c=getchar();
while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){ans=(ans<<1)+(ans<<3)+c-'0';c=getchar();}
return ans*f;
}
inline void write(int x)
{
if(x<0) putchar('-'),x=-x;
if(x/10) write(x/10);
putchar((char)(x%10)+'0');
}
template<typename T>inline T Abs(T a){return a>0?a:-a;};
template<typename T,typename TT>inline T Min(T a,TT b){return a<b?a:b;}
template<typename T,typename TT> inline T Max(T a,TT b){return a<b?b:a;}
const int N=2e5+5;
int n,dfn[N],low[N],dep[N],up[N][20],nm[N],nm2[N],ans[N],cnt;
struct Edge
{
int v,ne;
}e[N*2];
int head[N],tot;
inline void add(int u,int v)
{
e[++tot]=(Edge){v,head[u]};
head[u]=tot;
}
struct BIT
{
int c[N];
inline void add(int x,int v)
{
for(;x<=n;x+=lowbit(x))
c[x]+=v;
}
inline int query(int x)
{
int res=0;
for(;x;x-=lowbit(x))
res+=c[x];
return res;
}
inline int query(int l,int r)
{
return query(r)-query(l-1);
}
inline void clear()
{
for(int i=1;i<=n;++i)
c[i]=0;
}
}sm;
struct node
{
int x,p,w;
bool operator < (const node &t)const
{
return x<t.x;
}
};
vector<node> T1,T2;
void dfs1(int u,int fa)
{
dfn[u]=++cnt;
dep[u]=dep[fa]+1;
up[u][0]=fa;
for(int i=1;i<=19;++i)
up[u][i]=up[up[u][i-1]][i-1];
for(int i=head[u];i;i=e[i].ne)
{
int v=e[i].v;
if(v==fa) continue;
dfs1(v,u);
}
low[u]=cnt;
}
inline int LCA(int x,int y)
{
if(dep[x]<dep[y]) swap(x,y);
for(int i=19;i>=0;--i)
if(dep[up[x][i]]>=dep[y])
x=up[x][i];
if(x==y) return x;
for(int i=19;i>=0;--i)
if(up[x][i]!=up[y][i])
x=up[x][i],y=up[y][i];
return up[x][0];
}
int id[N];
bool cmp(int x,int y)
{
return up[x][0]<up[y][0];
}
void dfs2(int u,int fa)
{
for(int i=head[u];i;i=e[i].ne)
{
int v=e[i].v;
if(v==fa) continue;
nm[v]+=nm[u];
dfs2(v,u);
nm2[u]+=nm2[v];
}
ans[u]=nm[u]+nm2[u];
}
signed main()
{
n=read();
for(int i=1;i<n;++i)
{
int u=read(),v=read();
add(u,v);add(v,u);
}
dfs1(1,0);
for(int i=1;i<=n;++i)
for(int j=i;i*j<=n;++j)
{
int lca=LCA(i,j);
if(i*j<lca) nm2[up[lca][0]]++;
T1.push_back((node){i*j,i,1});
T1.push_back((node){i*j,j,1});
T1.push_back((node){i*j,lca,-1});
if(up[lca][0]) T1.push_back((node){i*j,up[lca][0],-1});
T2.push_back((node){i*j,i,1});
T2.push_back((node){i*j,j,1});
T2.push_back((node){i*j,lca,-2});
}
sort(T1.begin(),T1.end());
for(int i=1,j=-1;i<=n;++i)
{
while(j+1<SZ(T1)&&T1[j+1].x<i)
{
++j;
sm.add(dfn[T1[j].p],T1[j].w);
}
nm[i]+=sm.query(dfn[i],low[i]);id[i]=i;
}
sort(id+1,id+1+n,cmp);
sort(T2.begin(),T2.end());
sm.clear();
for(int i=1,j=-1;i<=n;++i)
{
while(j+1<SZ(T2)&&T2[j+1].x<up[id[i]][0])
++j,sm.add(dfn[T2[j].p],T2[j].w);
nm[id[i]]-=sm.query(dfn[id[i]],low[id[i]]);
}
dfs2(1,0);
for(int i=1;i<=n;++i)
write(n*(n+1)/2-ans[i]),puts("");
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 13608kb
input:
5 1 2 4 2 2 5 3 5
output:
15 15 15 15 14
result:
ok 5 number(s): "15 15 15 15 14"
Test #2:
score: -100
Runtime Error
input:
300000 40632 143306 32259 40632 225153 143306 269774 225153 289668 40632 191636 269774 85717 191636 58564 191636 156509 143306 289939 40632 247103 269774 40257 40632 98149 289668 142277 143306 291616 40257 46813 225153 56324 143306 277154 142277 53903 289668 114266 32259 152231 58564 241151 152231 4...