lzm点分治
swordofsky · · 个人记录
点分治
实现详见笔记本。
题目
CF161D
纯模板。
:::info[code]
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=5e4+5;
ll n,k,x,y,sz[N],f[N],cnt[N],tmp[N],id,tot,rt,ans;
vector<ll>g[N];
void getsz(ll x,ll ls){
sz[x]=1;
for(auto i:g[x]){
if(i==ls||f[i]) continue;
getsz(i,x);
sz[x]+=sz[i];
}
}
void getrt(ll x,ll ls){
ll mx=0;
for(auto i:g[x]){
if(i==ls||f[i]) continue;
getrt(i,x);
mx=max(mx,sz[i]);
}
mx=max(mx,tot-sz[x]);
if(mx*2<=tot) rt=x;
}
void getmp(ll x,ll ls,ll d){
if(d>k) return;
tmp[++id]=d;
for(auto i:g[x]){
if(i==ls||f[i]) continue;
getmp(i,x,d+1);
}
}
void sol(ll x){
getsz(x,0);
tot=sz[x];
getrt(x,0);
cnt[0]=f[rt]=1;
for(int i:g[rt]){
if(f[i]) continue;
id=0;
getmp(i,rt,1);
for(int j=1;j<=id;j++){
ans+=cnt[k-tmp[j]];
}
for(int j=1;j<=id;j++){
cnt[tmp[j]]++;
}
}
for(int i=0;i<=k;i++){
cnt[i]=0;
}
for(int i:g[rt]){
if(f[i]) continue;
sol(i);
}
}
int main(){
cin>>n>>k;
for(int i=1;i<n;i++){
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
sol(1);
cout<<ans;
return 0;
}
:::
P4178
加上边权,用树状数组求前缀和。
:::info[code]
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lb x&(-x)
const int N=4e4+5;
ll n,k,x,y,w,sz[N],f[N],cnt[N],tmp[N],tr[N],id,tot,rt,ans;
struct P{
ll x,w;
};
vector<P>g[N];
void add(ll x){
while(x<=k+1){
tr[x]++;
x+=lb;
}
}
ll ask(ll x){
ll ans=0;
while(x){
ans+=tr[x];
x-=lb;
}
return ans;
}
void getsz(ll x,ll ls){
sz[x]=1;
for(auto j:g[x]){
int i=j.x;
if(i==ls||f[i]) continue;
getsz(i,x);
sz[x]+=sz[i];
}
}
void getrt(ll x,ll ls){
ll mx=0;
for(auto j:g[x]){
int i=j.x;
if(i==ls||f[i]) continue;
getrt(i,x);
mx=max(mx,sz[i]);
}
mx=max(mx,tot-sz[x]);
if(mx*2<=tot) rt=x;
}
void getmp(ll x,ll ls,ll d){
if(d>k) return;
tmp[++id]=d;
for(auto i:g[x]){
if(i.x==ls||f[i.x]) continue;
getmp(i.x,x,d+i.w);
}
}
void sol(ll x){
getsz(x,0);
tot=sz[x];
getrt(x,0);
f[rt]=1;
add(1);
for(auto i:g[rt]){
if(f[i.x]) continue;
id=0;
getmp(i.x,rt,i.w);
for(int j=1;j<=id;j++){
ans+=ask(k-tmp[j]+1);
}
for(int j=1;j<=id;j++){
add(tmp[j]+1);
}
}
for(int i=0;i<=k;i++){
tr[i]=0;
}
for(auto i:g[rt]){
if(f[i.x]) continue;
sol(i.x);
}
}
int main(){
cin>>n;
for(int i=1;i<n;i++){
cin>>x>>y>>w;
g[x].push_back({y,w});
g[y].push_back({x,w});
}
cin>>k;
sol(1);
cout<<ans;
return 0;
}
:::
CF1101D
要求路径上
:::info[code]
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+5;
ll n,x,y,sz[N],f[N],a[N],s[N],p[N],tmp[N],l1[N],l2[N],id,tot,rt,ans;
vector<ll>g[N],d[N];
bool isp[N];
void init(int n){
for(int i=2;i<=n;i++){
if(isp[i]) continue;
d[i].push_back(i);
for(int j=i+i;j<=n;j+=i){
isp[j]=1;
d[j].push_back(i);
}
}
}
void getsz(ll x,ll ls){
sz[x]=1;
for(auto i:g[x]){
if(i==ls||f[i]) continue;
getsz(i,x);
sz[x]+=sz[i];
}
}
void getrt(ll x,ll ls){
ll mx=0;
for(auto i:g[x]){
if(i==ls||f[i]) continue;
getrt(i,x);
mx=max(mx,sz[i]);
}
mx=max(mx,tot-sz[x]);
if(mx*2<=tot) rt=x;
}
ll getmp(ll x,ll ls,ll d,ll s){
ll ans=d;
for(auto i:g[x]){
if(i==ls||f[i]) continue;
if(a[i]%s) continue;
ans=max(ans,getmp(i,x,d+1,s));
}
return ans;
}
void sol(ll x){
getsz(x,0);
tot=sz[x];
getrt(x,0);
f[rt]=1;
ll cnt=0;
for(int j:d[a[rt]]){
s[j]=++cnt;
p[cnt]=j;
}
for(auto i:g[rt]){
if(f[i]) continue;
for(int j:d[a[i]]){
if(!s[j]) continue;
ll t=getmp(i,rt,1,j);
if(l1[s[j]]<t) l2[s[j]]=l1[s[j]],l1[s[j]]=t;
else l2[s[j]]=max(l2[s[j]],t);
}
}
for(int j=1;j<=cnt;j++){
ans=max(ans,l1[j]+l2[j]+1);
s[p[j]]=l1[j]=l2[j]=0;
}
for(auto i:g[rt]){
if(f[i]) continue;
sol(i);
}
}
int main(){
cin>>n;
init(2e5);
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<n;i++){
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
sol(1);
cout<<ans;
return 0;
}
:::
P10421
类似于 P4178。
要求
转化:
用树状数组求出区间内链的个数
初始插入单个
:::info[code]
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lb x&(-x)
const int N=1e6+5;
ll n,l,r,x,y,w,sz[N],f[N],cnt[N],tmp[N],tc[N],ts[N],id,tot,rt,ans;
vector<ll>g[N];
void add(ll x,ll c,ll s){
x++;
while(x<=n+1){
tc[x]+=c;
ts[x]+=s;
x+=lb;
}
}
ll askc(ll x){
if(x<0) return 0;
x++;
ll ans=0;
while(x){
ans+=tc[x];
x-=lb;
}
return ans;
}
ll asks(ll x){
if(x<0) return 0;
x++;
ll ans=0;
while(x){
ans+=ts[x];
x-=lb;
}
return ans;
}
void getsz(ll x,ll ls){
sz[x]=1;
for(auto i:g[x]){
if(i==ls||f[i]) continue;
getsz(i,x);
sz[x]+=sz[i];
}
}
void getrt(ll x,ll ls){
ll mx=0;
for(auto i:g[x]){
if(i==ls||f[i]) continue;
getrt(i,x);
mx=max(mx,sz[i]);
}
mx=max(mx,tot-sz[x]);
if(mx*2<=tot) rt=x;
}
void getmp(ll x,ll ls,ll d){
tmp[++id]=d;
for(auto i:g[x]){
if(i==ls||f[i]) continue;
getmp(i,x,d+1);
}
}
void sol(ll x){
getsz(x,0);
tot=sz[x];
getrt(x,0);
f[rt]=1;
add(0,1,0);
ll p=0;
cnt[++p]=0;
for(auto i:g[rt]){
if(f[i]) continue;
id=0;
getmp(i,rt,1);
for(int j=1;j<=id;j++){
ll x=l-tmp[j],y=r-tmp[j];
ans+=tmp[j]*(askc(y)-askc(x-1))+asks(y)-asks(x-1);
}
for(int j=1;j<=id;j++){
add(tmp[j],1,tmp[j]);
cnt[++p]=tmp[j];
}
}
for(int i=1;i<=p;i++){
add(cnt[i],-1,-cnt[i]);
}
for(auto i:g[rt]){
if(f[i]) continue;
sol(i);
}
}
int main(){
cin>>n>>l>>r;
for(int i=2;i<=n;i++){
cin>>x;
g[x].push_back(i);
g[i].push_back(x);
}
sol(1);
cout<<ans;
return 0;
}
:::
20260817 测试
CF1575E
套用点分治模板思路。
一条经过根节点的路径,可以算出其一个子树中一条链的权值和
类似 P10421,用树状数组维护之前的总权值
注意到根节点两侧若颜色不同则额外需要一次换乘,因颜色只有两种,用两个树状数组维护
根节点一侧颜色为
颜色为
记得根节点自己也算长度为
:::info[code]
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lb x&(-x)
const int N=2e5+5,M=1e9+7;
ll T,n,k,a[N],x,y,w,rt,tot,sz[N],ts1[N],tc1[N],ts2[N],tc2[N],f[N],id,p,ans;
struct P{
ll x,w;
}tmp[N];
struct Q{
ll c,x,w;
}d[N];
vector<P>g[N];
void add1(ll x,ll c,ll s){
x++;
while(x<=n+1){
tc1[x]+=c;
ts1[x]+=s;
x+=lb;
}
}
ll asks1(ll x){
if(x<0) return 0;
ll ans=0;
x++;
while(x){
ans+=ts1[x];
ans%=M;
x-=lb;
}
return ans;
}
ll askc1(ll x){
if(x<0) return 0;
ll ans=0;
x++;
while(x){
ans+=tc1[x];
ans%=M;
x-=lb;
}
return ans;
}
void add2(ll x,ll c,ll s){
x++;
while(x<=n+1){
tc2[x]+=c;
ts2[x]+=s;
x+=lb;
}
}
ll asks2(ll x){
if(x<0) return 0;
ll ans=0;
x++;
while(x){
ans+=ts2[x];
ans%=M;
x-=lb;
}
return ans;
}
ll askc2(ll x){
if(x<0) return 0;
ll ans=0;
x++;
while(x){
ans+=tc2[x];
ans%=M;
x-=lb;
}
return ans;
}
void getsz(ll x,ll ls){
sz[x]=1;
for(auto i:g[x]){
if(i.x==ls||f[i.x]) continue;
getsz(i.x,x);
sz[x]+=sz[i.x];
}
}
void getrt(ll x,ll ls){
ll mx=0;
for(auto i:g[x]){
if(i.x==ls||f[i.x]) continue;
getrt(i.x,x);
mx=max(mx,sz[i.x]);
}
mx=max(mx,tot-sz[x]);
if(mx*2<=tot) rt=x;
}
void getmp(ll x,ll ls,ll d,ll lc,ll s){
if(d>k) return;
tmp[++id]={d,s};
for(auto i:g[x]){
if(i.x==ls||f[i.x]) continue;
getmp(i.x,x,d+(i.w!=lc),i.w,s+a[i.x]);
}
}
void sol(ll x){
getsz(x,-1);
tot=sz[x];
getrt(x,-1);
f[rt]=1;
p=0;
ans=(ans+a[rt])%M;
// add1(rt,1,a[rt]);
// d[++p]={1,1,0};
// add2(rt,1,a[rt]);
// d[++p]={2,1,0};
for(auto i:g[rt]){
if(f[i.x]) continue;
id=0;
getmp(i.x,rt,0,i.w,a[i.x]);
for(int j=1;j<=id;j++){
ll d1,d2;
if(i.w==1){
d1=k-tmp[j].x;d2=k-tmp[j].x-1;
}else{
d1=k-tmp[j].x-1;d2=k-tmp[j].x;
}
ans+=asks1(d1)+asks2(d2)+(tmp[j].w+a[rt])*(askc1(d1)+askc2(d2)+1);
ans%=M;
}
for(int j=1;j<=id;j++){
if(i.w==1) add1(tmp[j].x,1,tmp[j].w);
else add2(tmp[j].x,1,tmp[j].w);
d[++p]={i.w,tmp[j].x,tmp[j].w};
}
}
for(int i=1;i<=p;i++){
if(d[i].c==1) add1(d[i].x,-1,-d[i].w);
else add2(d[i].x,-1,-d[i].w);
}
for(auto i:g[rt]){
if(f[i.x]) continue;
sol(i.x);
}
}
int main(){
// freopen("as03.in","r",stdin);
// freopen("as03.out","w",stdout);
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<n;i++){
cin>>x>>y>>w;
g[x].push_back({y,w});
g[y].push_back({x,w});
}
sol(1);
cout<<ans;
return 0;
}
:::
20260818 测试
P6626
写了个在线,被卡常了。
我的思路:
点分治中找一个点经过重心不超过
点分治预处理,需要知道以
离线:
把查询存起来,最后点分治中查到其中一个
:::info[code]
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+5;
ll T,n,m,x,y,rt,tot,f[N],sz[N];
unordered_map<ll,ll>cnt[N],tr[N];
struct P{
ll x,y,w;
};
vector<P>q[N];
vector<ll>g[N];
void getsz(ll x,ll ls){
sz[x]=1;
for(int i:g[x]){
if(i==ls||f[i]) continue;
getsz(i,x);
sz[x]+=sz[i];
}
}
void getrt(ll x,ll ls){
ll mx=0;
for(int i:g[x]){
if(i==ls||f[i]) continue;
getrt(i,x);
mx=max(mx,sz[i]);
}
mx=max(mx,tot-sz[x]);
if(mx*2<=tot) rt=x;
}
void getmp(ll rt,ll t,ll x,ll ls,ll d){
cnt[rt][d]++;
tr[d][rt*n+t]++;
q[x].push_back({rt,t,d});
for(int i:g[x]){
if(i==ls||f[i]) continue;
getmp(rt,t,i,x,d+1);
}
}
inline void sol(ll x){
getsz(x,0);
tot=sz[x];
getrt(x,0);
f[rt]=1;
cnt[rt][0]=1;
for(int i:g[rt]){
if(f[i]) continue;
getmp(rt,i,i,rt,1);
}
for(int i:g[rt]){
if(f[i]) continue;
sol(i);
}
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>T;
while(T--){
cin>>n>>m;
for(int i=0;i<=n;i++){
g[i].clear();
q[i].clear();
cnt[i].clear();
tr[i].clear();
f[i]=0;
}
for(int i=1;i<n;i++){
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
sol(1);
while(m--){
cin>>x>>y;
ll ans=0;
if(cnt[x].count(y)) ans=cnt[x][y];
for(P i:q[x]){
if(y-i.w<0) continue;
if(cnt[i.x].count(y-i.w)) ans+=cnt[i.x][y-i.w];
if(tr[y-i.w].count(i.x*n+i.y)) ans-=tr[y-i.w][i.x*n+i.y];
}
cout<<ans<<"\n";
}
}
return 0;
}
:::
P12491
考虑点分治如何得到好路径。
设
若
处理
对于确定的
用数据结构(如树状数组)维护某个
:::info[code]
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+5;
const ll INF=1e18;
ll n,k,x,y,sz[N],f[N],a[N],b[N],id,tot,rt,ans=INF;
vector<ll>g[N];
struct P{
ll m,s,c;
}p[N];
bool cmp(P a,P b){
return a.m>b.m;
}
struct Q{
ll m1,c1,m2,c2;
}tr[N<<2];
void upd(Q &res,ll v,ll c){
if(c==-1||v>=INF) return;
if(v<res.m1){
if(c!=res.c1) res.m2=res.m1,res.c2=res.c1;
res.m1=v,res.c1=c;
}else if(c!=res.c1&&v<res.m2){
res.m2=v,res.c2=c;
}
}
Q merge(Q A,Q B){
Q res={INF,-1,INF,-1};
upd(res,A.m1,A.c1);
upd(res,A.m2,A.c2);
upd(res,B.m1,B.c1);
upd(res,B.m2,B.c2);
return res;
}
void build(ll id,ll l,ll r){
tr[id]={INF,-1,INF,-1};
if(l==r) return;
ll mid=l+r>>1;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
}
void upd(ll id,ll l,ll r,ll x,ll v,ll c){
if(l==r){
upd(tr[id],v,c);
return;
}
ll mid=l+r>>1;
if(x<=mid) upd(id*2,l,mid,x,v,c);
else upd(id*2+1,mid+1,r,x,v,c);
tr[id]=merge(tr[id*2],tr[id*2+1]);
}
Q ask(ll id,ll l,ll r,ll cl,ll cr){
if(cl<=l&&r<=cr) return tr[id];
ll mid=l+r>>1;
if(cr<=mid) return ask(id*2,l,mid,cl,cr);
if(cl>mid) return ask(id*2+1,mid+1,r,cl,cr);
return merge(ask(id*2,l,mid,cl,cr),ask(id*2+1,mid+1,r,cl,cr));
}
void getsz(ll x,ll ls){
sz[x]=1;
for(auto i:g[x]){
if(i==ls||f[i]) continue;
getsz(i,x);
sz[x]+=sz[i];
}
}
void getrt(ll x,ll ls){
ll mx=0;
for(auto i:g[x]){
if(i==ls||f[i]) continue;
getrt(i,x);
mx=max(mx,sz[i]);
}
mx=max(mx,tot-sz[x]);
if(mx*2<=tot) rt=x;
}
void getmp(ll x,ll ls,ll mi,ll sum,ll c){
p[++id]={mi,sum,c};
for(auto i:g[x]){
if(i==ls||f[i]) continue;
getmp(i,x,min(mi,a[i]),sum+b[i],c);
}
}
void sol(ll x){
getsz(x,0);
tot=sz[x];
getrt(x,0);
f[rt]=1;
id=0;
p[++id]={a[rt],b[rt],0};
for(auto i:g[rt]){
if(f[i]) continue;
getmp(i,rt,min(a[rt],a[i]),b[rt]+b[i],i);
}
vector<ll>v;
for(int i=1;i<=id;i++) v.push_back(p[i].s);
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
ll ms=v.size();
build(1,1,ms);
sort(p+1,p+id+1,cmp);
for(int i=1;i<=id;i++){
ll rs=(k+p[i].m-1)/p[i].m+b[rt]-p[i].s;
ll pos=lower_bound(v.begin(),v.end(),rs)-v.begin()+1;
if(pos<=ms){
Q t=ask(1,1,ms,pos,ms);
if(t.c1!=p[i].c&&t.m1<INF){
ans=min(ans,p[i].s+t.m1-b[rt]);
}else if(t.c2!=p[i].c&&t.m2<INF){
ans=min(ans,p[i].s+t.m2-b[rt]);
}
}
ll id=lower_bound(v.begin(),v.end(),p[i].s)-v.begin()+1;
upd(1,1,ms,id,p[i].s,p[i].c);
}
for(auto i:g[rt]){
if(f[i]) continue;
sol(i);
}
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>a[i]>>b[i];
if(a[i]*b[i]>=k) ans=min(ans,b[i]);
}
for(int i=1;i<n;i++){
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
sol(1);
cout<<ans<<"\n";
return 0;
}
:::