跳到正文

个人记录

BZOJ 有意思的题写写

EastPorridge约 40 分钟
原文
文章信息与版本

文章信息

发布
2022年11月16日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 40 分钟

版本

  1. v1当前

More and more vegetables,what should I do?\large \text {More and more vegetables,what should I do?}

BZOJ4127 Abs

树上的线段树维护区间绝对值和,区间增。

算是势能线段树吧,因为只有加没有减,所以一个数从负数到正数只有一次,所以我们维护区间的最大负数,负数,正数的个数,分别的负数,正数和,绝对值和与一个加的懒标记。

如果没有负数到正数的操作,就直接懒标记下传,否则就直接修改到单点,这个由负变正的操作最多只会有 nn 次,所以线段树部分的整体复杂度还是 O(nlog⁡n)O(n \log n) 的,再套个树剖就可以了。

Code.

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=4e5+10;
int h[N],ne[N<<1],e[N<<1],idx,dep[N],fa[N],sz[N],son[N],w[N],pl[N],cnt,id[N],top[N],n,m;
struct node {ll s[2],c[2],lz,nm,sum;} tr[N<<2];
void add(int u,int v) {ne[++idx]=h[u],e[idx]=v,h[u]=idx;}
void dfs(int u,int father,int depth)
{
	sz[u]=1; fa[u]=father; dep[u]=depth;
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i]; if(j == father) continue ;
		dfs(j,u,depth+1); sz[u]+=sz[j];
		if(sz[son[u]] < sz[j]) son[u]=j;
	}
}
void dfs2(int u,int t)
{
	top[u]=t; id[u]=++cnt; pl[cnt]=w[u];
	if(! son[u]) return ; dfs2(son[u],t);
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i]; if(j == fa[u] || j == son[u]) continue ;
		dfs2(j,j);
	}
}
void pushup(int u)
{
	tr[u].s[0]=tr[u<<1].s[0]+tr[u<<1|1].s[0];
	tr[u].s[1]=tr[u<<1].s[1]+tr[u<<1|1].s[1];
	tr[u].c[0]=tr[u<<1].c[0]+tr[u<<1|1].c[0];
	tr[u].c[1]=tr[u<<1].c[1]+tr[u<<1|1].c[1];
	tr[u].sum=tr[u<<1].sum+tr[u<<1|1].sum;
	tr[u].nm=max(tr[u<<1].nm,tr[u<<1|1].nm);
}
void pushdown(int u)
{
	if(tr[u].lz)
	{
		tr[u<<1].nm+=tr[u].lz; tr[u<<1|1].nm+=tr[u].lz;
		tr[u<<1].s[0]+=tr[u<<1].c[0]*tr[u].lz;
		tr[u<<1].s[1]+=tr[u<<1].c[1]*tr[u].lz;
		tr[u<<1|1].s[0]+=tr[u<<1|1].c[0]*tr[u].lz;
		tr[u<<1|1].s[1]+=tr[u<<1|1].c[1]*tr[u].lz;
		tr[u<<1].sum=tr[u<<1].s[1]-tr[u<<1].s[0];
		tr[u<<1|1].sum=tr[u<<1|1].s[1]-tr[u<<1|1].s[0];
		tr[u<<1].lz+=tr[u].lz; tr[u<<1|1].lz+=tr[u].lz;
	}
	tr[u].lz=0;
}
void build(int u,int l,int r)
{
	if(l == r)
	{
		if(pl[l] < 0) {tr[u].s[0]=pl[l]; tr[u].c[0]=1; tr[u].sum=-pl[l]; tr[u].nm=pl[l];}
		else {tr[u].s[1]=pl[l]; tr[u].c[1]=1; tr[u].sum=pl[l]; tr[u].nm=INT_MIN;}
		return ;
	}
	int mid = (l + r) >> 1;
	build(u<<1,l,mid); build(u<<1|1,mid+1,r);
	pushup(u);
}
void modify(int u,int l,int r,int L,int R,int k)
{
	if(l == r)
	{
		if(tr[u].c[0])
		{
			if(tr[u].s[0] + k <= 0) {tr[u].s[0]+=k; tr[u].nm=tr[u].s[0]; tr[u].sum=-tr[u].s[0];}
			else
			{
				tr[u].s[1]=tr[u].s[0]+k; tr[u].sum=tr[u].s[1]; tr[u].c[1]=1;
				tr[u].c[0]=tr[u].s[0]=0; tr[u].nm=INT_MIN;
			}
		}
		else tr[u].s[1]+=k,tr[u].sum=tr[u].s[1];
		return ;
	}
	if(L <= l && r <= R && tr[u].nm+k <= 0)
	{
		tr[u].nm+=k; 
		tr[u].s[0]+=tr[u].c[0]*k; tr[u].s[1]+=tr[u].c[1]*k;
		tr[u].sum=tr[u].s[1]-tr[u].s[0]; tr[u].lz+=k;
		return ;
	}
	pushdown(u);
	int mid = (l + r) >> 1;
	if(L <= mid) modify(u<<1,l,mid,L,R,k);
	if(mid < R) modify(u<<1|1,mid+1,r,L,R,k);
	pushup(u);
}
ll query(int u,int l,int r,int L,int R)
{
	if(L <= l && r <= R) return tr[u].sum;
	pushdown(u);
	int mid = (l + r) >> 1; ll res=0;
	if(L <= mid) res+=query(u<<1,l,mid,L,R);
	if(mid < R) res+=query(u<<1|1,mid+1,r,L,R);
	pushup(u);
	return res;
}
void modify_t(int u,int v,int k)
{
	while(top[u] != top[v])
	{
		if(dep[top[u]] < dep[top[v]]) swap(u,v);
		modify(1,1,n,id[top[u]],id[u],k); u=fa[top[u]];
	}
	if(dep[u] < dep[v]) swap(u,v); modify(1,1,n,id[v],id[u],k);
}
ll query_t(int u,int v)
{
	ll res=0;
	while(top[u] != top[v])
	{
		if(dep[top[u]] < dep[top[v]]) swap(u,v);
		res+=query(1,1,n,id[top[u]],id[u]); u=fa[top[u]];
	}
	if(dep[u] < dep[v]) swap(u,v); res+=query(1,1,n,id[v],id[u]);
	return res;
}
int main()
{
	memset(h,-1,sizeof h); scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",w+i);
	for(int i=1,u,v;i<n;i++) {scanf("%d%d",&u,&v); add(u,v); add(v,u);}
	dfs(1,0,1); dfs2(1,1); build(1,1,n);
	while(m -- )
	{
		int op,u,v,k;
		scanf("%d%d%d",&op,&u,&v);
		if(op == 1) {scanf("%d",&k); modify_t(u,v,k);}
		else printf("%lld\n",query_t(u,v));
	}
	return 0;
}

BZOJ4765 普通计算姬

跟 dfs 序有关的,显然又跟树剖加数据结构没关系,所以分块。

将 dfs 序重排的东西的点权插到树状数组里,显然 ii 子树的和就是 query(id[i]+sz[i]-1)-query(id[i]-1),再按原点的顺序分块,单点修改会影响的值,肯定是这一个块里面被它的子树所包含的,再统计一下每个块会被点 ii 影响的点的个数,我们可以 O(nn)O(n \sqrt n) 的预处理这个,就可以 O(n)O(\sqrt n) 的单点修改了,最坏复杂度 O(nnlog⁡n)O(n \sqrt n \log n),可以碾过。

这题不是不好写,是好绕。

Code.

C++
#include<bits/stdc++.h>
#define ll unsigned long long
using namespace std;
const int N=1e5+10,M=350; ll tr[N],sum[M],d[N];
int n,m,h[N],ne[N<<1],e[N<<1],idx,L[M],R[M],id[N],cnt,rt,sz[N],len,f[M][N],num[M],bl[N]; 
void add(int u,int v) {ne[++idx]=h[u],e[idx]=v,h[u]=idx;}
int lowbit(int x) {return x&-x;}
void modify(int x,ll k) {for(int i=x;i<=n;i+=lowbit(i)) tr[i]+=k;}
ll query(int x) {ll res=0; for(int i=x;i;i-=lowbit(i)) res+=tr[i]; return res;}
void dfs(int u,int father)
{
	id[u]=++cnt; sz[u]=1;
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i]; if(j == father) continue ;
		dfs(j,u); sz[u]+=sz[j];
	}
}
void dfs2(int u,int father)
{
	num[bl[u]]++;
	for(int i=1;i<=bl[n];i++) f[i][u]+=num[i];
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i]; if(j == father) continue ;
		dfs2(j,u);
	}
	num[bl[u]]--;
}
int main()
{
	memset(h,-1,sizeof h); scanf("%d%d",&n,&m); len=(int)sqrt(n);
	for(int i=1;i<=n;i++) scanf("%llu",d+i);
	for(int i=1,u,v;i<=n;i++) {scanf("%d%d",&u,&v); if(u) add(u,v),add(v,u); else rt=v;}
	dfs(rt,0);
	for(int i=1;i<=n;i++) modify(id[i],d[i]);
	for(int i=1;i<=n;i++) 
	{
		bl[i]=(i-1)/len+1; sum[bl[i]]+=query(id[i]+sz[i]-1)-query(id[i]-1);
		if(! L[bl[i]]) L[bl[i]]=i; R[bl[i]]=i;
	}
	dfs2(rt,0);
	while(m -- )
	{
		int op,u,k; scanf("%d%d%d",&op,&u,&k);
		if(op == 1)
		{
			for(int i=1;i<=bl[n];i++) sum[i]+=f[i][u]*(k-d[u]);
			modify(id[u],k-d[u]); d[u]=k;
		}
		else
		{
			ll res=0;
			if(bl[u] == bl[k]) {for(int i=u;i<=k;i++) res+=query(id[i]+sz[i]-1)-query(id[i]-1);}
			else
			{
				for(int i=bl[u]+1;i<=bl[k]-1;i++) res+=sum[i];
				for(int i=u;i<=R[bl[u]];i++) res+=query(id[i]+sz[i]-1)-query(id[i]-1);
				for(int i=L[bl[k]];i<=k;i++) res+=query(id[i]+sz[i]-1)-query(id[i]-1);
			}
			printf("%llu\n",res);
		}
	}
	return 0;
}

BZOJ2506 [2011福建集训] calc

取模有关,数据结构,考虑根号分治。

如果所有数据在 10410^4 中,如果模数 p≤100p \le 100,显然可以用 10710^7 的时间复杂度预处理出来,否则则暴力查询所有的 kk 不会超过 100100 次,直接套莫队就可以了。

Code.

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10,M=1e4+10;
vector<int> pl[110][110];
int n,m,a[N],cnt[M],ans[N],tot,len;
int get(int x) {return x/len;}
struct node
{
	int l,r,id,p,k;
	bool operator < (const node &o) const {
		if(get(l) == get(o.l)) return get(l)&1 ? r < o.r : r > o.r;
		return get(l) > get(o.l);
	}
} q[N];
inline void add(int x) {cnt[a[x]]++;}
inline void del(int x) {cnt[a[x]]--;}
int main()
{
	scanf("%d%d",&n,&m); len=(int)sqrt(n)+1;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",a+i);
		for(int j=1;j<=100;j++)	pl[j][a[i]%j].emplace_back(i);
	}
	for(int i=1,l,r,p,k;i<=m;i++)
	{
		scanf("%d%d%d%d",&l,&r,&p,&k);
		if(p <= 100) ans[i]=upper_bound(pl[p][k].begin(),pl[p][k].end(),r)-lower_bound(pl[p][k].begin(),pl[p][k].end(),l);
		else q[++tot]={l,r,i,p,k};
	}
	stable_sort(q+1,q+1+tot);
	for(int i=1,l=1,r=0;i<=tot;i++)
	{
		int l1=q[i].l,r1=q[i].r;
		while(l > l1) add(--l);
		while(r < r1) add(++r);
		while(l < l1) del(l++);
		while(r > r1) del(r--);
		for(int j=q[i].k;j<=10000;j+=q[i].p) ans[q[i].id]+=cnt[j];
	}
	for(int i=1;i<=m;i++) printf("%d\n",ans[i]);
	return 0;
}

BZOJ4152 [AMPPZ2014] The Captain

小小套路题,切比雪夫距离最短路,建完全图出来会似,所以分开按 x,yx,y 排序,两个相邻的点的距离一定短于两个中间有相隔的点,只需要建 2×n2 \times n 的边就可以了。

Code.

C++
#include<bits/stdc++.h>
using namespace std;
const int N=4e5+10; bitset<N> st;
int h[N],ne[N<<1],e[N<<1],w[N<<1],idx,n,d[N];
void add(int u,int v,int c) {ne[++idx]=h[u],e[idx]=v,w[idx]=c,h[u]=idx;}
struct node {int x,y,id;} pl[N];
bool cmpx(node a,node b) {return a.x < b.x;}
bool cmpy(node a,node b) {return a.y < b.y;}
void jk()
{
	priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
	q.emplace(make_pair(0,1)); d[1]=0;
	while(! q.empty())
	{
		int u=q.top().second; q.pop(); if(st[u]) continue ; st[u]=1;
		for(int i=h[u];~i;i=ne[i])
		{
			int j=e[i];
			if(d[j] > d[u] + w[i])
			{
				d[j]=d[u]+w[i]; 
				q.emplace(make_pair(d[j],j));
			}
		}
	}
}
int main()
{
	memset(h,-1,sizeof h); memset(d,0x3f,sizeof d); scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d%d",&pl[i].x,&pl[i].y),pl[i].id=i;
	sort(pl+1,pl+1+n,cmpx);
	for(int i=1;i<n;i++) add(pl[i].id,pl[i+1].id,abs(pl[i].x-pl[i+1].x)),add(pl[i+1].id,pl[i].id,abs(pl[i].x-pl[i+1].x));
	sort(pl+1,pl+1+n,cmpy);
	for(int i=1;i<n;i++) add(pl[i].id,pl[i+1].id,abs(pl[i].y-pl[i+1].y)),add(pl[i+1].id,pl[i].id,abs(pl[i].y-pl[i+1].y));
	jk();
	printf("%d\n",d[n]);
	return 0;
}

BZOJ3331 [BeiJing2013] 压力

无向图,两点最短距离的必经点的经过次数。

圆方树,建出来之后再树上差分,做完了,比较典。

Code.

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10,M=2e5+10; vector<int> G[M];
int h[N],ne[M<<1],e[M<<1],idx,n,m,q,dfn[N],low[N],nowstep,stk[N],tt,nw,top[M],fa[M],dep[M],sz[M],son[M],cnt,sum[M];
void add(int u,int v) {ne[++idx]=h[u],e[idx]=v,h[u]=idx;}
void add1(int u,int v) {G[u].emplace_back(v); G[v].emplace_back(u);}
void tarjan(int u)
{
	dfn[u]=low[u]=++nowstep; stk[++tt]=u;
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i];
		if(! dfn[j])
		{
			tarjan(j); low[u]=min(low[u],low[j]);
			if(dfn[u] <= low[j])
			{
				++nw; int y; add1(nw,u);
				do
				{
					y=stk[tt--];
					add1(y,nw);
				} while(y != j);
			}
		}
		else low[u]=min(low[u],dfn[j]);
	}
}
void dfs1(int u,int father,int depth)
{
	dep[u]=depth; sz[u]=1; fa[u]=father;
	for(auto j : G[u])
	{
		if(j == father) continue ;
		dfs1(j,u,depth+1); sz[u]+=sz[j];
		if(sz[son[u]] < sz[j]) son[u]=j;
	}
}
void dfs2(int u,int t)
{
	top[u]=t; if(! son[u]) return ; dfs2(son[u],t);
	for(auto j : G[u])
	{
		if(j == fa[u] || j == son[u]) continue ;
		dfs2(j,j);
	}
}
void dfs(int u,int father)
{
	for(auto j : G[u])
	{
		if(j == father) continue ;
		dfs(j,u); sum[u]+=sum[j];
	}
}
int lca(int u,int v)
{
	while(top[u] != top[v])
	{
		if(dep[top[u]] < dep[top[v]]) swap(u,v);
		u=fa[top[u]];
	}
	return dep[u] < dep[v] ? u : v;
}
int main()
{
	memset(h,-1,sizeof h); scanf("%d%d%d",&n,&m,&q); nw=n;
	for(int i=1,u,v;i<=m;i++)
	{
		scanf("%d%d",&u,&v);
		add(u,v); add(v,u);
	}
	tarjan(1); dfs1(1,0,1); dfs2(1,1);
	while(q -- )
	{
		int u,v; scanf("%d%d",&u,&v);
		sum[u]++; sum[v]++; int p=lca(u,v); sum[p]--; sum[fa[p]]--; 
	}
	dfs(1,0);
	for(int i=1;i<=n;i++) printf("%d\n",sum[i]);
	return 0;
}

BZOJ4867 [Ynoi2017]舌尖上的由乃

树上区间加,区间第 kk 大。放弃了,真卡不动了。开 1s 跑 dfs 预处理我都觉得它跑慢了。

Code.

C++
/*
飛んでいっちゃえ! キミのもとへ  わたしの ぴゅあぴゅあはーと?
受けとめてくれるなら 恐くはないの 
この気持ちが 大気圏 越えたとき  キミは見えなくなってた 道の向こう側
あい ☆ Don't mind
*/
#include<bits/stdc++.h>
#define re register
using namespace std;
inline char gc()
{
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
//#define gc getchar
inline int read()
{
	int x=0;char c=gc(); bool f=0;
	for(;c<'0'||c>'9';c=gc()) f|=(c=='-');
	for(;c>='0' && c<='9';c=gc()) x=(x<<1)+(x<<3)+(c^48);
	return x=f ? -x : x;
}
const int N=1e5+10,M=4e4+10;
struct node
{
	int v,id;
	bool operator < (const node &o) const {
		return v < o.v;
	}
} pl[N],xv[N],tmp[N]; bitset<N> st;
int n,m,h[N],ne[N],e[N],w[N],idx,bl[N],L[M],R[M],len,sz[N],id[N],p[N],q[N],qt,pt,cnt,yl,k,lz[M],mx;
inline void add(int u,int v,int c) {ne[++idx]=h[u],e[idx]=v,w[idx]=c,h[u]=idx;}
void dfs(int u,int father,int sum)
{
	id[u]=++cnt; sz[u]=1; pl[cnt]={sum,cnt};
	for(int i=h[u];i;i=ne[i])
	{
		int j=e[i]; if(j == father) continue ;
		dfs(j,u,sum+w[i]); sz[u]+=sz[j];
	}
}
inline void takina(int l,int r,int k)
{
	for(re int i=L[bl[l]];i<=R[bl[l]];i++) st[i]=0;
	for(re int i=l;i<=r;i++) pl[i].v+=k,st[pl[i].id]=1;
	qt=pt=yl=0; 
	for(re int i=L[bl[l]];i<=R[bl[l]];i++) {if(st[i]) q[++qt]=i; else p[++pt]=i;}
	int i=1,j=1;
	while(i <= qt && j <= pt)
	{
		if(xv[q[i]].v + k < xv[p[j]].v) tmp[++yl]=xv[q[i++]],tmp[yl].v+=k;
		else tmp[++yl]=xv[p[j++]];
	}
	while(i <= qt) tmp[++yl]=xv[q[i++]],tmp[yl].v+=k;
	while(j <= pt) tmp[++yl]=xv[p[j++]];
	for(re int i=1;i<=yl;i++) xv[L[bl[l]]+i-1]=tmp[i];
	for(re int i=L[bl[l]];i<=R[bl[l]];i++) pl[xv[i].id].id=i;
}
inline int check(int x,int l,int r)
{
	int tot=0;
	if(bl[l] == bl[r]) {for(int i=l;i<=r;i++) if(pl[i].v+lz[bl[l]] <= x) tot++; return tot;}
	else
	{
		for(re int i=l;i<=R[bl[l]];i++) {if(pl[i].v+lz[bl[l]] <= x) tot++;}
		for(re int i=L[bl[r]];i<=r;i++) {if(pl[i].v+lz[bl[r]] <= x) tot++;}
		for(re int i=bl[l]+1;i<=bl[r]-1;i++)
		{
			if(xv[R[i]].v + lz[i] <= x) {tot+=(R[i]-L[i]+1); continue ;}
			int lm=L[i],rm=R[i]+1;
			while(lm < rm)
			{
				int mid = (lm + rm) >> 1;
				if(xv[mid].v + lz[i] <= x) lm=mid+1;
				else rm=mid;
			}
			tot+=(lm-L[i]);
		}
	}
	return tot;
}
int main()
{
	n=read(); m=read(); read(); len=(int)sqrt(n*log2(n))-1000;
	for(re int i=2,u,c;i<=n;i++) {u=read(); c=read(); add(u,i,c); mx+=c;}
	dfs(1,0,0);
	for(re int i=1;i<=n;i++) {bl[i]=(i-1)/len+1; if(! L[bl[i]]) L[bl[i]]=i; R[bl[i]]=i;}
	for(re int i=1;i<=bl[n];i++)
	{
		for(int j=L[i];j<=R[i];j++) xv[j]=pl[j];
		sort(xv+L[i],xv+R[i]+1);
		for(int j=L[i];j<=R[i];j++) pl[xv[j].id].id=j;
	}
	while(m -- )
	{
		int op=read(),x=read(); k=read();
		if(op == 1)
		{
			if(k > sz[x]) {puts("-1"); continue ;}
			int l=0,r=mx+1,res=0;
			while(l <= r)
			{
				int mid = (l + r) >> 1;
				if(check(mid,id[x],id[x]+sz[x]-1) >= k) res=mid,r=mid-1;
				else l=mid+1;
			}
			printf("%d\n",res);
		}
		else
		{
			int l=id[x],r=id[x]+sz[x]-1; mx+=k;
			if(bl[l] == bl[r]) {takina(l,r,k); continue ;}
			else
			{
				for(int i=bl[l]+1;i<=bl[r]-1;i++) lz[i]+=k;
				takina(l,R[bl[l]],k); takina(L[bl[r]],r,k);
			}
		}
	}
	return 0;
}

BZOJ4631 踩气球

长度为 nn 的序列,单点修改,给定 mm 个区间,每次修改后询问有多少个区间的值都为 00。

链表或并查集维护一下非零区间的长度,将区间 [l,r][l,r] 看成在 rr 点上 ll 位置 +1+1 的线段树,每次单点修改至 00 时把它于从左数最近的非零位置线段树合并,统计这段区间已经变成 00 的区间,记录答案。

没有 Fyrsta 我一辈子都会忘记 rt[p]=merge(rt[p],rt[x]),Fyrsta 是我的红太阳。

Code.

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int tr[N<<2],ls[N<<2],rs[N<<2],cnt,n,m,a[N],f[N],rt[N],q,ans;
int find(int x) {if(f[x] != x) f[x]=find(f[x]); return f[x];}
void modify(int &u,int l,int r,int x)
{
	if(! u) u=++cnt; if(l == r) return tr[u]++,void();
	int mid = (l + r) >> 1;
	if(x <= mid) modify(ls[u],l,mid,x); else modify(rs[u],mid+1,r,x);
	tr[u]=tr[ls[u]]+tr[rs[u]];
}
void query(int u,int l,int r,int x)
{
	if(! u || ! tr[u]) return ;
	if(l == r) return ans+=tr[u],tr[u]=0,void();
	int mid = (l + r) >> 1;
	if(x <= mid) query(ls[u],l,mid,x); query(rs[u],mid+1,r,x);
	tr[u]=tr[ls[u]]+tr[rs[u]];
}
int merge(int x,int y)
{
	if(! x || ! y) return x | y;
	tr[x]+=tr[y];
	ls[x]=merge(ls[x],ls[y]); rs[x]=merge(rs[x],rs[y]);
	return x;
}
int main()
{
	scanf("%d%d",&n,&m); iota(f+1,f+1+n,1);
	for(int i=1;i<=n;i++) scanf("%d",a+i);
	for(int i=1,l,r;i<=m;i++) scanf("%d%d",&l,&r),modify(rt[r],1,n,l);
	scanf("%d",&q);
	while(q -- )
	{
		int x; scanf("%d",&x); x=(x+ans-1)%n+1;
		if(! --a[x])
		{
			f[x]=x-1; int p=find(x);
			query(rt[x],1,n,p+1); rt[p]=merge(rt[p],rt[x]);
		}
		printf("%d\n",ans);
	}
	return 0;
}

BZOJ3207 花神的嘲讽计划Ⅰ

问序列 [l,r][l,r] 中是否存在长度 kk 的询问串。

最喜欢的一题,将所有长度 kk 的串全都哈希一下,原题就变成区间 [l,r−k+1][l,r-k+1] 是否出现过原串的哈希值,开个桶莫队就可以了。

Code.

C++
#include<bits/stdc++.h>
#define ll unsigned long long
using namespace std;
const int N=1e5+10,p=1e9+7; bitset<N> st;
int n,m,k,tot,len,s[N]; ll ha[N],fin[N],num[N],a[N];
int get(int x) {return x/len;}
struct node
{
	int l,r,id,pos;
	bool operator < (const node &o) const {
		if(get(l) == get(o.l)) return get(l)&1 ? r < o.r : r > o.r;
		return get(l) < get(o.l); 
	}
} q[N];
void add(int x) {s[a[x]]++;}
void del(int x) {s[a[x]]--;}
int main()
{
	scanf("%d%d%d",&n,&m,&k); fin[0]=1; len=(int)sqrt(n)+1;
	for(int i=1;i<=k;i++) fin[i]=fin[i-1]*p;
	for(int i=1,x;i<=n;i++) {scanf("%d",&x); ha[i]=ha[i-1]*p+x;}
	for(int i=1;i<=n-k+1;i++) a[i]=ha[i+k-1]-ha[i-1]*fin[k],num[i]=a[i];
	sort(num+1,num+n-k+2); int cnt=unique(num+1,num+n-k+2)-num-1;
	for(int i=1;i<=n-k+1;i++) a[i]=lower_bound(num+1,num+1+cnt,a[i])-num;
	for(int i=1,l,r,pos;i<=m;i++)
	{
		ll ham=0; scanf("%d%d",&l,&r); r=r-k+1;
		for(int j=1,x;j<=k;j++) scanf("%d",&x),ham=ham*p+x;
		pos=lower_bound(num+1,num+1+cnt,ham)-num;
		if(num[pos] != ham) {st[i]=1; continue ;}
		q[++tot]={l,r,i,pos};
	}
	sort(q+1,q+1+tot); int l=1,r=0;
	for(int i=1;i<=tot;i++)
	{
		int l1=q[i].l,r1=q[i].r;
		while(l > l1) add(--l);
		while(r < r1) add(++r);
		while(l < l1) del(l++);
		while(r > r1) del(r--);
		st[q[i].id]=(s[q[i].pos] == 0);
	}
	for(int i=1;i<=m;i++) puts(st[i] ? "Yes" : "No");
	return 0;
}

BZOJ4242 水壶

网格图 kruskal 重构树,建边会很寄,所以 bfs 建图,每个格点和它最近的点编号,这样一张图最多有 4×n×m4 \times n \times m 的点,直接记录边下来重构,lca 就可以了,点是 pp 不是 nn,坑死了。

Code.

C++
#include<bits/stdc++.h>
using namespace std;
inline int read()
{
	int x=0;char c=getchar(); bool f=0;
	for(;c<'0' || c>'9';c=getchar()) f|=(c=='-');
	for(;c>='0' && c<='9';c=getchar()) x=(x<<1)+(x<<3)+(c^48);
	return x=f ? -x : x;
}
const int M=2010,N=4e5+10;
struct node
{
	int u,v,w;
	bool operator < (const node &o) const {
		return w < o.w;
	}
} ; vector <node> pl; bitset<M> s[M];
struct plt {int x,y;} ; queue<plt> q; int dx[]={0,1,0,-1,0},dy[]={0,0,1,0,-1};
int st[M][M],d[M][M],h[N],ne[N],e[N],f[N],tot,idx,n,m,p,Q,top[N],sz[N],son[N],dep[N],fa[N],w[N];
int find(int x) {if(f[x] != x) f[x]=find(f[x]); return f[x];}
inline void add(int u,int v) {ne[++idx]=h[u],e[idx]=v,h[u]=idx;}
void dfs(int u,int father,int depth)
{
	dep[u]=depth; sz[u]=1; fa[u]=father;
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i]; if(j == father) continue ;
		dfs(j,u,depth+1); sz[u]+=sz[j];
		if(sz[son[u]] < sz[j]) son[u]=j;
	}
}
void dfs2(int u,int t)
{
	top[u]=t; if(! son[u]) return ; dfs2(son[u],t);
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i];
		if(j == fa[u] || j == son[u]) continue ;
		dfs2(j,j);
	}
}
inline int lca(int u,int v)
{
	while(top[u] != top[v])
	{
		if(dep[top[u]] < dep[top[v]]) swap(u,v);
		u=fa[top[u]];
	}
	return dep[u] < dep[v] ? u : v;
}
int main()
{
	memset(h,-1,sizeof h); 
	n=read(); m=read(); p=read(); Q=read(); iota(f+1,f+1+p,1);
	char ch=getchar();
	for(int i=1;i<=n;i++) 
	{
		while(ch != '.' && ch != '#') ch=getchar();
		for(int j=1;j<=m;j++) {s[i][j]=(ch == '#'); ch=getchar();}
	}
	for(int i=1,x,y;i<=p;i++) {x=read(); y=read(); st[x][y]=i; q.emplace(plt{x,y});}
	while(! q.empty())
	{
		auto u=q.front(); q.pop();
		for(int i=1;i<=4;i++)
		{
			int nx=u.x+dx[i],ny=u.y+dy[i];
			if(nx < 1 || nx > n || ny < 1 || ny > m || s[nx][ny] == 1 || st[nx][ny] == st[u.x][u.y]) continue ;
			if(st[nx][ny]) pl.emplace_back(node{st[u.x][u.y],st[nx][ny],d[u.x][u.y]+d[nx][ny]});
			else st[nx][ny]=st[u.x][u.y],d[nx][ny]=d[u.x][u.y]+1,q.emplace(plt{nx,ny});
		}
	}
	sort(pl.begin(),pl.end()); int nw=p;
	for(auto it : pl)
	{
		int u=find(it.u),v=find(it.v); if(u == v) continue ;
		++nw; f[u]=f[v]=f[nw]=nw; w[nw]=it.w; add(nw,u); add(nw,v);
	}
	for(int i=nw;i>=1;i--) if(! dep[i]) dfs(i,0,1),dfs2(i,i);
	while(Q -- )
	{
		int u=read(),v=read();
		if(find(u) != find(v)) {puts("-1"); continue ;}
		else printf("%d\n",w[lca(u,v)]);
	}
	return 0;
}

BZOJ4264 小C找朋友

判断任意两个点的边集是否相等。

给每个点随机赋值,两个相邻的边相互异或,算出一个哈希值,直接判就可以了。

C++
#include<bits/stdc++.h>
#define ll unsigned long long
using namespace std;
const int N=1e6+10;
int n,m,x[N],y[N]; ll rd[N],ha[N],res; 
mt19937 rnd(time(NULL));
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) rd[i]=(ll)rnd()*rnd();
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&x[i],&y[i]);
		ha[x[i]]^=rd[y[i]]; ha[y[i]]^=rd[x[i]];
	}
	for(int i=1;i<=m;i++) if((ha[x[i]]^rd[y[i]]) == (ha[y[i]]^rd[x[i]])) res++;
	sort(ha+1,ha+1+n);
	for(int i=1,j=0;i<=n;i++)
	{
		j++;
		if(i == n || ha[i] != ha[i+1]) res+=(ll)j*(j-1)/2ull,j=0;
	}
	printf("%llu\n",res);
	return 0;
}

原文
存档评论2 条抓取于