bzoj2738 행렬 곱셈(전체 2분)

제목 링크: 여기를 클릭!!!
제목: 하위 행렬의 K를 구하는 것이다.
문제풀이: 전체 2분의 답안을 2차원 트리 수조로 유지하면 됩니다.hdu5412랑 비슷해요.
hdu5412
1. 수정이 없으면 값을 삽입할 수 있습니다.
코드:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define PI 2*asin(1.0)
#define LL long long
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d
",x,x) #define key_value ch[ch[root][1]][0]C:\Program Files\Git\bin const int MOD = 1000000007; const int N = 500+15; const int maxn = 330000+1000; const int letter = 130; const int INF = 1e17; const double pi=acos(-1.0); const double eps=1e-8; using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,q; int p[N][N],tot,max1; int ans[maxn],p1[maxn],p2[maxn]; struct node{ int id,a,b,c,d; int val; }qu[maxn]; int lowbit(int x){return x&(-x);} void update(int i,int j,int val){ for(int x=i;x<=n;x+=lowbit(x)) for(int y=j;y<=n;y+=lowbit(y)) p[x][y]+=val; } int query(int i,int j){ int ans=0; for(int x=i;x>0;x-=lowbit(x)) for(int y=j;y>0;y-=lowbit(y)) ans+=p[x][y]; return ans; } void solve(int L,int R,int low,int high){ if(L>R) return; int i,j,sum; if(low==high){ while(L<=R){ int j=p1[L]; if(qu[j].id==2) ans[j]=low; L++; } return; } int mid=(low+high)/2; int l=L,r=R; for(i=L;i<=R;i++){ j=p1[i]; if(qu[j].id==2){ sum=query(qu[j].c,qu[j].d)+query(qu[j].a-1,qu[j].b-1)-query(qu[j].c,qu[j].b-1)-query(qu[j].a-1,qu[j].d); if(sum<qu[j].val) qu[j].val-=sum,p2[r--]=j; else p2[l++]=j; } else { if(qu[j].val<=mid) update(qu[j].a,qu[j].b,1),p2[l++]=j; else p2[r--]=j; } } for(i=L;i<=R;i++){ j=p1[i]; if(qu[j].id==1&&qu[j].val<=mid) update(qu[j].a,qu[j].b,-1); } for(i=L;i<l;i++)p1[i]=p2[i]; for(r=R;i<=R;r--,i++)p1[i]=p2[r]; solve(L,l-1,low,mid); solve(l,R,mid+1,high); } int main(){ n=read(),q=read(); max1=0; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){ qu[++tot].id=1; qu[tot].a=i,qu[tot].b=j,qu[tot].val=read(); max1=max(max1,qu[tot].val); } while(q--){ qu[++tot].id=2; qu[tot].a=read(),qu[tot].b=read(); qu[tot].c=read(),qu[tot].d=read(); qu[tot].val=read(); } for(int i=1;i<=tot;i++) p1[i]=i; solve(1,tot,0,max1); for(int i=1;i<=tot;i++){ if(qu[i].id==2) printf("%d
",ans[i]); } return 0; }

2. 서열을 배열하여 삽입하면 트리 모양의 그룹을 비울 필요가 없고 시간도 더 빠르다.
#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define PI 2*asin(1.0)
#define LL long long
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d
",x,x) #define key_value ch[ch[root][1]][0]C:\Program Files\Git\bin const int MOD = 1000000007; const int N = 500+15; const int maxn = 60000+1000; const int letter = 130; const int INF = 1e17; const double pi=acos(-1.0); const double eps=1e-8; using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,q,cnt=0,max1,vs; int p[N][N]; int p1[maxn],p2[maxn],ans[maxn]; struct node{ int a,b,c,d,val; }qu[maxn],ps[N*N]; int lowbit(int x){return x&(-x);} bool cmp(node a,node b){return a.val<b.val;} void update(int i,int j,int val){ for(int x=i;x<=n;x+=lowbit(x)) for(int y=j;y<=n;y+=lowbit(y)) p[x][y]+=val; } int query(int i,int j){ int ans=0; for(int x=i;x>0;x-=lowbit(x)) for(int y=j;y>0;y-=lowbit(y)) ans+=p[x][y]; return ans; } void solve(int L,int R,int low,int high){ if(L>R) return; int i,j,mid=(low+high)>>1,sum; if(low==high){ while(L<=R){ans[p1[L]]=low,L++;} return; } while(ps[vs+1].val<=mid) update(ps[vs+1].a,ps[vs+1].b,1),vs++; while(ps[vs].val>mid) update(ps[vs].a,ps[vs].b,-1),vs--; int l=L,r=R; for(i=L;i<=R;i++){ j=p1[i]; sum=query(qu[j].c,qu[j].d)+query(qu[j].a-1,qu[j].b-1)-query(qu[j].c,qu[j].b-1)-query(qu[j].a-1,qu[j].d); if(sum<qu[j].val) p2[r--]=j; else p2[l++]=j; } for(i=L;i<l;i++) p1[i]=p2[i]; for(r=R;i<=R;r--,i++) p1[i]=p2[r]; solve(L,l-1,low,mid); solve(l,R,mid+1,high); } int main(){ max1=0; n=read(),q=read(); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){ ps[++cnt].val=read(); ps[cnt].a=i,ps[cnt].b=j; max1=max(max1,ps[cnt].val); } vs=0; sort(ps+1,ps+cnt+1,cmp); for(int i=1;i<=q;i++){ qu[i].a=read(),qu[i].b=read(); qu[i].c=read(),qu[i].d=read(); qu[i].val=read(); } for(int i=1;i<=q;i++) p1[i]=i; solve(1,q,0,max1); for(int i=1;i<=q;i++) printf("%d
",ans[i]); return 0; }

좋은 웹페이지 즐겨찾기