首页 > 代码库 > tsinsen A1333. 矩阵乘法

tsinsen A1333. 矩阵乘法

题目链接:传送门

技术分享

 

题目思路:整体二分(二分的是答案,附带的是操作)

     把矩阵中的元素对应成插入操作,然后就有插入和询问操作。

     然后根据插入操作对于答案的影响,询问操作所匹配的符合答案个数,将操作分为两段,继续递归。。。

#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <cstring>#include <stack>#include <cctype>#include <queue>#include <string>#include <vector>#include <set>#include <map>#include <climits>#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define fi first#define se second#define ping(x,y) ((x-y)*(x-y))#define mst(x,y) memset(x,y,sizeof(x))#define mcp(x,y) memcpy(x,y,sizeof(y))using namespace std;#define gamma 0.5772156649015328606065120#define MOD 1000000007#define inf 0x3f3f3f3f#define N 60005#define maxn 1000005typedef pair<int,int> PII;typedef long long LL;int n,m,k,len,S,T;int ans[N];struct Node{    int id,x,y,a,b,v,f;    Node(){}    Node(int _id,int _x,int _y,int _a,int _b,int _v,int _f):        id(_id),x(_x),y(_y),a(_a),b(_b),v(_v),f(_f){}}node[maxn],t1[maxn],t2[maxn];int tree[505][505];void add(int x,int y,int v){for(int i=x;i<=n;i+=(i&-i))for(int j=y;j<=n;j+=(j&-j))tree[i][j]+=v;}int get(int x,int y){    int res=0;    for(int i=x;i;i-=(i&-i))for(int j=y;j;j-=(j&-j))res+=tree[i][j];    return res;}int query(int x,int y,int a,int b){return get(a,b)+get(x,y)-get(x,b)-get(a,y);}void solve(int ql,int qr,int l,int r){    if(ql>qr)return;    if(l==r){        for(int i=ql;i<=qr;++i)if(node[i].f==2)            ans[node[i].id]=l;        return;    }    int len1=0,len2=0,mid=l+r>>1;    for(int i=ql;i<=qr;++i){        if(node[i].f==1){            if(node[i].v<=mid){                add(node[i].x,node[i].y,1);                t1[len1++]=node[i];            }            else t2[len2++]=node[i];        }        else{            int pos=query(node[i].x-1,node[i].y-1,node[i].a,node[i].b);            if(pos<node[i].v){node[i].v-=pos;t2[len2++]=node[i];}            else{                t1[len1++]=node[i];            }        }    }    for(int i=ql;i<=qr;++i)if(node[i].f==1&&node[i].v<=mid)add(node[i].x,node[i].y,-1);    for(int i=0;i<len1;++i)node[i+ql]=t1[i];    for(int i=0;i<len2;++i)node[i+ql+len1]=t2[i];    solve(ql,ql+len1-1,l,mid);solve(ql+len1,qr,mid+1,r);}int main(){    int i,j,group,x,y,x1,y1,Case=0;    scanf("%d%d",&n,&m);    int _min=INT_MAX,_max=INT_MIN;    for(i=1;i<=n;++i)for(j=1;j<=n;++j){        scanf("%d",&x);        _min=min(_min,x);        _max=max(_max,x);        node[++len]=Node(len,i,j,0,0,x,1);    }    for(i=1;i<=m;++i){        scanf("%d%d%d%d%d",&x,&y,&x1,&y1,&k);        node[++len]=Node(i,x,y,x1,y1,k,2);    }    solve(1,len,_min,_max);    for(i=1;i<=m;++i)printf("%d\n",ans[i]);    return 0;}

 

 

tsinsen A1333. 矩阵乘法