QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#321802#7036. Can You Solve the Harder Problem?zxcenWA 1342ms107544kbC++173.2kb2024-02-05 16:52:252024-02-05 16:52:25

Judging History

This is the latest submission verdict.

  • [2024-02-05 16:52:25]
  • Judged
  • Verdict: WA
  • Time: 1342ms
  • Memory: 107544kb
  • [2024-02-05 16:52:25]
  • Submitted

answer

#include<bits/stdc++.h>
#define ll long long

using namespace std;
const int N=2e5;
struct QUE{
	int l,r;
};
int T;
int n;
int a[N+10];
vector<QUE>que[N+10];
struct SGT{
	struct node{
		int maxn;
		ll sum,cov;
	}tree[(N+10)<<2];
	inline void cover(int rt,int l,int r,ll x){
		tree[rt].maxn=x;
		tree[rt].sum=(r-l+1)*x;
		tree[rt].cov=x;
	}
	inline void pushup(int rt){
		tree[rt].maxn=max(tree[rt<<1].maxn,tree[rt<<1|1].maxn);
		tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
	}
	inline void pushdown(int rt,int l,int r){
		if(tree[rt].cov){
			int mid=(l+r)>>1;
			cover(rt<<1,l,mid,tree[rt].cov);
			cover(rt<<1|1,mid+1,r,tree[rt].cov);
			tree[rt].cov=0;
		}
	}
	inline void init(int l,int r,int rt){
		tree[rt]={0,0,0};
		if(l==r){
			return ;
		}
		int mid=(l+r)>>1;
		init(l,mid,rt<<1);
		init(mid+1,r,rt<<1|1);
	}
	inline int find(int l,int r,int x,int rt){
		if(l==r){
			return l;
		}
		pushdown(rt,l,r);
		int mid=(l+r)>>1;
		if(tree[rt<<1].maxn<x){
			return find(l,mid,x,rt<<1);
		}
		else if(tree[rt<<1|1].maxn<x){
			return find(mid+1,r,x,rt<<1|1);
		}
		return 0;
	}
	inline void update(int l,int r,int pl,int pr,int x,int rt){
		if(pl<=l&&r<=pr){
			cover(rt,l,r,x);
			return ;
		}
		pushdown(rt,l,r);
		int mid=(l+r)>>1;
		if(pl<=mid){
			update(l,mid,pl,pr,x,rt<<1);
		}
		if(pr>mid){
			update(mid+1,r,pl,pr,x,rt<<1|1);
		}
		pushup(rt);
	}
	inline ll query(int l,int r,int pl,int pr,int rt){
		if(pl<=l&&r<=pr){
			return tree[rt].sum;
		}
		pushdown(rt,l,r);
		int mid=(l+r)>>1;
		ll res=0;
		if(pl<=mid){
			res+=query(l,mid,pl,pr,rt<<1);
		}
		if(pr>mid){
			res+=query(mid+1,r,pl,pr,rt<<1|1);
		}
		return res;
	}
}sgt;
struct SAM{
	int idx=0,lst=0;
	unordered_map<int,int>son[(N+10)<<1];
	int link[(N+10)<<1]={-1},len[(N+10)<<1],ep[(N+10)<<1];
	inline void insert(int num,int pos){
		int u=++idx,v=lst;
		son[u].clear();
		len[u]=len[lst]+1;
		ep[u]=pos;
		while(~v&&!son[v][num]){
			son[v][num]=u;
			v=link[v];
		}
		if(~v){
			int w=son[v][num];
			if(len[w]!=len[v]+1){
				int x=++idx;
				son[x].clear();
				for(auto k:son[w]){
					son[x][k.first]=k.second;
				}
				link[x]=link[w];
				len[x]=len[v]+1;
				ep[x]=pos;
				while(~v&&son[v][num]==w){
					son[v][num]=x;
				}
				link[u]=x;
				link[w]=x;
			}
			else{
				link[u]=w;
			}
		}
		else{
			link[u]=0;
		}
		lst=u;
	}
	inline void init_que(){
		for(int i=1;i<=idx;++i){
			que[ep[i]].push_back({ep[i]-len[i]+1,ep[i]-len[link[i]]});
		}
	}
}sam;
int main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin>>T;
	while(T--){
		cin>>n;
		for(int i=1;i<=n;++i){
			cin>>a[i];
		}
		sam.idx=0;
		sam.lst=0;
		sam.son[0].clear();
		for(int i=1;i<=n;++i){
			sam.insert(a[i],i);
		}
		for(int i=1;i<=n;++i){
			que[i].clear();
		}
		sam.init_que();
		sgt.init(1,n,1);
		ll ans=0;
		for(int i=1;i<=n;++i){
			int pos=sgt.find(1,n,a[i],1);
			if(1<=pos&&pos<=i){
				sgt.update(1,n,pos,i,a[i],1);
			}
			for(auto x:que[i]){
				ans+=sgt.query(1,n,x.l,x.r,1);
			}
		}
		cout<<ans<<'\n';
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 35116kb

input:

2
3
1 2 3
3
2 3 3

output:

14
14

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 1342ms
memory: 107544kb

input:

1000
407
205460 200518 200561 199235 198814 198136 198802 206763 198795 200705 205862 209366 204017 209481 206300 198499 197364 200897 208928 196983 205605 206396 205140 201050 199886 207314 196947 207905 204999 203288 200298 198594 198286 197714 206506 203962 197628 201127 206380 199090 200711 2063...

output:

7841107376
354955221046
1088247369
129273675507
172459416330
206491543604
101566551494
205346976871
105210739061
202675597683
28140440273
132048041765
180365844391
60746643625
112836541469
341551996780
225124509622
287229509522
69566525055
7176074628
270162755037
73762176306
291580614777
83124040688...

result:

wrong answer 1st lines differ - expected: '17377263880', found: '7841107376'