「NOIP数据结构阶段测试0426」Conclusion
\textrm{Part 1}\quad \texttt{point/} 分数
| T1 | T2 | T3 |
|---|---|---|
| 100 | 8 | 404 Not Found |
\textrm{Part 2}\quad \texttt{Analysis/} 题目&错因解析
T1 P2343 宝石管理系统
能被我场切的题算什么蓝题
蓝题全靠平衡树本身的算法难度撑起来的。事实上比模版还简单。
题意易转化为动态维护第
由于此题没有区间询问,还可以使⽤权值线段树实现。
赛时代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int inf=1145141919810, N=5e5+5;
int n, tot, root, a[N], m;
struct node
{
int val, rnk, lc, rc, siz;
}tree[N];
void pushup(int k)
{
tree[k].siz=tree[tree[k].lc].siz+tree[tree[k].rc].siz+1;
}
int add_node(int val)
{
tree[++tot].siz=1;
tree[tot].val=val,tree[tot].lc=tree[tot].rc=0,tree[tot].rnk=rand();
return tot;
}
void split(int k, int &a, int &b, int val)
{
if(!k)
{
a=b=0;
return ;
}
if(tree[k].val<=val)
{
a=k;
split(tree[k].rc,tree[k].rc,b,val);
}
else
{
b=k;
split(tree[k].lc,a,tree[k].lc,val);
}
pushup(k);
}
void merge(int &k, int a, int b)
{
if(!a||!b)
{
k=a+b;
return ;
}
if(tree[a].rnk<tree[b].rnk)
{
k=a;
merge(tree[a].rc,tree[a].rc,b);
}
else
{
k=b;
merge(tree[b].lc,a,tree[b].lc);
}
pushup(k);
}
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);
}
void del(int &k, int val)
{
int a=0, b=0, z=0;
split(k,a,b,val);
split(a,a,z,val-1);
merge(z,tree[z].lc,tree[z].rc);
merge(a,a,z);
merge(k,a,b);
return ;
}
int find_rank(int &k, int val)
{
int a=0, b=0;
split(k,a,b,val-1);
int tmp=tree[a].siz+1;
merge(k,a,b);
return tmp;
}
int find_num(int k, int x)
{
while(tree[tree[k].lc].siz+1!=x)
{
if(tree[tree[k].lc].siz>=x)
{
k=tree[k].lc;
}
else
{
x-=tree[tree[k].lc].siz+1;
k=tree[k].rc;
}
}
return tree[k].val;
}
signed main()
{
srand(time(0));
cin>>n>>m;
root=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
insert(root,a[i]);
}
while(m--)
{
int op, x;
cin>>op>>x;
if(op==1)
{
cout<<find_num(root,tot-x+1)<<"\n";
}
else
{
insert(root,x);
}
}
return 0;
}
T2 P4137 Rmq Problem / mex
数据、值域范围都不大,静态查询,与
抛开事实不谈,考场上其实离正解很近了。
考虑值域分块,每个块维护
代码:
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=2e5+5;
int n, m, a[N], sum[N], L[N], R[N], tot, len, pos[N], ans[N], cnt[N], siz[N];
struct node
{
int x, y, id;
}q[N];
void init()
{
tot=len=sqrt(200000);
for(int i=1;i<=tot;i++)
{
L[i]=(i-1)*len+1;
R[i]=i*len;
siz[i]=len;
}
if(R[tot]<200000)
{
tot++;
L[tot]=R[tot-1]+1;
R[tot]=200000;
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;
}
}
}
bool cmp(node x, node y)
{
if(x.x/len!=y.x/len)
{
return x.x<y.x;
}
if(x.x/len%2)
{
return x.y>y.y;
}
return x.y<y.y;
}
void add(int x)
{
cnt[a[x]]++;
if(cnt[a[x]]==1)
{
sum[pos[a[x]]]++;
}
}
void del(int x)
{
cnt[a[x]]--;
if(!cnt[a[x]])
{
sum[pos[a[x]]]--;
}
}
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])
{
return j;
}
}
}
return R[tot]+1;
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>a[i];
init();
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";
}
T3 P3604 美好的每一天
先展示一下来追梦重制题面。
题面
在蒸汽⾰命的核⼼都市"⻮轮之⼼",矗⽴着维系全城能源的巨型差分机「巴⻉奇七号」。近⽇该机出现谐 振失谐现象,机械⼯程师需要检测压⼒管道中各区段的稳定谐振⼦单元数量。
差分机的压⼒管道由
输入格式
⾸行两个整数
次行为⻓度
后续
输出格式
输出
这真的是形式化题意吗?
赛时看到这题直接弃了。
思路:
判断回⽂的合法,等价于判断
⼀个经典的技巧是,维护⼀个
参考代码:
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=6e4+5;
int n, m, len, a[N], sum[N], cnt[1<<26];
long long ans, Ans[N];
struct node
{
int x, y, id;
}q[N];
bool cmp(node x, node y)
{
if(x.x/len!=y.x/len)
{
return x.x<y.x;
}
if(x.x/len%2)
{
return x.y>y.y;
}
return x.y<y.y;
}
void add(int x)
{
ans+=cnt[sum[x]];
for(int i=0;i<26;i++)
{
ans+=cnt[(1<<i)^sum[x]];
}
cnt[sum[x]]++;
}
void del(int x)
{
cnt[sum[x]]--;
ans-=cnt[sum[x]];
for(int i=0;i<26;i++)
{
ans-=cnt[(1<<i)^sum[x]];
}
}
signed main()
{
cin>>n>>m;
len=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].x>>q[i].y;
q[i].x--,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]=ans;
}
for(int i=1;i<=m;i++)cout<<Ans[i]<<"\n";
}