#include <bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define ll long long #define int long long ll mod=1000000007; int inf=1000000007; ll infl=1000000000000000007; const int N=500007; int a[N]; int la[N]; int ra[N]; int b[N]; int lb[N]; int rb[N]; ll DP[N]; ll ile[N]; deque <int> q; int kr[N]; ll ans; void licz(int root,int v,int p,int k,bool s) { if(root==0) return ; if(s) { if(p<=root&&k>=root) { ans+=(ll)root-(ll)p; ans%=mod; licz(lb[root],0,root-1,p,0); licz(rb[root],v,root+1,k,1); } else if(root==v&&(p==0||k<p)) { licz(lb[root],la[v],0,0,0); licz(rb[root],ra[v],0,0,1); } else { if(p!=0&&k>=p) ans+=(ll)k-(ll)p+1; ans%=mod; int u=v; while(true) { if(u>=root) { if(u==v&&(p==0||k<p)) { int u1=la[u]; while(true) { ans+=DP[ra[u1]]+2*ile[ra[u1]]+1; ans%=mod; if(u1<=root) { ans-=root-u1; ans=(ans%mod+mod)%mod; if(p!=0&&k>=p) licz(lb[root],la[u1],root-1,p,0); else licz(lb[root],la[u1],root-1,u1,0); licz(rb[root],ra[u],root+1,u,1); break; } u1=la[u1]; } } else { ans+=DP[la[u]]+2*ile[la[u]]-u+root; ans=(ans%mod+mod)%mod; if(p!=0&&k>=p) licz(lb[root],la[v],root-1,p,0); else licz(lb[root],la[v],root-1,v,0); licz(rb[root],ra[u],root+1,u,1); } break; } ans++; if(u!=v||(p!=0&&k>=p)) ans+=DP[la[u]]+2*ile[la[u]]; ans%=mod; u=ra[u]; } } } else { if(p>=root&&k<=root) { ans+=(ll)p-(ll)root; ans%=mod; licz(lb[root],v,root-1,k,0); licz(rb[root],0,root+1,p,1); } else if(root==v&&(p==0||k>p)) { licz(lb[root],la[v],0,0,0); licz(rb[root],ra[v],0,0,1); } else { if(p!=0&&p>=k) ans+=(ll)p-(ll)k+1; ans%=mod; int u=v; while(true) { if(u<=root) { if(u==v&&(p==0||k>p)) { int u1=ra[u]; while(true) { ans+=DP[la[u1]]+2*ile[la[u1]]+1; ans%=mod; if(u1>=root) { ans-=u1-root; ans=(ans%mod+mod)%mod; licz(lb[root],la[u],root-1,u,0); if(p!=0&&k<=p) licz(rb[root],ra[u1],root+1,p,1); else licz(rb[root],ra[u1],root+1,u1,1); break; } u1=ra[u1]; } } else { ans+=DP[ra[u]]+2*ile[ra[u]]-root+u; ans=(ans%mod+mod)%mod; licz(lb[root],la[u],root-1,u,0); if(p!=0&&k<=p) licz(rb[root],ra[v],root+1,p,1); else licz(rb[root],ra[v],root+1,v,1); } break; } ans++; ans%=mod; if(u!=v||(p!=0&&k<=p)) ans+=DP[ra[u]]+2*ile[ra[u]]; ans%=mod; u=la[u]; } } } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; int r1,r2; for(int i=1;i<=n;i++) { cin>>a[i]; if(a[i]==-1) r1=i; if(a[i]!=-1) { kr[a[i]]++; if(a[i]>i) la[a[i]]=i; else ra[a[i]]=i; } } for(int i=1;i<=n;i++) if(kr[i]==0) q.pb(i); for(int i=1;i<=n;i++) { cin>>b[i]; if(b[i]==-1) r2=i; if(b[i]>i) lb[b[i]]=i; else rb[b[i]]=i; } while(!q.empty()) { int v=q[0]; q.pop_front(); ile[v]++; if(a[v]==-1) break; ile[a[v]]+=ile[v]; DP[a[v]]+=DP[v]; if(a[a[v]]!=-1&&((a[a[v]]>a[v])!=(a[v]>v))) DP[a[v]]+=ile[v]; kr[a[v]]--; if(kr[a[v]]==0) q.pb(a[v]); } licz(r2,r1,0,0,r1<r2); cout<<ans<<endl; return 0; }
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define ll long long #define int long long ll mod=1000000007; int inf=1000000007; ll infl=1000000000000000007; const int N=500007; int a[N]; int la[N]; int ra[N]; int b[N]; int lb[N]; int rb[N]; ll DP[N]; ll ile[N]; deque <int> q; int kr[N]; ll ans; void licz(int root,int v,int p,int k,bool s) { if(root==0) return ; if(s) { if(p<=root&&k>=root) { ans+=(ll)root-(ll)p; ans%=mod; licz(lb[root],0,root-1,p,0); licz(rb[root],v,root+1,k,1); } else if(root==v&&(p==0||k<p)) { licz(lb[root],la[v],0,0,0); licz(rb[root],ra[v],0,0,1); } else { if(p!=0&&k>=p) ans+=(ll)k-(ll)p+1; ans%=mod; int u=v; while(true) { if(u>=root) { if(u==v&&(p==0||k<p)) { int u1=la[u]; while(true) { ans+=DP[ra[u1]]+2*ile[ra[u1]]+1; ans%=mod; if(u1<=root) { ans-=root-u1; ans=(ans%mod+mod)%mod; if(p!=0&&k>=p) licz(lb[root],la[u1],root-1,p,0); else licz(lb[root],la[u1],root-1,u1,0); licz(rb[root],ra[u],root+1,u,1); break; } u1=la[u1]; } } else { ans+=DP[la[u]]+2*ile[la[u]]-u+root; ans=(ans%mod+mod)%mod; if(p!=0&&k>=p) licz(lb[root],la[v],root-1,p,0); else licz(lb[root],la[v],root-1,v,0); licz(rb[root],ra[u],root+1,u,1); } break; } ans++; if(u!=v||(p!=0&&k>=p)) ans+=DP[la[u]]+2*ile[la[u]]; ans%=mod; u=ra[u]; } } } else { if(p>=root&&k<=root) { ans+=(ll)p-(ll)root; ans%=mod; licz(lb[root],v,root-1,k,0); licz(rb[root],0,root+1,p,1); } else if(root==v&&(p==0||k>p)) { licz(lb[root],la[v],0,0,0); licz(rb[root],ra[v],0,0,1); } else { if(p!=0&&p>=k) ans+=(ll)p-(ll)k+1; ans%=mod; int u=v; while(true) { if(u<=root) { if(u==v&&(p==0||k>p)) { int u1=ra[u]; while(true) { ans+=DP[la[u1]]+2*ile[la[u1]]+1; ans%=mod; if(u1>=root) { ans-=u1-root; ans=(ans%mod+mod)%mod; licz(lb[root],la[u],root-1,u,0); if(p!=0&&k<=p) licz(rb[root],ra[u1],root+1,p,1); else licz(rb[root],ra[u1],root+1,u1,1); break; } u1=ra[u1]; } } else { ans+=DP[ra[u]]+2*ile[ra[u]]-root+u; ans=(ans%mod+mod)%mod; licz(lb[root],la[u],root-1,u,0); if(p!=0&&k<=p) licz(rb[root],ra[v],root+1,p,1); else licz(rb[root],ra[v],root+1,v,1); } break; } ans++; ans%=mod; if(u!=v||(p!=0&&k<=p)) ans+=DP[ra[u]]+2*ile[ra[u]]; ans%=mod; u=la[u]; } } } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; int r1,r2; for(int i=1;i<=n;i++) { cin>>a[i]; if(a[i]==-1) r1=i; if(a[i]!=-1) { kr[a[i]]++; if(a[i]>i) la[a[i]]=i; else ra[a[i]]=i; } } for(int i=1;i<=n;i++) if(kr[i]==0) q.pb(i); for(int i=1;i<=n;i++) { cin>>b[i]; if(b[i]==-1) r2=i; if(b[i]>i) lb[b[i]]=i; else rb[b[i]]=i; } while(!q.empty()) { int v=q[0]; q.pop_front(); ile[v]++; if(a[v]==-1) break; ile[a[v]]+=ile[v]; DP[a[v]]+=DP[v]; if(a[a[v]]!=-1&&((a[a[v]]>a[v])!=(a[v]>v))) DP[a[v]]+=ile[v]; kr[a[v]]--; if(kr[a[v]]==0) q.pb(a[v]); } licz(r2,r1,0,0,r1<r2); cout<<ans<<endl; return 0; } |