跳到正文

题解

我不知道题解应该叫什么名字

T7_DanieI约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年7月2日
收录
2026年7月2日
更新
2026年11月22日
篇幅
约 6 分钟
题目
P6325

版本

  1. v1当前

和机房同学 Clyth 讨论出了一种新的分块做法!


大家好,我是一个暴力数据结构爱好者,所以我就用分块和 set 过了这道题。

我们对 yy 分块,维护每一块中 xx 的最大值。

我们再处理每个点的最大值,并给每个点各开一个 set 来记录这个点中的 xx。

所以单次插入就是 O(log⁡V)O(\log V) 的。

对于询问:

  • 我们先判断当前 yy 是否合法,如果合法,在当前点的 set 中寻找第一个大于 xx 的点。

  • 随后我们逐个点判断散块,若散块合法,在当前点的 set 中寻找第一个大于等于 xx 的点。

  • 随后去处理整块,若整块合法,像散块一样的处理当前块。

所以单次查询就是 O(V)O(\sqrt V) 的。

注意这里需要离散化。

代码
C++
#include<bits/stdc++.h>
using namespace std;

const int N=2e5+5,M=500,V=2e5;
int X[N],Y[N],cnt;
multiset<int> s[N];
int maxn[N],maxx[M];
unordered_map<int,int> mp[N];
struct node{char ch;int x,y;}b[N];
struct edge{int x,y;}a[N];int tot;
int belong[N],st[M],ed[M],block,num;
/*
s 维护每个点
maxn 维护单点最大值
maxx 维护一块最大值
mp 维护对应数值的学生编号
X Y XX YY cnt b 用于读入并离散化
*/

inline int read(){
	int s=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
	while(isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s*f;
}

inline int ask(int x,int y){
    if(maxn[y]>x){//注意不能找自己,所以需要特判 y 本身
        int now=*s[y].upper_bound(x);//注意这里要跳过自身
        return mp[now][y];
    }
    //散块,若最大值 >=x,则这是合法的
    for(int i=y+1;i<=ed[belong[y]];i++){
        if(maxn[i]>=x){
            int now=*s[i].lower_bound(x);
            return mp[now][i];
        }
    }
    //整块,若最大值 >=x,则这是合法的
    int id=-1;
    for(int i=belong[y]+1;i<=num;i++)
        if(maxx[i]>=x){id=i;break;}
    if(id==-1) return -1;//无解
    //否则有解,遍历当前整块
    for(int i=st[id];i<=ed[id];i++){
        if(maxn[i]>=x){
            int now=*s[i].lower_bound(x);
            return mp[now][i];
        }
    }
}

signed main(){
    //先读入并离线
    int T;cin>>T;
    for(int i=1;i<=T;i++){
        char ch;cin>>ch;b[i].ch=ch;
        if(ch=='D'){
            int x=read(),y=read();b[i].x=x,b[i].y=y;
            X[++cnt]=x,Y[cnt]=y;
        }else{int now=read();b[i].x=now;}
    }
    //离散化 x 和 y 坐标
    sort(X+1,X+cnt+1);int lenX=unique(X+1,X+cnt+1)-X-1;
    for(int i=1;i<=T;i++)
        if(b[i].ch=='D') b[i].x=lower_bound(X+1,X+lenX+1,b[i].x)-X;
    sort(Y+1,Y+cnt+1);int lenY=unique(Y+1,Y+cnt+1)-Y-1;
    for(int i=1;i<=T;i++)
        if(b[i].ch=='D') b[i].y=lower_bound(Y+1,Y+lenY+1,b[i].y)-Y;
    //按照 y 分块,初始化信息
    block=(int)sqrt(lenY);num=lenY/block;
    for(int i=1;i<=lenY;i++){
        belong[i]=i/block;ed[belong[i]]=i;
        if(st[belong[i]]==0) st[belong[i]]=i;
    }
    //使用 unordered_map 记录编号,set 处理单点的询问
    for(int i=1;i<=T;i++){
        char ch=b[i].ch;
        if(ch=='D'){
            int x=b[i].x,y=b[i].y;
            a[++tot]={x,y};mp[x][y]=tot;//记录编号
            s[y].insert(x);maxn[y]=max(maxn[y],x);//放入当前点,更新最大值
            maxx[belong[y]]=max(maxx[belong[y]],x);//放入当前块,更新最大值
        }else{
            int now=b[i].x;
            int x=a[now].x,y=a[now].y,ans=ask(x,y);
            if(ans==-1) puts("NE");
            else cout<<ans<<"\n";
        }
    }
    return 0;
}
/*
对 y 分块,维护每一块 x 的最大值
给每个点开一个 set,并维护这个点的 x 最大值
插入就是单 log 的

查询的时候,查询 x 最大值是否满足,若满足,直接扫一遍 y
查询的时间复杂度是 O(sqrt n)
*/

代码只写了半个小时,可能比较难看。

原文
存档评论7 条抓取于