UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#201829#2988. 相似子串yizhiming1001696ms72844kbC++4.4kb2024-02-07 09:22:132024-02-07 15:09:54

answer

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#define int long long
using namespace std;
int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
const int N = 1e5+5;
const int Mod1 = 998244353;
const int Mod2 = 1e9+7;
int base=100003; 
int mi1[N],mi2[N],V=1e5;
struct seg{
	struct aa{
		int lc,rc,sz;//节点个数 
		int val1,val2;//哈希值 
	}node[N*40];
	int tot;
	aa merge(aa ls,aa rs,int l,int r){
		aa res;
		res.val1 = res.val2 = res.sz = 0;
		res.sz = ls.sz+rs.sz;
		int mid = (l+r)/2;
		int sz = r-mid;
		res.val1 = (ls.val1*mi1[sz]%Mod1+rs.val1)%Mod1;
		res.val2 = (ls.val2*mi2[sz]%Mod2+rs.val2)%Mod2;
//		cout<<"SZ:"<<sz<<" "<<ls.val1<<" "<<mi1[sz]<<" "<<rs.val1<<" "<<l<<" "<<r<<"\n";
		return res;
	}
	void pushup(int u,int l,int r){
		int ls = node[u].lc,rs = node[u].rc;
		node[u] = merge(node[ls],node[rs],l,r);
		node[u].lc = ls;node[u].rc = rs;
	}
	void ins(int &u,int v,int l,int r,int x){
		u = ++tot;
		node[u] = node[v];
		if(l==r){
			node[u].sz++;
			node[u].val1 = node[u].sz;
			node[u].val2 = node[u].sz;
			return;
		}
		int mid = (l+r)/2;
		if(x<=mid){
			ins(node[u].lc,node[v].lc,l,mid,x);
		}else{
			ins(node[u].rc,node[v].rc,mid+1,r,x);
		}
		pushup(u,l,r);
//		if(l==1&&r==V){
//			cout<<"H:"<<node[u].val1<<" "<<node[u].val2<<"\n";
//		}
	}
	int ask(int a,int b,int c,int d,int l,int r,int ll,int rr){//查询对应区间的第一个不相同的位置。 a<b,c<d
		if(ll>rr){
			return -1;
		}
		int res1 = (node[b].val1-node[a].val1+Mod1)%Mod1;
		int res2 = (node[d].val1-node[c].val1+Mod1)%Mod1;
		int res3 = (node[b].val2-node[a].val2+Mod2)%Mod2;
		int res4 = (node[d].val2-node[c].val2+Mod2)%Mod2;
//		cout<<"RES:"<<res1<<" "<<res2<<" "<<res3<<" "<<res4<<"\n";
//		cout<<"VAL:"<<node[a].val1<<" "<<node[b].val1<<" "<<node[c].val1<<" "<<node[d].val1<<"\n";
//		cout<<"SIZ:"<<node[a].sz<<" "<<node[b].sz<<" "<<node[c].sz<<" "<<node[d].sz<<"\n";
		if(res1==res2&&res3==res4){
			return -1;
		}
		if(l==r){
			int sz1 = node[b].sz-node[a].sz,sz2 = node[d].sz-node[c].sz;
			if(sz1==sz2){
				return -1;
			}
			if(abs(sz1-sz2)>1){
				return -2;
			}
			if(sz1>sz2){
				return l;
			}else{
				return l+V;
			}
//			return l;
		}
		int mid = (l+r)/2;
		if(rr<=mid){
			return ask(node[a].lc,node[b].lc,node[c].lc,node[d].lc,l,mid,ll,rr);
		}else if(ll>mid){
			return ask(node[a].rc,node[b].rc,node[c].rc,node[d].rc,mid+1,r,ll,rr);
		}else{
			int u = ask(node[a].lc,node[b].lc,node[c].lc,node[d].lc,l,mid,ll,mid);
			if(u==-1){
				return ask(node[a].rc,node[b].rc,node[c].rc,node[d].rc,mid+1,r,mid+1,rr);
			}
			return u;
		}
	}
	int qry(int a,int b,int c,int d,int l,int r,int ll,int rr){
		if(ll>rr){
			return 0;
		}
		if(l==ll&&r==rr){
			return node[b].sz-node[a].sz+node[d].sz-node[c].sz;
		}
		int mid = (l+r)/2;
		if(rr<=mid){
			return qry(node[a].lc,node[b].lc,node[c].lc,node[d].lc,l,mid,ll,rr);
		}else if(ll>mid){
			return qry(node[a].rc,node[b].rc,node[c].rc,node[d].rc,mid+1,r,ll,rr);
		}else{
			return qry(node[a].lc,node[b].lc,node[c].lc,node[d].lc,l,mid,ll,mid)+qry(node[a].rc,node[b].rc,node[c].rc,node[d].rc,mid+1,r,mid+1,rr);
		}
	}
}T;
int n,q;
int rt[N];
signed main(){
//	freopen("ex_a2.in","r",stdin);
//	freopen("me.txt","w",stdout);
	n = read();q = read();
	mi1[0] = 1;mi2[0] = 1; 
	for(int i=1;i<=V;i++){
		mi1[i] = mi1[i-1]*base%Mod1;
		mi2[i] = mi2[i-1]*base%Mod2;
	}
	for(int i=1;i<=n;i++){
		int x = read();
		T.ins(rt[i],rt[i-1],1,V,x);
	}
	
	while(q--){
		int a,b,c,d;
		a = read();b = read();c = read();d = read();
		int u = T.ask(rt[a-1],rt[b],rt[c-1],rt[d],1,V,1,V);
//		cout<<"U:"<<u<<" "<<rt[a-1]<<" "<<rt[b]<<" "<<rt[c-1]<<" "<<rt[d]<<"\n";
		if(u==-1){
			cout<<"YES\n";
			continue;
		}
		if(u==-2){
			cout<<"NO\n";
			continue;
		}
		int x = T.ask(rt[a-1],rt[b],rt[c-1],rt[d],1,V,(u>V?u-V:u)+1,V);
		if(x<0){
			cout<<"NO\n";
			continue;
		}
		if((u>V)+(x>V)!=1){
			cout<<"NO\n";
			continue;
		}
		int res = T.qry(rt[a-1],rt[b],rt[c-1],rt[d],1,V,(u>V?u-V:u)+1,(x>V?x-V:x)-1);
		if(res>0){
			cout<<"NO\n";
			continue;
		}
		int y = T.ask(rt[a-1],rt[b],rt[c-1],rt[d],1,V,(x>V?x-V:x)+1,V);
		if(y==-1){
			cout<<"YES\n";
		}else{
			cout<<"NO\n";
		}
	}
    return 0;
}
/*
6 1
1 3 4 2 3 4
1 2 5 6
*/

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 3436kb

input:

1000 1000
40025 48458 97086 78415 76938 71808 52491 96467 81990 89029 88412 76797 44759 80073 43635 ...

output:

YES
YES
YES
YES
YES
NO
YES
YES
YES
NO
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
NO...

result:

ok 1000 tokens

Test #2:

score: 10
Accepted
time: 2ms
memory: 3436kb

input:

1000 1000
69744 92174 63952 36902 40748 32933 60242 43020 12822 18583 84269 33755 13117 75801 5871 8...

output:

YES
YES
YES
YES
YES
YES
YES
NO
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
NO
YES
YES
YES
YES
YES
YES
YE...

result:

ok 1000 tokens

Test #3:

score: 10
Accepted
time: 218ms
memory: 72844kb

input:

100000 100000
8 4 17 8 4 10 19 3 14 18 11 10 8 2 7 18 2 8 18 17 16 3 18 15 15 16 5 9 20 12 6 11 20 1...

output:

NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
N...

result:

ok 100000 tokens

Test #4:

score: 10
Accepted
time: 139ms
memory: 72580kb

input:

100000 100000
1192 1192 45430 45430 44692 44692 46878 46878 68670 68670 69305 69305 80469 80469 2221...

output:

NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
N...

result:

ok 100000 tokens

Test #5:

score: 10
Accepted
time: 123ms
memory: 72612kb

input:

100000 100000
64728 33717 46555 82262 17578 99121 39183 29026 36451 3512 24307 55525 60881 71398 220...

output:

YES
YES
YES
YES
YES
YES
YES
NO
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
NO
YES
YES
YES
YES
YES
NO
NO
...

result:

ok 100000 tokens

Test #6:

score: 10
Accepted
time: 171ms
memory: 72520kb

input:

100000 100000
2030 29575 55708 7893 41387 2946 55708 41387 41387 84734 68661 51149 84734 51149 40070...

output:

NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
N...

result:

ok 100000 tokens

Test #7:

score: 10
Accepted
time: 303ms
memory: 72640kb

input:

100000 100000
77584 83199 12852 23312 51680 90179 35136 78375 61547 49389 44615 86507 45244 24298 21...

output:

NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
...

result:

ok 100000 tokens

Test #8:

score: 10
Accepted
time: 304ms
memory: 72596kb

input:

100000 100000
48446 18460 31385 83942 29144 78217 71839 76045 45132 62950 10261 80012 69105 88259 18...

output:

NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO...

result:

ok 100000 tokens

Test #9:

score: 10
Accepted
time: 296ms
memory: 72616kb

input:

100000 100000
67258 22476 80140 65110 15276 45289 52581 78244 71886 4975 93295 27696 50915 43339 811...

output:

NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO...

result:

ok 100000 tokens

Test #10:

score: 10
Accepted
time: 140ms
memory: 72664kb

input:

100000 100000
93192 79039 80188 16618 93192 16618 93659 79039 28122 14377 93192 79039 59195 70654 36...

output:

NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
N...

result:

ok 100000 tokens

Extra Test:

score: 0
Extra Test Passed