QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#577228#9313. Make MaxwanghuiminRE 0ms0kbC++231.4kb2024-09-20 09:27:082024-09-20 09:27:09

Judging History

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

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

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: 0
Dangerous Syscalls

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: