#include<bits/stdc++.h> #define lim 500009 #define mk make_pair #define ff first #define sc second using namespace std; typedef unsigned int uint; const int md = 1000000007; int n; int t[2][lim][3]; void akt(int ii,int i) { if((t[ii][i][1]>0)&&(t[ii][t[ii][i][1]][0]!=i)) { t[ii][i][1] = t[ii][t[ii][i][1]][0]; akt(ii,i); } if((t[ii][i][2]>0)&&(t[ii][t[ii][i][2]][0]!=i)) { t[ii][i][2] = t[ii][t[ii][i][2]][0]; akt(ii,i); } } long long rem(int dir,int ii,int i) { if(i == 0) { return 0; } long long wyn = abs(t[ii][i][0]-i)-1; //printf("%lld %d %d %d\n", wyn, dir, ii, i); if(dir) { wyn = (wyn+rem(dir,ii,t[ii][i][1]))%md; wyn = (wyn+rem(dir,ii,t[ii][i][2]))%md; t[ii][i][0] = (n+i)%(n+1); t[ii][i][1] = 0; t[ii][i][2] = (i+1)%(n+1); } else { wyn = (wyn+rem(dir,ii,t[ii][i][1]))%md; wyn = (wyn+rem(dir,ii,t[ii][i][2]))%md; t[ii][i][0] = (i+1)%(n+1); t[ii][i][1] = (n+i)%(n+1); t[ii][i][2] = 0; } return wyn; } long long rot(int dir,int ii,int i) { long long wyn = 0; //printf("%d %d %d %d\n", dir, ii, i, t[ii][i][dir+1]); if(dir) { if((i==0)||(t[ii][i][2]==0)) { exit(0); } wyn = rem(dir,0,t[ii][t[ii][i][2]][1])+abs(i-t[ii][i][2])-1; if(wyn>0) { t[ii][t[ii][i][2]][0] = t[ii][i][2]-1; t[ii][t[ii][i][2]][1] = 0; t[ii][i][2] = i+1; } int pom = t[ii][i][2]; t[ii][pom][0] = t[ii][i][0]; t[ii][i][0] = pom; t[ii][i][2] = 0; t[ii][pom][1] = i; akt(ii,t[ii][pom][0]); } else { if((i==0)||(t[ii][i][1]==0)) { exit(0); } wyn = rem(dir,0,t[ii][t[ii][i][1]][2])+abs(i-t[ii][i][1])-1; if(wyn>0) { t[ii][t[ii][i][1]][0] = t[ii][i][1]+1; t[ii][t[ii][i][1]][2] = 0; t[ii][i][1] = i-1; } int pom = t[ii][i][1]; t[ii][pom][0] = t[ii][i][0]; t[ii][i][0] = pom; t[ii][i][1] = 0; t[ii][pom][2] = i; akt(ii,t[ii][pom][0]); } wyn = (wyn+1)%md; return wyn; } long long f(int i) { //printf("##%d\n", i); long long wyn = 0; while(t[0][i][1]!=t[1][i][1]) { if(t[0][i][1]<t[1][i][1]) { wyn = (wyn+rot(1,0,t[0][i][1]))%md; } else { wyn = (wyn+rot(0,0,t[0][i][1]))%md; } } while(t[0][i][2]!=t[1][i][2]) { //printf("%lld %d %d\n", wyn, t[0][i][2], t[1][i][2]); if(t[0][i][2]<t[1][i][2]) { wyn = (wyn+rot(1,0,t[0][i][2]))%md; } else { wyn = (wyn+rot(0,0,t[0][i][2]))%md; } } //printf("%lld\n", wyn); if(t[0][i][1]>0) wyn = (wyn+f(t[0][i][1]))%md; if(t[0][i][2]>0) wyn = (wyn+f(t[0][i][2]))%md; return wyn; } int main() { scanf("%d", &n); for(int ii = 0;ii<2;++ii) { for(int i = 1;i<=n;++i) { int a; scanf("%d", &a); a = max(a,0); t[ii][i][0] = a; if(i<a) { t[ii][a][1] = i; } else { t[ii][a][2] = i; } } } printf("%lld\n", f(0)); } /* 11 -1 9 5 3 6 2 6 7 1 9 10 2 3 6 5 3 -1 8 6 11 9 8 29 //*/
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 | #include<bits/stdc++.h> #define lim 500009 #define mk make_pair #define ff first #define sc second using namespace std; typedef unsigned int uint; const int md = 1000000007; int n; int t[2][lim][3]; void akt(int ii,int i) { if((t[ii][i][1]>0)&&(t[ii][t[ii][i][1]][0]!=i)) { t[ii][i][1] = t[ii][t[ii][i][1]][0]; akt(ii,i); } if((t[ii][i][2]>0)&&(t[ii][t[ii][i][2]][0]!=i)) { t[ii][i][2] = t[ii][t[ii][i][2]][0]; akt(ii,i); } } long long rem(int dir,int ii,int i) { if(i == 0) { return 0; } long long wyn = abs(t[ii][i][0]-i)-1; //printf("%lld %d %d %d\n", wyn, dir, ii, i); if(dir) { wyn = (wyn+rem(dir,ii,t[ii][i][1]))%md; wyn = (wyn+rem(dir,ii,t[ii][i][2]))%md; t[ii][i][0] = (n+i)%(n+1); t[ii][i][1] = 0; t[ii][i][2] = (i+1)%(n+1); } else { wyn = (wyn+rem(dir,ii,t[ii][i][1]))%md; wyn = (wyn+rem(dir,ii,t[ii][i][2]))%md; t[ii][i][0] = (i+1)%(n+1); t[ii][i][1] = (n+i)%(n+1); t[ii][i][2] = 0; } return wyn; } long long rot(int dir,int ii,int i) { long long wyn = 0; //printf("%d %d %d %d\n", dir, ii, i, t[ii][i][dir+1]); if(dir) { if((i==0)||(t[ii][i][2]==0)) { exit(0); } wyn = rem(dir,0,t[ii][t[ii][i][2]][1])+abs(i-t[ii][i][2])-1; if(wyn>0) { t[ii][t[ii][i][2]][0] = t[ii][i][2]-1; t[ii][t[ii][i][2]][1] = 0; t[ii][i][2] = i+1; } int pom = t[ii][i][2]; t[ii][pom][0] = t[ii][i][0]; t[ii][i][0] = pom; t[ii][i][2] = 0; t[ii][pom][1] = i; akt(ii,t[ii][pom][0]); } else { if((i==0)||(t[ii][i][1]==0)) { exit(0); } wyn = rem(dir,0,t[ii][t[ii][i][1]][2])+abs(i-t[ii][i][1])-1; if(wyn>0) { t[ii][t[ii][i][1]][0] = t[ii][i][1]+1; t[ii][t[ii][i][1]][2] = 0; t[ii][i][1] = i-1; } int pom = t[ii][i][1]; t[ii][pom][0] = t[ii][i][0]; t[ii][i][0] = pom; t[ii][i][1] = 0; t[ii][pom][2] = i; akt(ii,t[ii][pom][0]); } wyn = (wyn+1)%md; return wyn; } long long f(int i) { //printf("##%d\n", i); long long wyn = 0; while(t[0][i][1]!=t[1][i][1]) { if(t[0][i][1]<t[1][i][1]) { wyn = (wyn+rot(1,0,t[0][i][1]))%md; } else { wyn = (wyn+rot(0,0,t[0][i][1]))%md; } } while(t[0][i][2]!=t[1][i][2]) { //printf("%lld %d %d\n", wyn, t[0][i][2], t[1][i][2]); if(t[0][i][2]<t[1][i][2]) { wyn = (wyn+rot(1,0,t[0][i][2]))%md; } else { wyn = (wyn+rot(0,0,t[0][i][2]))%md; } } //printf("%lld\n", wyn); if(t[0][i][1]>0) wyn = (wyn+f(t[0][i][1]))%md; if(t[0][i][2]>0) wyn = (wyn+f(t[0][i][2]))%md; return wyn; } int main() { scanf("%d", &n); for(int ii = 0;ii<2;++ii) { for(int i = 1;i<=n;++i) { int a; scanf("%d", &a); a = max(a,0); t[ii][i][0] = a; if(i<a) { t[ii][a][1] = i; } else { t[ii][a][2] = i; } } } printf("%lld\n", f(0)); } /* 11 -1 9 5 3 6 2 6 7 1 9 10 2 3 6 5 3 -1 8 6 11 9 8 29 //*/ |