/ 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:27: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:35: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:43:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int m = r+l >> 1,ans = N;
             ~^~
/in/foo.cc: In function 'int main()':
/in/foo.cc:79: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:79: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:79: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 33ms 2.727 MiB
#2 Accepted 26ms 2.625 MiB
#3 Accepted 29ms 3.84 MiB
#4 Accepted 26ms 2.125 MiB
#5 Accepted 35ms 3.48 MiB
#6 Accepted 22ms 1.855 MiB
#7 Accepted 20ms 3.25 MiB
#8 Accepted 55ms 3.25 MiB
#9 Accepted 62ms 4.613 MiB
#10 Accepted 59ms 5.625 MiB
#11 Accepted 23ms 3.875 MiB
#12 Accepted 24ms 2.328 MiB
#13 Accepted 26ms 4.25 MiB
#14 Accepted 54ms 3.469 MiB

代码

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

using namespace std;

const int N = 1e5+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;
}

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 &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++)if(deg[i] < 2)update(1,1,n,i,L[i]);
        // for(int i=1;i<=n;i++) printf("%d [%d %d]\n",i, L[i],R[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;
}
/***

Input

3
7 4
0 0
1 3
0 0
2 5
6 7
0 0
0 0

7 4
5 7
6 3
0 0
2 1
0 0
0 0
0 0

7 4
0 0
6 3
0 0
2 5
1 7
0 0
0 0

Output

0
1 2 3 4 6 5 7
1
3 2 6 4 5 1 7
2
1 5 7 4 3 2 6
***/


信息

递交者
类型
递交
题目
tabris is SirBat 1 (**数据范围有改动**) 在这个oj后三组数据会栈溢出 最好手工栈
语言
C++
递交时间
2017-11-21 16:33:52
评测时间
2017-11-21 16:35:36
评测机
分数
1792
总耗时
499ms
峰值内存
5.625 MiB