QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#577231#9313. Make MaxwanghuiminRE 0ms3800kbC++171.4kb2024-09-20 09:28:312024-09-20 09:28:31

Judging History

你现在查看的是最新测评结果

  • [2024-09-20 09:28:31]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3800kb
  • [2024-09-20 09:28:31]
  • 提交

answer

#include<bits/stdc++.h>
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define pc putchar
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48),ch=getchar();}
    return x*f;
}
using namespace std;
const int maxn=2e5+5;
int n;
int main() {
    int T=read();
    while(T--){
        n=read();
        vector<int>a(n+5);
        for(int i=1;i<=n;i++) a[i]=read();
        //现在我们要建笛卡尔树。
        vector<int>stk(n+4);int top=0;
        vector<int>ls(n+4),rs(n+4);
        vector<int>ans(n+4);
        for(int i=1;i<=n;i++){
            int pre=0;
            while(top&&stk[a[top]]<a[i]) pre=stk[top],stk[top--]=0;
            if(top){
                rs[stk[top]]=i;
            }
            if(pre){
                ls[i]=pre;
            }
            stk[++top]=i;
            // for(int j=1;j<=top;j++) cout<<stk[j]<<" ";pc('\n');
        }
        int ret=0;
        auto dfs =[&](auto &&dfs,int x)->void{
            // cout<<"x:"<<x<<" a[x]:"<<a[x]<<" ls[x]:"<<ls[x]<<" rs[x]:"<<rs[x]<<endl;
            if(ls[x]) ans[ls[x]]=ans[x]+(a[x]!=a[ls[x]]),dfs(dfs,ls[x]);
            if(rs[x]) ans[rs[x]]=ans[x]+(a[x]!=a[rs[x]]),dfs(dfs,rs[x]);
            ret+=ans[x];
        };
        dfs(dfs,stk[1]);
        cout<<ret<<endl;
    }
    // system("pause");
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4
2
1 2
2
2 2
7
1 1 1 2 2 2 2
3
1 2 3

output:

1
0
3
3

result:

ok 4 number(s): "1 0 3 3"

Test #2:

score: -100
Runtime Error

input:

2
198018
875421126 585870339 471894633 383529988 625397685 944061047 704695631 105113224 459022561 760848605 980735314 847376362 980571959 329939331 644635272 326439858 752879510 837384394 175179068 182094523 397239381 1199016 185143405 279638454 252374970 822030887 860312140 137248166 993229443 164...

output:


result: