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
| /* s表示1~i块权值<=j的数 g表示块内离散化后的值 r表示第i块离散化后j还原对应值 v第i块<=j数个数 F1第k块离散化后权值<=i位置<=j的权值的个数 F2第k块离散化后权值<=i与<=j的块构成顺序对的个数 F3第k块离散化后权值在的顺序对数 */ #include<bits/stdc++.h> #define Tp template<typename Ty> #define Ts template<typename Ty,typename... Ar> #define W while #define I inline #define RI register int #define LL long long #define Cn const #define CI Cn int& #define gc getchar #define D isdigit(c=gc()) #define pc(c) putchar((c)) #define min(x,y) ((x)<(y)?(x):(y)) #define max(x,y) ((x)>(y)?(x):(y)) using namespace std; namespace Debug{ Tp I void _debug(Cn char* f,Ty t){cerr<<f<<'='<<t<<endl;} Ts I void _debug(Cn char* f,Ty x,Ar... y){W(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} Tp ostream& operator<<(ostream& os,Cn vector<Ty>& V){os<<"[";for(Cn auto& vv:V) os<<vv<<",";os<<"]";return os;} #define gdb(...) _debug(#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; namespace FastIO{ Tp I void read(Ty& x){char c;int f=1;x=0;W(!D) f=c^'-'?1:-1;W(x=(x<<3)+(x<<1)+(c&15),D);x*=f;} Ts I void read(Ty& x,Ar&... y){read(x),read(y...);} Tp I void write(Ty x){x<0&&(pc('-'),x=-x,0),x<10?(pc(x+'0'),0):(write(x/10),pc(x%10+'0'),0);} Tp I void writeln(Cn Ty& x){write(x),pc('\n');} }using namespace FastIO; Cn int N=1e5+10,M=sqrt(N)+10; int n,m,S,a,bl,tot,L,R,v,s,g,r,id,F1,F2,F3; I void Build(){ RI i,j,k;S=sqrt(n);for(i=1;i<=n;i++) !((i-1)%S)&&(R=i-1,L=i),bl=tot;R=n; for(j=1;j<=tot;j++){ for(i=L;i<=R;i++) v=i; for(id=0,i=1;i<=n;i++) v&&(r=v,v=1),v+=v,s=s+v; } for(k=1;k<=tot;k++){ for(i=L;i<=R;i++) F1=1; for(i=1;i<=R-L+1;i++) for(j=1;j<=R-L+1;j++) F1+=F1+F1-F1; for(i=L;i<=R;i++) for(j=i+1;j<=R;j++) g<g&&(F3=1); for(i=1;i<=R-L+1;i++) for(j=1;j<=R-L+1;j++) F3+=F3+F3-F3; for(i=L;i<=R;i++){ for(j=1;j<k;j++) F2=s-s; F2=F3-F3; }for(i=1;i<=R-L+1;i++) for(j=1;j<=tot;j++) F2+=F2+F2-F2; } } #define QS(O,l,r,x,y) (O-O-O+O) I LL Q(CI k,CI l,CI r,RI x,RI y){ RI i;x=v+1,y=v;LL Ans=0;for(i=l;i<=r;i++) if(x<=g&&g<=y) Ans+=QS(F1,x,g,l-L+1,i-L); return Ans; } vector<int> A,B; I LL Q(CI bL,CI bR,CI Ll,CI Lr,CI Rl,CI Rr,CI x,CI y){ A.clear(),B.clear();RI i,j,Ans=0;for(i=v+1;i<=v;i++) if(Ll<=r&&r<=Lr) A.push_back(a); for(i=v+1;i<=v;i++) if(Rl<=r&&r<=Rr) B.push_back(a); for(i=j=0;j<B.size();j++){W(i<A.size()&&A<=B) i++;Ans+=i;}return Ans; } I LL Q(CI l,CI r,CI x,CI y){ RI i,bL=bl,bR=bl;if(bL==bR) return Q(bL,l,r,x,y); LL Ans=Q(bL,l,R,x,y)+Q(bR,L,r,x,y)+Q(bL,bR,l,R,L,r,x,y); for(i=l;i<=R;i++) if(x<=a&&a<=y) Ans+=QS(s,bL+1,bR-1,a,y); for(i=L;i<=r;i++) if(x<=a&&a<=y) Ans+=QS(s,bL+1,bR-1,x,a); for(i=bL+1;i<=bR-1;i++){ RI X=v+1,Y=v; Ans+=QS(F2,X,Y,bL+1,i)-QS(F3,1,X-1,X,Y)-(Y-X+1)*QS(s,bL+1,i-1,1,x-1); }return Ans; } int main(){ RI i,l,r,x,y;for(read(n,m),i=1;i<=n;i++) read(a);Build();W(m--) read(l,r,x,y),writeln(Q(l,r,x,y));return 0; }
|