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
| #include<bits/stdc++.h> using namespace std;
typedef long long ll; typedef unsigned long long ull; typedef double db; typedef pair <int,int> Pii; typedef vector <int> V; #define reg register #define mp make_pair #define pb push_back #define Mod1(x) ((x>=P)&&(x-=P)) #define Mod2(x) ((x<0)&&(x+=P)) #define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i) #define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i) template <class T> inline void cmin(T &a,T b){ ((a>b)&&(a=b)); } template <class T> inline void cmax(T &a,T b){ ((a<b)&&(a=b)); }
char IO; template <class T=int> T rd(){ T s=0; int f=0; while(!isdigit(IO=getchar())) f|=IO=='-'; do s=(s<<1)+(s<<3)+(IO^'0'); while(isdigit(IO=getchar())); return f?-s:s; }
const int N=1e5+10,M=2e5+10,S=1000;
int n,m,A[N],len,P[N]; struct Blocker{ int s[M],t[N]; void clear(){ memset(s,0,(n+1)<<2),memset(t,0,(n+1)<<2); } void Add(int x){ int p=x/len; rep(i,x,(p+1)*len-1) s[i]++; rep(i,p+1,n/len) t[i]++; } int operator [](const int &x) const{ return s[x]+t[x/len]; } } B; int L[M],R[M],U[M],D[M],p1[M],p2[M],I[M],T[M]; ll Ans[M]; struct Que{ int l,r,k,id; }; vector <Que> Q[N];
void SolvePoints(){ rep(i,1,n) { B.Add(A[i]); for(Que x:Q[i]) { rep(j,x.l,x.r) { int u=U[x.id],d=D[x.id]; if(A[j]<u || A[j]>d) continue; if(j>i) cmin(d,A[j]-1); else cmax(u,A[j]+1); Ans[x.id]+=x.k*(B[d]-B[u-1]); } } } }
vector <Pii> E[N];
void SolveB1(){ static ll s[S][S],c[S]; rep(k,1,n) { int i=P[k],t=0,p=i/len; c[p]++; drep(j,p-1,0) t+=c[j],s[j][p]+=t; for(Pii x:E[k]) { int u=x.first,l=p1[u]+1,r=p2[u]-1; rep(j,l+1,r) Ans[u]+=s[l][j]*x.second; } } }
void SolveB2(){ static int s[S][S],C[N]; rep(i,0,n/len) { int l=max(1,i*len),r=min(n,(i+1)*len-1); rep(j,1,n) C[j]=C[j-1]+(l<=P[j] && P[j]<=r); int L=C[n]; rep(a,1,L+1) rep(b,a-1,L+1) s[a][b]=0; rep(a,l,r) rep(b,a+1,r) if(A[a]<=A[b]) s[C[A[a]]][C[A[b]]]++; drep(a,L,1) rep(b,a,L) s[a][b]+=s[a+1][b]+s[a][b-1]-s[a+1][b-1]; rep(j,1,m) if(p1[j]<i && i<p2[j]) { Ans[j]+=s[C[U[j]-1]+1][C[D[j]]]; Ans[j]-=1ll*T[j]*(C[D[j]]-C[U[j]-1]); T[j]+=C[U[j]-1]; } } }
int Que(int i,int l,int r,int u,int d){ if(r-l>45) { int mid=(l+r*3)/4; Q[l-1].pb({mid+1,r,-1,i}); Q[mid].pb({mid+1,r,1,i}); return Que(i,l,mid,u,d)+Que(i,mid+1,r,u,d); } int ans=0; rep(i,l,r) if(u<=A[i] && A[i]<=d) rep(j,i+1,r) ans+=A[i]<=A[j] && A[j]<=d; return ans; }
int main(){ freopen("tears.in","r",stdin),freopen("tears.out","w",stdout); n=rd(),m=rd(),len=ceil(sqrt(n/4.0)); fprintf(stderr,"Block len=%d ,Block Count=%d\n",len,n/len); rep(i,1,n) P[A[i]=rd()]=i; clock_t ti=clock(); rep(i,1,m) { I[i]=i,L[i]=rd(),R[i]=rd(),U[i]=rd(),D[i]=rd(); p1[i]=L[i]/len,p2[i]=R[i]/len; if(p1[i]==p2[i]){ Ans[i]=Que(i,L[i],R[i],U[i],D[i]); continue; } Ans[i]=Que(i,L[i],(p1[i]+1)*len-1,U[i],D[i])+Que(i,p2[i]*len,R[i],U[i],D[i]); Q[L[i]-1].pb({p2[i]*len,R[i],-1,i}); Q[p2[i]*len-1].pb({p2[i]*len,R[i],1,i}); if(p1[i]<p2[i]-1) { Q[(p1[i]+1)*len-1].pb({L[i],(p1[i]+1)*len-1,-1,i}); Q[p2[i]*len-1].pb({L[i],(p1[i]+1)*len-1,1,i}); E[D[i]].pb(mp(i,1)); E[U[i]-1].pb(mp(i,-1)); } } fprintf(stderr,"Part0 %d\n",int(clock()-ti)),ti=clock(); SolvePoints(); fprintf(stderr,"Part1 %d\n",int(clock()-ti)),ti=clock(); sort(I+1,I+m+1,[&](int x,int y){ return L[x]<L[y]; }); SolveB1(); fprintf(stderr,"Part2 %d\n",int(clock()-ti)),ti=clock(); SolveB2(); fprintf(stderr,"Part3 %d\n",int(clock()-ti)),ti=clock(); rep(i,1,m) printf("%lld\n",Ans[i]); }
|