2025年4月25日NOIP数据结构小结
T1:100pts
T2:0pts
T3:没做
T1
平衡树模板,只需要实现插入和排名的功能即可
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+5;
int n,tot,root,q;
struct node
{
int lt,rt,sz,val,rnk;
}tree[N];
void update(int cur){tree[cur].sz=tree[tree[cur].lt].sz+tree[tree[cur].rt].sz+1;}
int add_node(int val)
{
int cur=++tot;
tree[tot].sz=1;
tree[tot].val=val;
tree[tot].rnk=rand();
return tot;
}
void split(int cur,int &a,int &b,int val)
{
if(!cur)
{
a=b=0;
return ;
}
if(tree[cur].val<=val)
{
a=cur;
split(tree[cur].rt,tree[cur].rt,b,val);
}
else
{
b=cur;
split(tree[cur].lt,a,tree[cur].lt,val);
}
update(cur);
return ;
}
void merge(int &cur,int a,int b)
{
if(a==0||b==0)
{
cur=a+b;
return ;
}
if(tree[a].rnk<tree[b].rnk)
{
cur=a;
merge(tree[a].rt,tree[a].rt,b);
}
else
{
cur=b;
merge(tree[b].lt,a,tree[b].lt);
}
update(cur);
return ;
}
void insert(int &k,int val)
{
int a=0,b=0,cur=add_node(val);
split(k,a,b,val);
merge(a,a,cur);
merge(k,a,b);
}
int K_th(int k,int x)
{
while(tree[tree[k].lt].sz+1!=x)
{
if(tree[tree[k].lt].sz>=x)
k=tree[k].lt;
else
{
x-=tree[tree[k].lt].sz+1;
k=tree[k].rt;
}
}
return tree[k].val;
}
signed main()
{
// freopen("A.in","r",stdin);
// freopen("A.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
srand(time(0));
cin>>n>>q;
for(int i=1,s;i<=n;i++)
cin>>s,insert(root,s);
while(q--)
{
int op,w;
cin>>op>>w;
if(op==1)
cout<<K_th(root,n-w+1)<<"\n";
else
insert(root,w),n++;
}
return 0;
}T2
赛时用的线段树+莫队没有发现时间复杂度会炸,同时代码出现了一些小问题,所以导致只拿0pts
赛事代码:
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,m,tot,cnt[N],lt=1,rt=0,ans[N],a[N],tree[N*4],tag[N*4];
struct node
{
int l,r,id;
}q[N];
bool cmp(node x,node y)
{
if(x.r/tot!=y.r/tot)
return x.r<y.r;
return x.l<y.l;
}
void pushup(int cur){tree[cur]=tree[cur*2]+tree[cur*2+1];}
void update(int cur,int lt,int rt,int ql,int qr,int val)
{
if(lt>qr||rt<ql)
return ;
// cout<<cur<<"\n";
if(ql==lt&&qr==rt)
{
// cout<<cur<<" "<<lt<<" "<<rt<<" "<<val<<"\n";
cnt[lt]+=val;
if(cnt[lt]==0)
tree[cur]=0;
if(cnt[lt]>=1)
tree[cur]=1;
return ;
}
int mid=(lt+rt)/2;
update(cur*2,lt,mid,ql,qr,val);
update(cur*2+1,mid+1,rt,ql,qr,val);
pushup(cur);
}
int query(int cur,int lt,int rt)
{
if(lt==rt)
return lt;
int mid=(lt+rt)/2;
// cout<<cur<<" "<<tree[cur]<<" "<<lt<<" "<<rt<<"\n";
if(tree[cur*2]<mid-lt+1)
return query(cur*2,lt,mid);
else
return query(cur*2+1,mid+1,rt);
}
void add(int cur){update(1,0,2e5+1,cur,cur,1);}
void del(int cur){update(1,0,2e5+1,cur,cur,-1);}
signed main()
{
// freopen("B.in","r",stdin);
// freopen("B.in","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>m;
tot=sqrt(n);
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=m;i++)
cin>>q[i].l>>q[i].r,q[i].id=i;
sort(q+1,q+1+m,cmp);
for(int i=1;i<=m;i++)
{
int qx=q[i].l,qy=q[i].r;
// cout<<q[i].id<<" "<<q[i].l<<" "<<q[i].r<<" "<<lt<<" "<<rt<<" "<<i<<"\n";
while(qx<lt)
{
lt--;
add(a[lt]);
}
while(qy>rt)
{
rt++;
add(a[rt]);
}
while(qx>lt)
{
del(a[lt]);
lt++;
}
while(qy<rt)
{
del(a[rt]);
rt--;
}
ans[q[i].id]=query(1,0,2e5+1);
// for(int j=0;j<=2;j++)
// cout<<cnt[j]<<" ";
// cout<<tree[1]<<"\n";
}
for(int i=1;i<=m;i++)
cout<<ans[i]<<"\n";
return 0;
}由于线段树每调用一次就会有log所以会炸,考虑可以用权值分块,同曼哈顿交易。
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, V = 2e5;
int a[N], sum[N], L[N], R[N], pos[N], ans[N], cnt[N], siz[N];
int n, m, tot, len;
struct node
{
int x, y, id;
}q[N];
void init_block()
{
tot = len = sqrt(V);
for(int i = 1; i <= tot; i++)
{
L[i] = (i - 1) * len + 1;
R[i] = i * len;
siz[i] = len;
}
if(R[tot] < V)
{
tot++;
L[tot] = R[tot - 1] + 1;
R[tot] = V;
siz[tot] = R[tot] - L[tot] + 1;
}
L[1] = 0;
siz[1]++;
for(int i = 1; i <= tot; i++)
for(int j = L[i]; j <= R[i]; j++)
pos[j] = i;
return ;
}
bool cmp(node u, node v)
{
if(u.x / len != v.x / len)
return u.x < v.x;
return u.y < v.y;
}
void add(int x)
{
cnt[a[x]]++;
if(cnt[a[x]] == 1)
sum[pos[a[x]]]++;
return ;
}
void del(int x)
{
cnt[a[x]]--;
if(cnt[a[x]] == 0)
sum[pos[a[x]]]--;
return ;
}
int query()
{
for(int i = 1; i <= tot; i++)
{
if(sum[i] == siz[i])
continue;
for(int j = L[i]; j <= R[i]; j++)
if(cnt[j] == 0)
return j;
}
return R[tot]+1;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++)
cin >> a[i];
init_block();
for(int i = 1; i <= m; i++)
{
cin >> q[i].x >> q[i].y;
q[i].id = i;
}
sort(q + 1, q + 1 + m, cmp);
int x = 1, y = 0;
for(int i = 1; i <= m; i++)
{
int qx = q[i].x, qy = q[i].y;
while(x > qx)
add(--x);
while(y < qy)
add(++y);
while(x<qx)
del(x++);
while(y>qy)
del(y--);
ans[q[i].id] = query();
}
for(int i = 1;i<=m;i++)
cout << ans[i] << "\n";
return 0;
}T3
压缩状态+前缀异或+莫队
1.静态询问,可以离线
2.会问的判断转换为对26种字母出现次数奇偶性的判断;
长度为偶数的回文串,所有字母均出现偶数次;
长度为奇数回文数,只有1中字母出现奇数次,其余都是偶数次。
3.可以用一个长度为26位的2进制数表示26种字母的就行,1表示奇数,0表示偶数
4.合法的字串的二进制数有27种情形,全0,以及26个不同位置的1
5.维护a[i]表示位置i上面的字母的二进制,例如字母x,a[i]=1<<(‘x’-‘a’)
维护sum[i]表示第1到第i个位置,没种字母出现的次数的前缀异或值
sum[i]=sum[i-1]^a[i];
6.对于一个区间[x,y],sum[y]^sum[x-1]的二进制上至多只有1个1,那么[x,y]合法
7.对于莫队的一队指针x和y,设x移动时,则求取以x为端点的区间,sum[x]^?==至多一个1(枚举),?==sum[x]^至多一个1。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
struct node
{
int l,r,id;
}q[N];
int n,m,k,ans[N],tot,num,cnt[1<<26],a[N],sum[N];
bool cmp(node x,node y)
{
if(x.l/tot!=y.l/tot)
return x.l<y.l;
if((x.l/tot)%2==1)
return x.r<y.r;
return x.r<y.r;
}
void add(int x)
{
num+=cnt[sum[x]];
for(int i=0;i<26;i++)
num+=cnt[(1<<i)^sum[x]];
cnt[sum[x]]++;
return ;
}
void del(int x)
{
cnt[sum[x]]--;
num-=cnt[sum[x]];
for(int i=0;i<26;i++)
num-=cnt[(1<<i)^sum[x]];
return ;
}
signed main()
{
ios::sync_with_stdio(0),cin.tie(0);
cin>>n>>m;
tot=sqrt(n)+1;
for(int i=1;i<=n;i++)
{
char c;
cin>>c;
a[i]=1<<(c-'a');
sum[i]=sum[i-1]^a[i];
}
for(int i=1;i<=m;i++)
cin>>q[i].l>>q[i].r,q[i].l--,q[i].id=i;
sort(q+1,q+1+m,cmp);
int lt=1,rt=0;
for(int i=1;i<=m;i++)
{
int qx=q[i].l,qy=q[i].r;
while(lt>qx)
{
lt--;
add(lt);
}
while(rt<qy)
{
rt++;
add(rt);
}
while(lt<qx)
{
del(lt);
lt++;
}
while(rt>qy)
{
del(rt);
rt--;
}
ans[q[i].id]=num;
}
for(int i=1;i<=m;i++)
cout<<ans[i]<<"\n";
return 0;
}