萌新妺子求调线段树+二分,TLE + WA 0pts !
查看原帖
萌新妺子求调线段树+二分,TLE + WA 0pts !
681036
OldDriverTree楼主2023/4/8 22:08

rt,样例都没过,悬赏关注!

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+1;
int n,m,pos,a[N];

struct Query {
	int op,l,r;
}Q[N];

struct SGT
{
	struct node  {
		int l,r,cnt,tag;
	}T[N<<2];
	
	#define ls rt<<1
	#define rs rt<<1|1
	#define mid ( (T[rt].l+T[rt].r)>>1 )
	#define inrange l<=T[rt].l&&T[rt].r<=r
	void pushup(int rt) {
		T[rt].cnt=T[ls].cnt+T[rs].cnt;
	}
	void upd(int rt,int x) {
		int len=T[rt].r-T[rt].l+1;
		T[rt].cnt=x*len,T[rt].tag=x;
	}
	void push_down(int rt) { 
		upd(ls,T[rt].tag);
		upd(rs,T[rt].tag);
		T[rt].tag=-1;
	}
	void build(int rt,int l,int r,int x) {
		T[rt].l=l,T[rt].r=r,T[rt].tag=-1;
		if (l==r) return T[rt].cnt=a[l]>=x,void();
		build(ls,l,mid,x),build(rs,mid+1,r,x),pushup(rt);
	}
	void modify(int rt,int l,int r,int x)
	{
		if (l>r) return;
		if (inrange) return upd(rt,x);
		if (~T[rt].tag) push_down(rt);
		if (l<=mid) modify(ls,l,r,x);
		if (mid<r) modify(rs,l,r,x);
		pushup(rt);
	}
	int query(int rt,int l,int r)
	{
		if (inrange) return T[rt].cnt;
		if (~T[rt].tag) push_down(rt);
		if (r<=mid) return query(ls,l,r);
		if (mid<l) return query(rs,l,r);
		return query(ls,l,r)+query(rs,l,r);
	}
}T;

bool check(int x)
{
	T.build(1,1,n,x);
	for (int i=0;i<m;i++)
	{
		int op=Q[i].op,l=Q[i].l,r=Q[i].r;
		int cnt=T.query(1,l,r);
		if (!op) {
			T.modify(1,l,r-cnt,0);
			T.modify(1,r-cnt+1,r,1);
		}else {
			T.modify(1,l,l+cnt-1,1);
			T.modify(1,l+cnt,r,0);
		}
	}return T.query(1,pos,pos);
}
int main()
{
	scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	for (int i=0;i<m;i++) scanf("%d%d%d",&Q[i].op,&Q[i].l,&Q[i].r);
	int l=1,r=n; while (l<r)
	{
		int Mid=(l+r)>>1;
		if (check(Mid) ) l=Mid;
		else r=Mid-1;
	}printf("%d",l);
	return 0;
}
2023/4/8 22:08
加载中...