topic
solution
Isn't this just using a balanced tree?
How could such a complicated code be used! ! ! (╯‵□′)╯︵┻━┻
It’s really not possible to use discretization and chairman tree.
How is it possible to hit the line segment tree (╯‵□′)╯︵┻━┻
Adding two points to the tree array is fine, you don’t dislike the direct tree array.
I don’t want to fight either (╯‵□′)╯︵┻━┻
Does the balance tree have STL?
No STL(╯‵□′)╯︵┻━┻
Then use the overall dichotomy
No (╯‵□′)╯︵┻━┻
Then learn later.
Here, let’s think about it carefully, this is actually asking kkk is big, but thiskkk turns out to be incremental, which means we can use the heap, and then query the firstkkevery timek is big, then kick out the top of the pile, sok + 1 k+1k+1 is the top of the pile again! ! ! ! perfect.
Then I was WA. . . .
Think about it carefully. Although the element on the top of the heap is unlikely to appear again in continuous inquiries, if you finish the inquiries kkAfter k is big, insert a smaller than it to replace the dead ghost, so next time we askk + 1 k+1k+1 is still the top of the pile.
So we need a tool stack that can be regretted. Yes, it is the top stack.
We put all the deleted numbers in a big root pile, if the number xx inserted at this timex is smaller than Duitou, then putxxx is inserted into the big root pile, and the original top of the big root pile is inserted back into the small root pile to participate in the inquiry.
NB on the top pile! ! ! !
Of course, I do it by hand, the code is so long (╯‵□′)╯︵┻━┻.
Code
O (nlogn) O (nlogn) O ( n l o g n )
#include<cstdio>
#include<cstring>
#define N 31000
using namespace std;
int a[N],n,m,b[N];
inline void wap(int &x,int &y){
int t=x;x=y;y=t;}
struct xiao_dui
{
int a[N],tot;
void ins(int x)
{
a[++tot]=x;int now=tot;
while(now>1)
{
int i=now>>1;
if(a[i]>a[now])wap(a[i],a[now]),now=i;
else break;
}
}
int pop()
{
int ans=a[1];wap(a[1],a[tot]);tot--;
int now=1;
while((now<<1)<=tot)
{
int i=now<<1;
if(i<tot && a[i+1]<a[i])i++;
if(a[i]<a[now])wap(a[i],a[now]),now=i;
else break;
}
return ans;
}
}a1;
struct da_dui
{
int a[N],tot;
void ins(int x)
{
a[++tot]=x;int now=tot;
while(now>1)
{
int i=now>>1;
if(a[i]<a[now])wap(a[i],a[now]),now=i;
else break;
}
}
int pop()
{
int ans=a[1];wap(a[1],a[tot]);tot--;
int now=1;
while((now<<1)<=tot)
{
int i=now<<1;
if(i<tot && a[i+1]>a[i])i++;
if(a[i]>a[now])wap(a[i],a[now]),now=i;
else break;
}
return ans;
}
int top(){
return tot==0?-999999999:a[1];}
}a2;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=m;i++)
{
scanf("%d",&b[i]);
b[i]=b[i]-i+1;
}
int t=0;
for(int i=1;i<=n;i++)
{
if(a[i]<a2.top())
{
a1.ins(a2.pop());
a2.ins(a[i]);
}
else a1.ins(a[i]);
while(a1.tot==b[t+1] && t<m)
{
t++;
int x=a1.pop();a2.ins(x);
printf("%d\n",x);
}
if(t==m)break;
}
return 0;
}