1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <cstdio>
#include <map>
#include <iostream>
#include <string>
#include <queue>

using namespace std;
#define MAX 500200

typedef long long I;

int n;

#define D(x) 
#define DR(x) 

#define P 1000000009LL

struct T {
    int o;
    int l=0,p=0;
};

T w[2][MAX];

int print(int k = 0) {
    for(int i=0;i<=n;i++) {
        cout << w[k][i].o << " ";
    }
    cout << "\n";
    for(int i=0;i<=n;i++) {
        cout << w[k][i].l << " ";
    }
    cout << "\n";
    for(int i=0;i<=n;i++) {
        cout << w[k][i].p << " ";
    }
    cout << "\n";
}

string tab(int t) {
    return string(t*2, ' ');
}

struct Stack2 {
    int dir, last_l, last_p, n,t;
};


I toLR(int dir, T *ww, int last_l,int last_p,int n, int t = 0) {
    I res = 0;

    queue<Stack2> q;
    q.push(Stack2{dir, last_l, last_p,n,t});

    while(!q.empty()) {
        auto s = q.front();
        q.pop();
        dir = s.dir; last_l = s.last_l; last_p = s.last_p; n = s.n; t = s.t;

        if(last_l<=n && n<=last_p) {
            if(ww[n].l) q.push(Stack2{-1,last_l,n-1, ww[n].l,t+1});
            if(ww[n].p) q.push(Stack2{ 1,n+1,last_p, ww[n].p,t+1});
            
            if(dir == -1) {
                if(ww[n].p) res = (res + (last_p-n)) % P;
            } else {
                if(ww[n].l) res = (res + (n-last_l)) % P;
            }
        }
    }
    return res;
}

I toRight(T *ww, int last_l,int last_p,int n, int t =0) {
    return toLR( 1, ww, last_l, last_p, n, t);
}
I toLeft(T *ww, int last_l,int last_p,int n, int t = 0) {
    return toLR(-1, ww, last_l, last_p, n, t);
}

struct Stack {
    int last_l, last_p, r0,r1,t;
};


I policz(int last_l, int last_p, int r0, int r1, int t = 0) {
    I res = 0;

    queue<Stack> q;
    q.push(Stack{last_l, last_p, r0, r1, t});

    while(!q.empty()) {
        auto s = q.front();
        q.pop();
        last_l = s.last_l; last_p = s.last_p; r0 = s.r0; r1 = s.r1; t = s.t;

        D(cout << tab(t) << "pol: " << last_l<< "-" << r0 << "," << r1 << "-" << last_p << "\n");

        if(r0 == 0 || r1 == 0) continue;
        
        int ir0 = r0, ir1=r1, tmp;
        //Prawo
        while(ir0!=ir1) {
            if(ir0<ir1) {
                tmp = w[0][ir0].p;
                I add = toLeft(w[0], ir0+1, tmp-1, w[0][tmp].l) + tmp - ir0 -1;
                D(cout << tab(t) << "PR0: ir0: " << ir0 << "->" << tmp << " res+=" << add << "\n");
                res = (res + add) % P;
                ir0 = tmp;
            }
            if(ir1<ir0) {
                tmp = w[1][ir1].p;
                I add = toLeft(w[1], ir1+1, tmp-1, w[1][tmp].l) + tmp - ir1 - 1;
                D(cout << tab(t) << "PR1: ir1: " << ir1 << "->" << tmp << " res+=" << add << "\n");
                res = (res + add) % P;
                ir1 = tmp;
            }
        }
        //Lewo
        int jr0 = r0, jr1=r1;
        while(jr0!=jr1) {
            if(jr1<jr0) {
                tmp = w[0][jr0].l;
                I add = toRight(w[0], tmp+1, jr0-1, w[0][tmp].p) + jr0 - tmp -1;
                D(cout << tab(t) << "LE0: jr0: " << jr0 << "->" << tmp << " res+=" << add << "\n");
                res = (res + add) % P;
                jr0 = tmp;
            }
            if(jr0<jr1) {
                tmp = w[1][jr1].l;
                I add = toRight(w[1], tmp+1, jr1-1, w[1][tmp].p) + jr1 - tmp -1;
                D(cout << tab(t) << "LE1: jr1: " << jr1 << "->" << tmp << " res+=" << add << "\n");
                res = (res + add) % P;
                jr1 = tmp;
            }
        }
        res = (res + abs(r1-r0)) % P;
        D(cout << tab(t) << "[" << ir1 << "," << jr1 << "] = " << res << "\n");
        q.push(Stack{last_l, jr0-1, w[0][jr0].l, w[1][jr1].l, t+1});
        q.push(Stack{ir1+1, last_p, w[0][ir0].p, w[1][ir1].p, t+1});
    }
    return res;
}

int main() {

    scanf("%d", &n);
    for(int i=1;i<=n;i++){
        scanf("%d", &w[0][i].o);
    } 
    for(int i=1;i<=n;i++) {
        scanf("%d", &w[1][i].o);
    }

    for(int k=0;k<2;k++) {
        for(int i=1;i<=n;i++) {
            int o = w[k][i].o;
            if(o == -1) o = 0;
            if(o>i) w[k][o].l = i;
            else  w[k][o].p = i;
        }
    }

    D(print(0);
    cout << "1:-------\n";
    print(1);
    )

    I res = policz(1,n, w[0][0].p, w[1][0].p);
    cout << res << "\n";
    return 0;
}