QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#323172 | #7036. Can You Solve the Harder Problem? | dcytrl | RE | 0ms | 15916kb | C++14 | 4.7kb | 2024-02-08 19:03:27 | 2024-02-08 19:03:28 |
Judging History
answer
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define FlushIn fread(Fread::ibuf,1,1<<21,stdin)
#define FlushOut fwrite(Fwrite::obuf,1,Fwrite::S-Fwrite::obuf,stdout)
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P__ puts("")
#define PU puts("--------------------")
#define popc __builtin_popcount
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define lowb lower_bound
#define uppb upper_bound
#define rep(a,b,c) for(int a=(b);a<=(c);a++)
#define per(a,b,c) for(int a=(b);a>=(c);a--)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=d)
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=d)
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) (x&-x)
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mem(x,y) memset(x,y,sizeof x)
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
typedef long long i64;
bool greating(int x,int y){return x>y;}
namespace Fread{
const int SIZE=1<<21;
char ibuf[SIZE],*S,*T;
inline char getc(){if(S==T){T=(S=ibuf)+fread(ibuf,1,SIZE,stdin);if(S==T)return '\n';}return *S++;}
}
namespace Fwrite{
const int SIZE=1<<21;
char obuf[SIZE],*S=obuf,*T=obuf+SIZE;
inline void flush(){fwrite(obuf,1,S-obuf,stdout);S=obuf;}
inline void putc(char c){*S++=c;if(S==T)flush();}
struct NTR{~NTR(){flush();}}ztr;
}
/*#ifdef ONLINE_JUDGE
#define getchar Fread::getc
#define putchar Fwrite::putc
#endif*/
inline int rd(){
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-48;ch=getchar();}return x*f;
}
inline void write(i64 x,char ch='\0'){
if(x<0){x=-x;putchar('-');}
int y=0;char z[40];
while(x||!y){z[y++]=x%10+48;x/=10;}
while(y--)putchar(z[y]);if(ch!='\0')putchar(ch);
}
bool Mbg;
const int maxn=2e5+5,maxm=1e6+5,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,a[maxn],b[maxn];
int sa[maxn],rk[maxn],hi[maxn];
int id[maxn],nrk[maxn],prk[maxn<<1],cnt[maxn];
bool qwq(int x,int y,int z){return prk[x]==prk[y]&&prk[x+z]==prk[y+z];}
void SA(){
rep(i,1,n)b[i]=a[i];
sort(b+1,b+n+1);int m=unique(b+1,b+n+1)-(b+1);
rep(i,1,m)cnt[i]=0;
rep(i,1,n)cnt[rk[i]=lower_bound(b+1,b+m+1,a[i])-b]++;
rep(i,2,m)cnt[i]+=cnt[i-1];
per(i,n,1)sa[cnt[rk[i]]--]=i;
for(int j=1,p;j<n;j<<=1,m=p){
p=0;rep(i,n-j+1,n)id[++p]=i;
rep(i,1,n)if(sa[i]>j)id[++p]=sa[i]-j;
rep(i,1,m)cnt[i]=0;
rep(i,1,n)cnt[nrk[i]=rk[id[i]]]++;
rep(i,2,m)cnt[i]+=cnt[i-1];
per(i,n,1)sa[cnt[nrk[i]]--]=id[i];
rep(i,1,n)prk[i]=rk[i];
p=0;rep(i,1,n)rk[sa[i]]=qwq(sa[i],sa[i-1],j)?p:++p;
if(p==n)break;
}
int h=0;
rep(i,1,n){
if(h)--h;
while(a[i+h]==a[sa[rk[i]-1]+h])++h;
hi[rk[i]]=h;
}
}
int rht[maxn],sta[maxn],tp;
void Init(){
rep(i,1,n)rht[i]=n+1;
tp=0;rep(i,1,n){
while(tp&&a[i]>=a[sta[tp]])rht[sta[tp]]=i,tp--;
sta[++tp]=i;
}
}
i64 d[maxn<<2];
int tag[maxn<<2];
#define mid (l+r>>1)
void pu(int p){d[p]=d[lson(p)]+d[rson(p)];}
void pt(int p,int l,int r,int v){d[p]=1ll*(r-l+1)*v,tag[p]=v;}
void pd(int p,int l,int r){
if(tag[p]){
pt(lson(p),l,mid,tag[p]),pt(rson(p),mid+1,r,tag[p]);
tag[p]=0;
}
}
void bd(int l,int r,int p){
d[p]=tag[p]=0;
if(l==r)return;
bd(l,mid,lson(p)),bd(mid+1,r,rson(p));
}
void upd(int ll,int rr,int v,int l,int r,int p){
if(ll>rr)return;
if(ll<=l&&r<=rr)return pt(p,l,r,v);
pd(p,l,r);
if(ll<=mid)upd(ll,rr,v,l,mid,lson(p));
if(rr>mid)upd(ll,rr,v,mid+1,r,rson(p));
pu(p);
}
i64 qry(int ll,int rr,int l,int r,int p){
if(ll>rr)return 0;
if(ll<=l&&r<=rr)return d[p];
pd(p,l,r);i64 res=0;
if(ll<=mid)res+=qry(ll,rr,l,mid,lson(p));
if(rr>mid)res+=qry(ll,rr,mid+1,r,rson(p));
return res;
}
#undef mid
void solve_the_problem(){
n=rd();rep(i,1,n)a[i]=rd();
rep(i,1,n)if(a[i]>=1000000)exit(114);
SA(),Init(),bd(1,n,1);
i64 ans=0;
per(i,n,1){
upd(i,rht[i]-1,a[i],1,n,1);
int h=i+hi[rk[i]];
ans+=qry(h,n,1,n,1);
}
write(ans,10);rep(i,1,n)a[i]=0;
}
bool Med;
signed main(){
// freopen(".in","r",stdin);freopen(".out","w",stdout);
// fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
int _=rd();while(_--)solve_the_problem();
}
/*
*/
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 15916kb
input:
2 3 1 2 3 3 2 3 3
output:
14 14
result:
ok 2 lines
Test #2:
score: -100
Runtime Error
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:
17377263880 484769420621 1469039857 473739194467 490764968536 305434410403 488123854116 494441224927 484272937511 482411215077 34438338505 225764586244 494494400870 492874976740 486779601551 483167680203 341192630235 492747353987 491280077388 9765154255 491745355442 120812820486 487737922489 4725166...