跳到正文

个人记录

250426 noip1 ds总结

_recollector_约 13 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年2月2日
篇幅
约 13 分钟
获赞
1

版本

  1. v1当前

A

算法分析

观察到第 kk 大和插入操作,不难想到平衡树,故套板子即可。

时间复杂度 O(nlog⁡2n)O(n\log_2 n)。

代码实现

C++
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+5;
int rt,tot;
struct node{int val,pri,ls,rs,sz;}tr[N];
int New(int x)
{
    ++tot;
    tr[tot].val=x;
    tr[tot].pri=rand();
    tr[tot].sz=1;
    return tot;
}
void pushup(int p){tr[p].sz=tr[tr[p].ls].sz+tr[tr[p].rs].sz+1;}
void split(int p,int x,int &L,int &R)
{
    if(!p){L=R=0;return ;}
    if(tr[p].val<=x)
    {
        L=p;
        split(tr[p].rs,x,tr[p].rs,R);
    }
    else
    {
        R=p;
        split(tr[p].ls,x,L,tr[p].ls);
    }
    pushup(p);
}
int merge(int L,int R)
{
    if(!L||!R)
        return L+R;
    if(tr[L].pri<=tr[R].pri)
    {
        tr[L].rs=merge(tr[L].rs,R);
        pushup(L);
        return L;
    }
    tr[R].ls=merge(L,tr[R].ls);
    pushup(R);
    return R;
}
int kth(int p,int k)
{
    while(tr[tr[p].ls].sz+1!=k)
    {
        if(tr[tr[p].ls].sz+1>k)
            p=tr[p].ls;
        else
            k-=tr[tr[p].ls].sz+1,p=tr[p].rs;
    }
    return tr[p].val;
}
signed main()
{
    srand(time(0));
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;++i)
    {
        int x;
        cin>>x;
        int L,R;
        split(rt,x,L,R);
        rt=merge(merge(L,New(x)),R);
    }
    while(m--)
    {
        int op,x;
        cin>>op>>x;
        if(op==1)
            cout<<kth(rt,tr[rt].sz-x+1)<<'\n';
        else
        {
            int L,R;
            split(rt,x,L,R);
            rt=merge(merge(L,New(x)),R);
        }
    }
}

B

算法分析

注意到原来的信息不好维护,于是考虑莫队。我们考虑用一个值域分块去完成莫队的加减和查询。

时间复杂度 O(nn)O(n\sqrt{n})。

代码实现

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
char *p1,*p2,buf[100000];
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int read()
{
	int x=0;char ch=gc();
	while(!isdigit(ch))ch=gc();
	while(isdigit(ch))x=(x<<3)+(x<<1)+(ch^48),ch=gc();
	return x;
}
#define Min(x,y) ((x)<(y)?(x):(y))
const int N=2e5+5,V=2e5+1,inf=0x3f3f3f3f;
	int n,m;
struct query{int l,r,id;}q[N];
int pos[N],a[N],cntt[N],cnt[N],L[N],R[N],poss[N],ans[N];
inline bool cmp(const query&q1,const query&q2)
{
	return pos[q1.l]!=pos[q2.l]?q1.l<q2.l:((pos[q1.l]&1)?q1.r<q2.r:q1.r>q2.r);

}
inline void update(int x,int d)
{
	if(!cnt[x])
		++cntt[poss[x]];
	cnt[x]+=d;
	if(!cnt[x])
		--cntt[poss[x]];
}
inline int query(int l,int r)
{
	const int x=poss[l],y=poss[r];
	if(x==y)
	{
		for(int i=l;i<=r;++i)
			if(cnt[i]<=0)
				return i;
		return n+1;
	}
	int ans=min(query(l,R[x]),query(L[y],r));
	for(int i=x+1;i<y;++i)
	{
		if(cntt[i]==R[i]-L[i]+1)
			continue;
		return min(ans,query(L[i],R[i]));
	}
	return min(ans,n+1);
}
signed main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);
//	freopen("P4137_11.in","r",stdin);
	cin>>n>>m;
	const int tt=sqrt(n);
	int t=n/tt;
	for(int i=1;i<=n;++i)cin>>a[i];
	for(int i=1;i<=t;++i)
		L[i]=(i-1)*t+1,R[i]=i*t;
	if(R[t]<n)
	{
		++t;
		L[t]=R[t-1]+1;
		R[t]=n;
	}
	for(int i=1;i<=t;++i)
		for(int j=L[i];j<=R[i];++j)
			pos[j]=i;
	int t2=sqrt(V);
	for(int i=1;i<=t2;++i)
		L[i]=(i-1)*t2+1,R[i]=i*t2;
	if(R[t2]<V)
	{
		++t2;
		L[t2]=R[t2-1]+1;
		R[t2]=V;
	}
	for(int i=1;i<=t2;++i)
		for(int j=L[i];j<=R[i];++j)
			poss[j]=i;
	for(int i=1;i<=m;++i)
		cin>>q[i].l>>q[i].r,q[i].id=i;
	sort(q+1,q+m+1,cmp);
	int l=1,r=0;
	for(int i=1;i<=m;++i)
	{
		while(l>q[i].l)
		{
			--l;
			update(a[l],1);
		}
		while(r>q[i].r)
		{
			update(a[r],-1);
			--r;
		}
		while(l<q[i].l)
		{
			update(a[l],-1);
			++l;
		}
		while(r<q[i].r)
		{
			++r;
			update(a[r],1);
		}
		if(!cnt[0])
			ans[q[i].id]=0;
		else ans[q[i].id]=query(0,V);
	}
	for(int i=1;i<=m;++i)
		cout<<ans[i]<<'\n';
}

C

算法分析

首先,如果一个串编号重排之后是一个回文串,那么肯定只有零个或一个字母出现奇数次。

Subtask 1: 1≤n,m≤1021 \le n,m \le 10^2

我们考虑 O(n2)O(n^2) 处理每一个查询,非常简单,只需要维护一个 SS 从左往右扫就可以了。

Subtask 2: 1≤n,m≤1031\le n,m \le 10^3

我们定义 aia_i 为前 ii 个字母的出现次数的奇偶性状态压缩后的样子,那么原问题就变成了有多少对 (i,j)(i,j) 满足 l−1≤i<j≤rl-1 \le i<j \le r 并且 popcount(ai⊕aj)≤1popcount(a_i \oplus a_j) \le 1。

我们考虑莫队,然后这个问题就变成了一个经典的问题,可以轻松解决。

时间复杂度 O(nΣnlog⁡n)O(n\Sigma \sqrt n \log n),因为可恶的 noip 肯定会卡我们,所以只能拿到st2。

Subtask 3

我们考虑将这些数字离散化,这样还能少一个 loglog,时间复杂度 O(nΣn)O(n\Sigma \sqrt n)。

代码实现

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=6e4+5,M=2e6+5,inf=0x3f3f3f3f;
int a[N],b[N][26];
int n,m,t;
struct qry{int l,r,id;}q[N];
bool cmp(qry q1,qry q2){return q1.l/t!=q2.l/t?q1.l<q2.l:q1.r<q2.r;}
int sum,ans[N];
int rk[N],tot,val[N],mp[M];
map<int,int>vis;
signed main()
{
    string s;
    cin>>n>>m>>s;
    t=sqrt(n);
    s="#"+s;
    for(int i=0;i<=n;++i)
    {

        if(i>0)a[i]=(1<<(s[i]-'a'))^a[i-1];
        else a[i]=0;
        rk[++tot]=a[i];
        vis[a[i]]=1;
        for(int j=0;j<26;++j)
            b[i][j]=(a[i]^(1<<j));
    }
    for(int i=1;i<=m;++i)
    {
        cin>>q[i].l>>q[i].r;
        --q[i].l;
        q[i].id=i;
    }
    sort(rk+1,rk+tot+1);
    tot=unique(rk+1,rk+tot+1)-rk-1;
    for(int i=0;i<=n;++i)
    {
        int x=lower_bound(rk+1,rk+tot+1,a[i])-rk;
        val[x]=a[i];
        a[i]=x;
    }
    for(int i=0;i<=n;++i)
        for(int j=0;j<26;++j)
        {
            if(!vis[b[i][j]])
            {
                b[i][j]=-1;
                continue;
            }
            int x=lower_bound(rk+1,rk+tot+1,b[i][j])-rk;
            b[i][j]=x;
        }
    sort(q+1,q+m+1,cmp);
    for(int i=1,l=1,r=0;i<=m;++i)
    {
        // cout<<q[i].l<<' '<<q[i].r<<'\n';
        while(l>q[i].l)
        {
            --l;
            sum+=mp[a[l]];
            for(int j=0;j<26;++j)
                if(~b[l][j])sum+=mp[b[l][j]];
            ++mp[a[l]];
        }
        while(r<q[i].r)
        {
            ++r;
            sum+=mp[a[r]];
            for(int j=0;j<26;++j)
                if(~b[r][j])sum+=mp[b[r][j]];
            ++mp[a[r]];
        }
        while(l<q[i].l)
        {
            --mp[a[l]];
            sum-=mp[a[l]];
            for(int j=0;j<26;++j)
                if(~b[l][j])sum-=mp[b[l][j]];
            ++l;
        }
        while(r>q[i].r)
        {
            --mp[a[r]];
            sum-=mp[a[r]];
            for(int j=0;j<26;++j)
                if(~b[r][j])sum-=mp[b[r][j]];
            --r;
        }
        ans[q[i].id]=sum;
    }
    for(int i=1;i<=m;++i)
        cout<<ans[i]<<'\n';
}
原文