hdu2687

来源:互联网 发布:未来教育计算机二级vb 编辑:程序博客网 时间:2024/06/03 18:08

Matrix Rotation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 512    Accepted Submission(s): 247


Problem Description
Give you a n*n matrix, if you rotate it 90 degrees, we call it 1-Matrix, if you rotate it 180 degrees, we call it 2-Matrix, etc … This is said if you rotate the Matrix 90*k degrees, then we call it k-Matrix. Now, your task is to calculate all the sum of i-Matrix (0<= i<= k).
 

Input
There multiple test cases. Each test case begins with one integer n(1 <= n <= 10), following n lines, each line contains n integers, describe the original matrix, then a single line contains a k
(1 <= k <= 10^8)described above. Process to end of file.
 

Output
For each case, output the sum of all the i-Matrix (0<= i<= k) in n lines.
Each line there are n integers separated by one space.
Note that there is no extra space at the end of each line.
 

Sample Input
31 2 32 3 43 4 5 10
 

Sample Output
33 32 3134 33 32

35 34 33

参考代码:

#include <stdio.h>#include<cstdio>#include<algorithm>#include <iostream>using namespace std;const int maxn=100001;int n,m;int s[maxn<<2],sl[maxn<<2],sr[maxn<<2],ss[maxn<<2];void pushup(int rt){        s[rt]=s[rt<<1]+s[rt<<1|1];        sl[rt]=max(s[rt<<1|1]+sl[rt<<1],sl[rt<<1|1]);        sr[rt]=max(s[rt<<1]+sr[rt<<1|1],sr[rt<<1]);        ss[rt]=max(ss[rt<<1],ss[rt<<1|1]);        ss[rt]=max(ss[rt],sl[rt<<1]+sr[rt<<1|1]);}void init(int rt,int l,int r){        int m=(l+r)>>1;        if(l!=r)        {                init(rt<<1,l,m);                init(rt<<1|1,m+1,r);                pushup(rt);        }        else        {                scanf("%d",&s[rt]);                sl[rt]=sr[rt]=ss[rt]=s[rt];        }}void change(int rt,int l,int r,int t,int tt){        if(l==t&&r==t)        {                sl[rt]=sr[rt]=ss[rt]=s[rt]=tt;        }        else        {                int m=(l+r)>>1;                if(m>=t)                        change(rt<<1,l,m,t,tt);                else                        change(rt<<1|1,m+1,r,t,tt);                pushup(rt);        }}int main(){        int t,tt;        while(scanf("%d%d",&n,&m)!=EOF)        {                init(1,1,n);                while(m--)                {                        scanf("%d%d",&t,&tt);                        change(1,1,n,t,tt);                        printf("%d\n",ss[1]);                }        }}

原创粉丝点击