Description
You are given a permutation $ a_1, a_2, dots, a_n $ of numbers from $ 1 $ to $ n $ . Also, you have $ n $ sets $ S_1,S_2,dots, S_n $ , where $ S_i={a_i} $ . Lastly, you have a variable $ cnt $ , representing the current number of sets. Initially, $ cnt = n $ .
We define two kinds of functions on sets:
$ f(S)=minlimits_{uin S} u $ ;
$ g(S)=maxlimits_{uin S} u $ .
You can obtain a new set by merging two sets $ A $ and $ B $ , if they satisfy $ g(A) < f(B) $ (Notice that the old sets do not disappear).
Formally, you can perform the following sequence of operations:
- $ cntgets cnt+1 $ ;
- $ S_{cnt}=S_ucup S_v $ , you are free to choose $ u $ and $ v $ for which $ 1le u, v < cnt $ and which satisfy $ g(S_u) < f(S_v) $ .
You are required to obtain some specific sets.
There are $ q $ requirements, each of which contains two integers $ l_i $ , $ r_i $ , which means that there must exist a set $ S_{k_i} $ ( $ k_i $ is the ID of the set, you should determine it) which equals $ {a_umid l_ileq uleq r_i} $ , which is, the set consisting of all $ a_i $ with indices between $ l_i $ and $ r_i $ .
In the end you must ensure that $ cntleq 2.2 imes 10^6 $ . Note that you don't have to minimize $ cnt $ . It is guaranteed that a solution under given constraints exists.
Solution
合并两个集合的要求是它们其中一个的最大值小于另一个的最小值,题中要求合并出一段区间
在权值线段树上每个节点维护该节点上的数的在原数组中的下标
每次合并出给定集合可以在权值线段树上爆搜,在每个节点找到在区间内的数合并即可
因为每个节点最多可以合并出的集合数为$len^2$,$len$为节点大小,可以用map维护每个节点已经合并出哪些区间的集合
在线段树较上层,限制时间复杂度的是每个节点的经过次数和,因为不可能每个节点的所有区间都会被合并出
在线段树较下层,限制时间复杂度的是每个节点的能合并出的集合数之和,因为当所有集合都被合并出就相当于$O(1)$回答了
解上层和下层的分界线在第几层
$$sum_{i=1}^x (2^{log n -i}frac{{2^i}^2}{2})=qspace 2^{log n-x}$$
解得$x=frac{log q}{2}$
将两层的时间复杂度上限算出,即将$x$回代
时间复杂度$O(nsqrt q)$
#include<unordered_map> #include<algorithm> #include<iostream> #include<utility> #include<vector> #include<cstdio> #include<map> using namespace std; int n,pos[5005],ans[100005],tot,q; map<pair<int,int>,int>mp[20005]; vector<int>ve[20005]; pair<int,int>S[2200005]; inline int read(){ int f=1,w=0; char ch=0; while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9')w=(w<<1)+(w<<3)+ch-'0',ch=getchar(); return f*w; } void build(int i,int l,int r){ for(int j=l;j<=r;j++)ve[i].push_back(pos[j]); sort(ve[i].begin(),ve[i].end()); if(l==r)return; int mid=l+r>>1; build(i<<1,l,mid),build(i<<1|1,mid+1,r); } int query(int i,int l,int r,int L,int R){ int left=lower_bound(ve[i].begin(),ve[i].end(),L)-ve[i].begin(),right=upper_bound(ve[i].begin(),ve[i].end(),R)-ve[i].begin()-1,mid=l+r>>1; if(right<left)return 0; if(right==left)return ve[i][left]; if(mp[i].find(make_pair(left,right))!=mp[i].end())return mp[i][make_pair(left,right)]; int lc=query(i<<1,l,mid,L,R),rc=query(i<<1|1,mid+1,r,L,R); if(!lc||!rc)return mp[i][make_pair(left,right)]=lc|rc; S[++tot]=make_pair(lc,rc); return mp[i][make_pair(left,right)]=tot; } int main(){ tot=n=read(),q=read(); for(int i=1;i<=n;i++)pos[read()]=i; build(1,1,n); for(int i=1;i<=q;i++){ int l=read(),r=read(); ans[i]=query(1,1,n,l,r); } printf("%d ",tot); for(int i=n+1;i<=tot;i++)printf("%d %d ",S[i].first,S[i].second); for(int i=1;i<=q;i++)printf("%d ",ans[i]); return 0; }