求助,蒟蒻很迷茫
查看原帖
求助,蒟蒻很迷茫
357641
YyuanDa楼主2021/11/8 19:52
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstdio>
#define ls (p<<1)
#define rs (p<<1|1)
#define mid ((l+r)>>1)
using namespace std;
const int N=1e5+5;
int n,m,q,a[N],po[N],lo[N],ro[N];
struct tree{
	int val,tag,l,r;//0:nothing 1:0 2:1
}t[N<<2];
void init(){
	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%d%d",po+i,lo+i,ro+i);
	scanf("%d",&q);
}
void pushup(int p){
	t[p].val=t[ls].val+t[rs].val;
}
void build(int p,int l,int r,int x){
	t[p].l=l,t[p].r=r;
	if(l==r) t[p].val=x<=a[l],t[p].tag=0;
	else {
		build(ls,l,mid,x);
		build(rs,mid+1,r,x);
		pushup(p);
	}
}
void pushdown(int p,int l,int r){
	if(t[p].tag==0) return;
	if(t[p].tag==2) {
		//t[p].val=r-l+1;
		t[ls].tag=2,t[rs].tag=2;
		t[ls].val=mid-l+1,t[rs].val=r-mid;
	}else if(t[p].tag==1){
		t[ls].val=t[rs].val=0;
		t[ls].tag=t[rs].tag=1;
	}
	t[p].tag=0;
	
}
void update(int p,int l,int r,int x,int y,int d){//[x,y]=d
	if(l>y||r<x) return;
	if(l>=x&&r<=y) {
		t[p].val=d*(r-l+1);
		t[p].tag=d+1;
		return;
	}
	pushdown(p,l,r);
	update(ls,l,mid,x,y,d);
	update(rs,mid+1,r,x,y,d);
	pushup(p);
}
int query(int p,int l,int r,int x,int y){
	if(l>y||r<x) return 0;
	if(l>=x&&r<=y) return t[p].val;
	pushdown(p,l,r);
	return query(ls,l,mid,x,y)+query(rs,mid+1,r,x,y);	
}
void debug(){
	for(int i=1;i<=n;i++) printf("%d ",query(1,1,n,i,i));
	puts("");
	for(int i=1;i<=n*4;i++) 
		if(t[i].l)
		printf("%d %d %d %d %d\n",i,t[i].l,t[i].r,t[i].tag,t[i].val);
//	printf("FUCK :%d %d %d",t,lo[i],ro[i]);
	puts("");
}
bool check(int x){
	build(1,1,n,x);
	for(int i=1;i<=m;i++) {
		int t=query(1,1,n,lo[i],ro[i]);
		update(1,1,n,lo[i],ro[i],0);
		//if(i==2) debug();
		if(po[i]==0) update(1,1,n,ro[i]-t+1,ro[i],1);
		else update(1,1,n,lo[i],lo[i]+t-1,1);
	//	printf("%d\n",t);
		//if(i==2) debug();
	}
	//update(1,1,n,4,6,0);
	for(int i=1;i<=n;i++) query(1,1,n,i,i);
//	puts("");
	return query(1,1,n,q,q);
}
int main(){
	init();
	
	int L=1,R=n;
	while(L<R){
		//printf("%d %d\n",L,R);
		int m=(L+R+1)>>1;
		if(check(m)) L=m;
		else R=m-1;
	}
	printf("%d\n",L);
//	for(int i=1;i<=n;i++) printf("%d ",query(1,1,n,i,i));
	return 0;
}

85行为什么删掉就错了?

2021/11/8 19:52
加载中...