Codeforces Global Round 7 E. Bombs(线段树+思维)

题目链接
在这里插入图片描述
在这里插入图片描述
思路:思维量很大的题,不看题解完全做不来。。。首先要明白一件事答案肯定是递减的,所以想减少复杂度就可以通过逐渐枚举答案ans,剩下的就是判断ans能否作为答案。只有区间内的某一个位置大于等于ans的数减去后面的炸弹数量大于0那么ans就可行。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=3e5+1; 
int a[maxn],b[maxn],pos[maxn];
struct node{
	int l,r,lazy,sum;
}tree[maxn<<2];
void pushup(int x)
{
	tree[x].sum=max(tree[x<<1].sum,tree[x<<1|1].sum);
}
void pushdown(int x)
{
	if(tree[x].lazy)
	{
		tree[x<<1].sum+=tree[x].lazy;
		tree[x<<1|1].sum+=tree[x].lazy;
		tree[x<<1].lazy+=tree[x].lazy;
		tree[x<<1|1].lazy+=tree[x].lazy;
		tree[x].lazy=0;
	}
}
void build(int x,int l,int r)
{
	tree[x].l=l;tree[x].r=r;
	if(l==r){
		tree[x].lazy=tree[x].sum=0;return ;
	}
	int mid=(l+r)>>1;
	build(x<<1,l,mid);
	build(x<<1|1,mid+1,r);
	pushup(x);
}
void update(int x,int l,int r,int v)
{
	if(l<=tree[x].l&&tree[x].r<=r){
		tree[x].sum+=v;
		tree[x].lazy+=v;
		return ;
	}
	pushdown(x);
	int mid=(tree[x].l+tree[x].r)>>1;
	if(l<=mid) update(x<<1,l,r,v);
	if(mid<r) update(x<<1|1,l,r,v);
	pushup(x);
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;++i) scanf("%d",&a[i]),pos[a[i]]=i;
	for(int i=1;i<=n;++i) scanf("%d",&b[i]);
	build(1,1,n);
	int now=n;
	printf("%d ",n);
	update(1,1,pos[n],1);
	for(int i=1;i<n;++i)
	{
		update(1,1,b[i],-1);
		while(tree[1].sum<=0) now--,update(1,1,pos[now],1);
		printf("%d ",now);
	}
}
发布了283 篇原创文章 · 获赞 0 · 访问量 7298

猜你喜欢

转载自blog.csdn.net/qq_42479630/article/details/105126366