2738: Matrix multiplication
Time limit:20 Sec Memory limit:256 MB
submit:841 solved:351
[Submit] [Status] [Discuss]
Description
Give you a n*n matrix that does not count the matrix multiplication, but every time you ask for a sub-rectangle, the K decimal.
Input
The first line is two number n,q, which indicates the matrix size and the number of inquiry groups;
Next n row n column altogether n*n number, represents this matrix;
Then the Q line of 5 numbers per line describes a query: X1,y1,x2,y2,k indicates that the x1,y1 is found in the upper-left corner, with (X2,y2) as the lower-right corner of the sub-rectangle in the K decimal.
Output
For each set of queries, the output is a small number of K.
Sample Input
2 2
2 1
3 4
1 2 1) 2 1
1 1 2) 2 3
Sample Output
1
3
HINT
The number in the matrix is a non-negative integer within 109;
20% of Data:n<=100,q<=1000;
40% of Data:n<=300,q<=10000;
60% of Data:n<=400,q<=30000;
100% data: n<=500,q<=60000.
Thinking problem + whole two points.
First, the elements in the matrix are sorted in ascending order, and then the whole two points can be divided.
That is, the two points each query matrix is in the number of elements added when the time reached Span style= "Display:inline-block; width:0px; Height:2.503em; " > (optimized with a two-dimensional tree array).
Same as "Bzoj 2527".
#include <iostream>#include <cmath>#include <algorithm>#include <cstdio>#include <cstring>#include <cstdlib>#define M 500005Using namespace Std;struct jz{int x,y, V;} A[m];struct query{intX1,y1,x2,y2,k,id,cur;}Q[m], B[m];intt[505][505],ans[m],n,q;bool CMP (JZ a,jz b) {returnA.V<B.V;}intLowbit (int x){return x& (-x);} void Update (int x,int y,intK) {if(!x)x++;if(!y)y++; for(intI=x; I<=n;i+=lowbit (i)) for(intj=y; J<=n;j+=lowbit (j)) T[i][j]+=k;}intSumint x,int y){intcn1=0; for(intI=x;i>0; I-=lowbit (i)) for(intj=y;j>0; J-=lowbit (j)) Ans+=t[i][j];returnAns;}intGetsum (int x){returnSUM (b[x].x2,b[x].y2)-sum (b[x].x2,b[x].y1-1)-sum (b[x].x1-1, b[x].y2) +sum (b[x].x1-1, b[x].y1-1); }voidRead(int&TMP) {tmp=0; Char Ch=getchar ();intfu=1; for(;ch<' 0 '|| Ch>' 9 '; Ch=getchar ())if(ch=='-') fu=-1; for(; ch>=' 0 '&&ch<=' 9 '; Ch=getchar ()) tmp=tmp*10+ch-' 0 '; Tmp*=Fu;} void Solve (intLintRintHintT) {if(h>t)return;if(L==R) { for(inti=h;i<=t;i++) ans[b[i].id]=a[l].v;return; }intMid= (l+r) >>1; for(inti=l;i<=mid;i++) Update (A[i].x, A[i].y,1);intt1=h-1, t2=t+1; for(inti=h;i<=t;i++) {intTmp=getsum (i);if(TMP+B[I].CUR>=B[I].K)Q[++t1]=b[i];ElseB[i].cur+=tmp,Q[--t2]=b[i]; } for(inti=h;i<=t;i++) b[i]=Q[i]; for(inti=l;i<=mid;i++) Update (A[i].x, A[i].y,-1); Solve (L,MID,H,T1); Solve (mid+1, r,t2,t);}intMain () {scanf ("%d%d", &n,&q);intnow=0; for(intI=1; i<=n;i++) for(intj=1; j<=n;j++)Read(A[++NOW].V), A[now].x=i,a[now].y=j;Sort(A +1, A +1+NOW,CMP); for(intI=1; i<=q;i++)Read(B[I].X1),Read(B[i].y1),Read(B[I].X2),Read(B[i].y2),Read(B[I].K), b[i].id=i,b[i].cur=0; Solve (1, now,1, Q); for(intI=1; i<=q;i++)printf("%d\ n", Ans[i]);return 0;}
"Bzoj 2738" matrix multiplication