有个细节,就是query的时候的顺序,不注意到直接T飞,分析知道如果它只在一边的话你直接一边
可以保证复杂度
#include<iostream>
#include<cstring>
#include<algorithm>
const int N = 2e5+10;
using namespace std;
using ll = long long;
int n;
int a[N],b[N];
int tr[2][N<<2];
inline void pushup(int op,int u){
if(!op)tr[0][u] = max(tr[0][u<<1],tr[0][u<<1|1]);
if(op)tr[1][u] = min(tr[1][u<<1],tr[1][u<<1|1]);
}
inline void build(int op,int u,int l,int r){
if(l==r){
if(!op)tr[op][u] = a[l];
else tr[op][u] = b[l];
return;
}
int mid = (l+r)>>1;
build(op,u<<1,l,mid),build(op,u<<1|1,mid+1,r);
pushup(op,u);
}
int hackt;
inline int query(int op,int u,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr)return tr[op][u];
int mid = (l+r)>>1;
if(qr<=mid)return query(op,u<<1,l,mid,ql,qr);
else if(ql>mid)return query(op,u<<1|1,mid+1,r,ql,qr);
else{
if(!op)return max(query(op,u<<1,l,mid,ql,qr),query(op,u<<1|1,mid+1,r,ql,qr));
else return min(query(op,u<<1,l,mid,ql,qr),query(op,u<<1|1,mid+1,r,ql,qr));
}
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)cin>>b[i];
build(0,1,1,n);
build(1,1,1,n);
ll res = 0;
for(int i=1;i<=n;i++){
if(a[i]>b[i])continue;
int l=i-1,r=n+1;
while(l+1!=r){
int mid = (l+r)>>1;
int tem1 = query(0,1,1,n,i,mid);
int tem2 = query(1,1,1,n,i,mid);
if(tem1<tem2)l = mid;
else r = mid;
}
int ans_l = l+1;
l = i-1,r = n+1;
while(l+1!=r){
int mid = (l+r)>>1;
int tem1 = query(0,1,1,n,i,mid);
int tem2 = query(1,1,1,n,i,mid);
if(tem1<=tem2)l = mid;
else r = mid;
}
int ans_r = l;
if(ans_l>ans_r)continue;
if(query(0,1,1,n,i,ans_l)!=query(1,1,1,n,i,ans_l))continue;
res = res + ans_r-ans_l+1;
}
cout<<res<<"\n";
}