比赛 20241025 评测结果 WWTTTTTTTW
题目名称 sequence 最终得分 0
用户昵称 flyfree 运行时间 14.550 s
代码语言 C++ 内存使用 56.42 MiB
提交时间 2024-10-25 11:15:23
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100000
#define MAXN 100010
inline ll read(){
	ll x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=x*10+c-'0';
		c=getchar();
	}
	return x*f;
}
struct node{
	ll l,r,sum;
	ll s[2];
}tr[MAXN*50];
ll t,n,q,idx;
ll a[MAXN],rot[MAXN];
void push_up(ll now){
	tr[now].sum=tr[tr[now].s[0]].sum+tr[tr[now].s[1]].sum;
}
void build(ll &now,ll l,ll r){
	now=++idx;
	tr[now].l=l,tr[now].r=r,tr[now].sum=0;
	if(l==r)return;
	ll mid=(l+r)/2;
	build(tr[now].s[0],l,mid);
	build(tr[now].s[1],mid+1,r);
}
void ad(ll &now,ll p,ll loc){
	now=++idx;
	tr[now].l=tr[p].l,tr[now].r=tr[p].r,tr[now].sum=tr[p].sum+1;
	if(tr[now].l==tr[now].r)return;
	ll mid=(tr[now].l+tr[now].r)/2;
	if(loc<=mid){
		tr[now].s[1]=tr[p].s[1];
		ad(tr[now].s[0],tr[p].s[0],loc);
	}else{
		tr[now].s[0]=tr[p].s[0];
		ad(tr[now].s[1],tr[p].s[1],loc);
	}
	push_up(now);
}
ll find1(ll l1,ll r1,ll l2,ll r2){//找第一个l1-r1大于l2-r2 
	if(!tr[l1].sum&&!tr[l2].sum&&!tr[r1].sum&&!tr[r2].sum)return -1;
//	cout<<tr[l1].l<<" "<<tr[l1].r<<" "<<tr[r1].sum-tr[l1].sum<<" "<<tr[r2].sum-tr[l2].sum<<endl;
	if(tr[l1].l==tr[l1].r){
		if(tr[r1].sum-tr[l1].sum==tr[r2].sum-tr[l2].sum)return -1;
		else if(tr[r1].sum-tr[l1].sum-(tr[r2].sum-tr[l2].sum)>1)return 0;
		else return tr[l1].l;
	}
	ll s10=tr[tr[r1].s[0]].sum-tr[tr[l1].s[0]].sum,s20=tr[tr[r2].s[0]].sum-tr[tr[l2].s[0]].sum;
	if(s10-s20>1||s10-s20<-1)return 0;
	else if(s10-s20==0){
		ll x=find1(tr[l1].s[0],tr[r1].s[0],tr[l2].s[0],tr[r2].s[0]);
		ll y=find1(tr[l1].s[1],tr[r1].s[1],tr[l2].s[1],tr[r2].s[1]);
		if(x==-1&&y==-1)return -1;
		else if(x==0||y==0)return 0;
		else if(x==-1)return y;
		else return x;
	}else if(s10-s20>0)return find1(tr[l1].s[0],tr[r1].s[0],tr[l2].s[0],tr[r2].s[0]);
	else return find1(tr[l1].s[1],tr[r1].s[1],tr[l2].s[1],tr[r2].s[1]);
}
ll find2(ll l1,ll r1,ll l2,ll r2){//找第一个l2-r2>l1-r1 
	if(!tr[l1].sum&&!tr[l2].sum&&!tr[r1].sum&&!tr[r2].sum)return -1;
	if(tr[l1].l==tr[l1].r){
		if(tr[r1].sum-tr[l1].sum==tr[r2].sum-tr[l2].sum)return -1;
		else if(tr[r1].sum-tr[l1].sum-(tr[r2].sum-tr[l2].sum)<-1)return 0;
		else return tr[l1].l;
	}
	ll s10=tr[tr[r1].s[0]].sum-tr[tr[l1].s[0]].sum,s20=tr[tr[r2].s[0]].sum-tr[tr[l2].s[0]].sum;
	if(s10-s20>1||s10-s20<-1)return 0;
	else if(s10==s20){
		ll x=find2(tr[l1].s[0],tr[r1].s[0],tr[l2].s[0],tr[r2].s[0]);
		ll y=find2(tr[l1].s[1],tr[r1].s[1],tr[l2].s[1],tr[r2].s[1]);
		if(x==-1&&y==-1)return -1;
		else if(x==0||y==0)return 0;
		else if(x==-1)return y;
		else return x;
	}else if(s10-s20>0)return find2(tr[l1].s[1],tr[r1].s[1],tr[l2].s[1],tr[r2].s[1]);
	else return find2(tr[l1].s[0],tr[r1].s[0],tr[l2].s[0],tr[r2].s[0]);
}
ll re(ll l1,ll r1,ll l,ll r){
	if(tr[l1].l>=l&&tr[l1].r<=r)return tr[r1].sum-tr[l1].sum;
	ll mid=(tr[l1].l+tr[l1].r)/2,ans=0;
	if(l<=mid)ans+=re(tr[l1].s[0],tr[r1].s[0],l,r);
	if(r>mid)ans+=re(tr[l1].s[1],tr[r1].s[1],l,r);
	return ans;
}
int main(){
	freopen("sequence.in","r",stdin);
	freopen("sequence.out","w",stdout);
	t=read();
	while(t--){
		n=read(),q=read(),idx=0;
		build(rot[0],1,N);
		for(int i=1;i<=n;i++){
			a[i]=read();
			ad(rot[i],rot[i-1],a[i]);
		}
		for(int i=1;i<=q;i++){
			ll l1=read(),r1=read(),l2=read(),r2=read();
			ll x=find1(rot[l1-1],rot[r1],rot[l2-1],rot[r2]),y=find2(rot[l1-1],rot[r1],rot[l2-1],rot[r2]);
//			cout<<x<<" "<<y<<endl;
			if(x==y&&x==-1)cout<<"YES\n";
			else if(x==0||y==0)cout<<"NO\n";
			else if(x==y)cout<<"YES\n";
			else{
				if(x>y)swap(x,y);
				ll o=re(rot[l1-1],rot[r1],x+1,y-1);
				if(o)cout<<"NO\n";
				else cout<<"YES\n";
			}
		}
	}
	return 0;
}