/ tabris /

记录详情

Accepted

/in/foo.cc:1:0: warning: ignoring #pragma comment  [-Wunknown-pragmas]
 #pragma comment(linker, "/STACK:102400000000,102400000000") 
 
/in/foo.cc: In function 'void build(int, int, int)':
/in/foo.cc:41:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int m = r+l >> 1;
             ~^~
/in/foo.cc: In function 'void update(int, int, int, int, int)':
/in/foo.cc:48:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int m = r+l >> 1;
             ~^~
/in/foo.cc: In function 'int query(int, int, int, int, int)':
/in/foo.cc:55:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int m = r+l >> 1,ans = N;
             ~^~
/in/foo.cc: In function 'int main()':
/in/foo.cc:108:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=1;i<v.size();i++) printf(" %d", v[i]); puts("");
                     ~^~~~~~~~~
/in/foo.cc:108:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
         for(int i=1;i<v.size();i++) printf(" %d", v[i]); puts("");
         ^~~
/in/foo.cc:108:58: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the 'for'
         for(int i=1;i<v.size();i++) printf(" %d", v[i]); puts("");
                                                          ^~~~
# 状态 耗时 内存占用
#1 Accepted 23ms 12.48 MiB
#2 Accepted 54ms 12.629 MiB
#3 Accepted 58ms 14.727 MiB
#4 Accepted 40ms 12.5 MiB
#5 Accepted 361ms 23.367 MiB
#6 Accepted 380ms 23.492 MiB
#7 Accepted 345ms 21.367 MiB
#8 Accepted 354ms 21.367 MiB

代码

#pragma comment(linker, "/STACK:102400000000,102400000000") 
#include <bits/stdc++.h>

using namespace std;

const int N = 5e5+8;
/****************************************************/

int ch[N][2],mn[N<<2];
int deg[N];
int n,m,ans;
int L[N],R[N],cnt;

// void dfs(int &u){if(!u) return ;
//     L[u] = ++ cnt;
//     dfs(ch[u][0]),dfs(ch[u][1]);
//     R[u] = cnt;
// }

int mystack[N],cur[N];

void Dfs(int m){
    int len = 0;mystack[++len]=m;cur[m]=0;
    for(int u;len;){
        u = mystack[len];
        // printf("%d <--\n",u);
        if(cur[u]==0) L[u] = ++ cnt;             // 第一次遍历到当前节点
        if(cur[u]>=2) {R[u]=cnt,--len;continue;} // 最后一次
        int &to = ch[u][cur[u]];                 // 遍历子节点
        if(to) mystack[++len] = to, cur[to] = 0;
        cur[u]++;
    }
}

void pushup(int rt){
    mn[rt]=min(mn[rt<<1],mn[rt<<1|1]);
}
void build(int rt,int l,int r){
    mn[rt] = N;
    if(l == r) return ;
    int m = r+l >> 1;
    build(rt<<1  ,l  ,m);
    build(rt<<1|1,m+1,r);
    pushup(rt);
}
void update(int rt,int l,int r,int v,int p){
    if(l==r){mn[rt] = v; return ; }
    int m = r+l >> 1;
    if(p<=m) update(rt<<1  ,l  ,m,v,p);
    else     update(rt<<1|1,m+1,r,v,p);
    pushup(rt); return ;
}
int query(int rt,int l,int r,int L,int R){
    if(L<=l&&r<=R) return mn[rt];
    int m = r+l >> 1,ans = N;
    if(L<=m) ans = min(ans,query(rt<<1  ,l  ,m,L,R));
    if(R> m) ans = min(ans,query(rt<<1|1,m+1,r,L,R));
    return ans;
}

vector<int>v;
int z , y;
void Dfs1(int m){
    int len = 0;mystack[++len]=m;cur[m]=0;
    for(int u;len;){
        u = mystack[len];
        if(cur[u]==0) {
            if(ch[u][0]) z = query(1,1,n,L[ch[u][0]],R[ch[u][0]]);else z = u;
            if(ch[u][1]) y = query(1,1,n,L[ch[u][1]],R[ch[u][1]]);else y = u;
            if(y < z) swap(ch[u][0],ch[u][1]),ans++;
        }
        if(cur[u]==1) {v.push_back(u);}
        if(cur[u]>=2) {--len;continue;}
        int &to = ch[u][cur[u]];
        if(to) mystack[++len] = to, cur[to] = 0;
        cur[u]++;
    }
}
// void dfs1(int &u){if(!u) return ;
//     if(ch[u][0]) z = query(1,1,n,L[ch[u][0]],R[ch[u][0]]);else z = u;
//     if(ch[u][1]) y = query(1,1,n,L[ch[u][1]],R[ch[u][1]]);else y = u;

//     // printf("%d <--- %d %d %d %d\n", u,z,y,ch[u][0],ch[u][1]);
//     if(y < z) swap(ch[u][0],ch[u][1]),ans++;
//     dfs1(ch[u][0]);
//     v.push_back(u);
//     dfs1(ch[u][1]);
// }
int main(){
    //freopen("tiS1_6.in","r",stdin);
    //freopen("tiS1_6.out","w",stdout);
    
    while(~scanf("%d%d",&n,&m)){
        v.clear(); ans = 0; cnt = 0;
        build(1,1,n);
        for(int i=1;i<=n;i++) {
            scanf("%d%d",&ch[i][0],&ch[i][1]);
            deg[i] = 0;
            if(ch[i][0] != 0) deg[i] ++;
            if(ch[i][1] != 0) deg[i] ++;
        }
        Dfs(m);
        // for(int i=1;i<=n;i++) printf("%d [%d %d]\n",i, L[i],R[i] );

        for(int i=1;i<=n;i++)if(deg[i] < 2)update(1,1,n,i,L[i]);
        Dfs1(m);
        printf("%d\n%d",ans,v[0]);
        for(int i=1;i<v.size();i++) printf(" %d", v[i]); puts("");
    }
    return  0;
}

信息

递交者
类型
递交
题目
tabris is SirBat 1 (**数据范围有改动**) 在这个oj后三组数据会栈溢出 最好手工栈
语言
C++
递交时间
2017-11-23 17:45:18
评测时间
2017-11-23 17:52:58
评测机
分数
1024
总耗时
1619ms
峰值内存
23.492 MiB