?
# | Author | Problem | Lang | Verdict | Time | Memory | Sent | Judged | |
---|---|---|---|---|---|---|---|---|---|
180033894 |
Practice: zyx_Mikasa |
1117G - 12 | C++17 (GCC 9-64) | Accepted | 2776 ms | 164396 KB | 2022-11-08 07:03:03 | 2022-11-08 07:03:03 |
// LUOGU_RID: 93238548 #include<cstdio> #include<iostream> #include<algorithm> #define ll long long #define fir first #define sec second using namespace std; const int N=2e6+5; int n,m,q,top,p[N],a[N],b[N]; int st[N],L[N],R[N]; ll cntl[N],cntr[N],fl[N],fr[N],ans[N]; struct node { int x,l,r,id,op; bool operator <(node const &b)const{return x<b.x;} }s[N]; int lowbit(int x){return x&(-x);} void update(ll f[],int x,int val) {for(;x<=n;x+=lowbit(x)) f[x]+=val;} ll query(ll f[],int x) { ll res=0; for(;x;x-=lowbit(x)) res+=f[x]; return res; } ll calc(int l,int r) { ll res=query(fr,r)+r*(query(cntr,n)-query(cntr,r)); res-=l*query(cntl,l)+query(fl,n)-query(fl,l); return res; } void inisert(int x) { L[x]++;R[x]--; update(cntl,L[x],1);update(cntr,R[x],1); update(fl,L[x],L[x]);update(fr,R[x],R[x]); } int main() { scanf("%d%d",&n,&q); for(int i=1;i<=n;i++) { scanf("%d",&p[i]); while(top&&p[i]>p[st[top]]) R[st[top--]]=i; L[i]=st[top];st[++top]=i; } while(top) R[st[top--]]=n+1; for(int i=1;i<=q;i++) scanf("%d",&a[i]); for(int i=1;i<=q;i++) scanf("%d",&b[i]); for(int i=1;i<=q;i++) { if(a[i]>1) s[++m]=(node){a[i]-1,a[i],b[i],i,-1}; s[++m]=(node){b[i],a[i],b[i],i,1}; ans[i]=b[i]-a[i]+1; } sort(s+1,s+1+m); int now=1; for(int i=1;i<=n;i++) { inisert(i); while(s[now].x==i) { ans[s[now].id]+=calc(s[now].l,s[now].r)*s[now].op; now++; } } for(int i=1;i<=q;i++) printf("%lld ",ans[i]); return 0; }
?
?
?
?