[BZOJ 2738] matrix multiplication and bzoj2738 Matrix Multiplication

Source: Internet
Author: User

[BZOJ 2738] matrix multiplication and bzoj2738 Matrix Multiplication
2738: Matrix Multiplication

Time Limit: 20 Sec Memory Limit: 256 MB
Submit: 841 Solved: 351
[Submit] [Status] [Discuss]
Description

This gives you a N * N matrix, which does not involve matrix multiplication, but each time you ask for the K decimal number of a subrectangle.
Input

Number N and Q In the first line, indicating the matrix size and number of inquiry groups;
N rows and N columns in the next N rows have N * N numbers, indicating the matrix;
Next, the number of five rows in the Q row describes a query: x1, y1, x2, y2, k, which indicates that the query is found in the upper left corner of (x1, y1), with (x2, y2) it is the K decimal point in the Child rectangle in the lower right corner.
Output

For each group of queries, the K-th small number is output.
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 less than 109;

20% of data: N <= 100, Q <= 1000;

40% of data: N <= 300, Q <= 10000;

60% of data: N <= 400, Q <= 30000;

100% of data: N <= 500, Q <= 60000.


Thinking questions + two points for the whole.

First, we sort the elements in the matrix in ascending order, and then we can divide them into two parts.

That is, when the number of elements in each query matrix is added K (Optimized using a two-dimensional tree array ).

It is the 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{    int x1,y1,x2,y2,k,id,cur;}q[M],b[M];int t[505][505],ans[M],n,Q;bool cmp(jz a,jz b){    return a.v<b.v;}int lowbit(int x){    return x&(-x);}void Update(int x,int y,int k){    if (!x) x++;    if (!y) y++;    for (int i=x;i<=n;i+=lowbit(i))        for (int j=y;j<=n;j+=lowbit(j))            t[i][j]+=k;}int sum(int x,int y){    int ans=0;    for (int i=x;i>0;i-=lowbit(i))        for (int j=y;j>0;j-=lowbit(j))            ans+=t[i][j];    return ans;}int Getsum(int x){    return sum(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); }void read(int &tmp){    tmp=0;    char ch=getchar();    int fu=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(int l,int r,int h,int t){    if (h>t) return;    if (l==r)    {        for (int i=h;i<=t;i++)            ans[b[i].id]=a[l].v;        return;    }    int mid=(l+r)>>1;    for (int i=l;i<=mid;i++)        Update(a[i].x,a[i].y,1);    int t1=h-1,t2=t+1;    for (int i=h;i<=t;i++)    {        int tmp=Getsum(i);        if (tmp+b[i].cur>=b[i].k) q[++t1]=b[i];        else b[i].cur+=tmp,q[--t2]=b[i];    }    for (int i=h;i<=t;i++)        b[i]=q[i];    for (int i=l;i<=mid;i++)        Update(a[i].x,a[i].y,-1);    Solve(l,mid,h,t1);    Solve(mid+1,r,t2,t);}int main(){    scanf("%d%d",&n,&Q);    int now=0;    for (int i=1;i<=n;i++)        for (int j=1;j<=n;j++)            read(a[++now].v),a[now].x=i,a[now].y=j;    sort(a+1,a+1+now,cmp);    for (int i=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 (int i=1;i<=Q;i++)        printf("%d\n",ans[i]);    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.