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
| #include <bits/stdc++.h> #define int long long #define rep(i,a,b) for(int i=(a);i<=(b);++i) #define per(i,a,b) for(int i=(a);i>=(b);--i) using namespace std; const int N=2e5+5; int n,a[N],b[N]; int mn[N][22],mx[N][22]; signed main() { scanf("%lld",&n); rep(i,1,n) scanf("%lld",&a[i]),mx[i][0]=a[i]; rep(i,1,n) scanf("%lld",&b[i]),mn[i][0]=b[i]; rep(j,1,21) { rep(i,1,n) { if(i+(1<<(j-1))-1>n) mx[i][j]=mx[i][j-1]; else mx[i][j]=max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]); } } rep(j,1,21) { rep(i,1,n) { if(i+(1<<(j-1))-1>n) mn[i][j]=mn[i][j-1]; else mn[i][j]=min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]); } } int ans=0; rep(i,1,n) { int l=i,r=n; int rr=0,ll=0; while(r-l>5) { int mid=(l+r)>>1; int lg=log2(mid-i+1); int mxx=max(mx[i][lg],mx[mid-(1<<lg)+1][lg]); int mnn=min(mn[i][lg],mn[mid-(1<<lg)+1][lg]); if(mxx>mnn) r=mid; else l=mid; } per(j,r,l) { int lg=log2(j-i+1); int mxx=max(mx[i][lg],mx[j-(1<<lg)+1][lg]); int mnn=min(mn[i][lg],mn[j-(1<<lg)+1][lg]); if(mxx==mnn) {rr=j;break;} } l=i,r=n; while(r-l>5) { int mid=(l+r)>>1; int lg=log2(mid-i+1); int mxx=max(mx[i][lg],mx[mid-(1<<lg)+1][lg]); int mnn=min(mn[i][lg],mn[mid-(1<<lg)+1][lg]); if(mxx>=mnn) r=mid; else l=mid; } rep(j,l,r) { int lg=log2(j-i+1); int mxx=max(mx[i][lg],mx[j-(1<<lg)+1][lg]); int mnn=min(mn[i][lg],mn[j-(1<<lg)+1][lg]); if(mxx==mnn) {ll=j;break;} } if(ll&&rr) ans+=(rr-ll+1); } printf("%lld",ans); return 0; }
|