Can you help the God of sheep?
InputThe first line contains an integer T (T ≤ 5) indicating the number of test cases. Then T test cases follow.
Each test case begins with an integer n (5 < n < 800). Then the following n rows describe the picture to pixelate, where each row has n integers representing the original color values. The j-th integer in the i-th row is the color value of cell (i, j) of the picture. Color values are nonnegative integers and will not exceed 1,000,000,000 (10^9).
After the description of the picture, there is an integer Q (Q ≤ 100000 (10^5)), indicating the number of mosaics.
Then Q actions follow: the i-th row gives the i-th replacement made by the God of sheep: xi, yi, Li (1 ≤ xi, yi ≤ n, 1 ≤ Li < 10000, Li is odd). This means the God of sheep will change the color value in (xi, yi) (located at row xi and column yi) according to the Li x Li region as described above. For example, an query (2, 3, 3) means changing the color value of the cell at the second row and the third column according to region (1, 2) (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 2), (3, 3), (3, 4). Notice that if the region is not entirely inside the picture, only cells that are both in the region and the picture are considered.
Note that the God of sheep will do the replacement one by one in the order given in the input.��OutputFor each test case, print a line "Case #t:"(without quotes, t means the index of the test case) at the beginning.
For each action, print the new color value of the updated cell.Sample Input
1 3 1 2 3 4 5 6 7 8 9 5 2 2 1 3 2 3 1 1 3 1 2 3 2 2 3
Sample Output
Case #1: 5 6 3 4 6
题意:
看清楚是单点修改。
题解,树套树,外面一层是那几行,里面是行的列区间,nlognlogn的时间复杂度。
1 #include<cstring> 2 #include<cmath> 3 #include<algorithm> 4 #include<iostream> 5 #include<cstdio> 6 7 #define N 807 8 #define ll long long 9 using namespace std; 10 inline int read() 11 { 12 int x=0,f=1;char ch=getchar(); 13 while(ch>'9'||ch<'0'){if (ch=='-') f=-1;ch=getchar();} 14 while(ch<='9'&&ch>='0'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} 15 return x*f; 16 } 17 18 int n,m; 19 int a[N][N]; 20 struct Node 21 { 22 int mi,mx; 23 }tr[N<<2][N<<2]; 24 25 void build_sec(int lct,int p,int l,int r,int fla) 26 { 27 if (l==r) 28 { 29 if (fla)tr[lct][p].mi=tr[lct][p].mx=a[fla][l]; 30 else tr[lct][p].mi=min(tr[lct<<1][p].mi,tr[lct<<1|1][p].mi),tr[lct][p].mx=max(tr[lct<<1][p].mx,tr[lct<<1|1][p].mx); 31 return; 32 } 33 int mid=(l+r)>>1; 34 build_sec(lct,p<<1,l,mid,fla),build_sec(lct,p<<1|1,mid+1,r,fla); 35 if (fla) 36 { 37 tr[lct][p].mi=min(tr[lct][p<<1].mi,tr[lct][p<<1|1].mi); 38 tr[lct][p].mx=max(tr[lct][p<<1].mx,tr[lct][p<<1|1].mx); 39 } 40 else 41 { 42 tr[lct][p].mi=min(tr[lct<<1][p].mi,tr[lct<<1|1][p].mi); 43 tr[lct][p].mx=max(tr[lct<<1][p].mx,tr[lct<<1|1][p].mx); 44 } 45 } 46 void build_fir(int p,int l,int r) 47 { 48 if (l==r) 49 { 50 build_sec(p,1,1,n,l); 51 return; 52 } 53 int mid=(l+r)>>1; 54 build_fir(p<<1,l,mid),build_fir(p<<1|1,mid+1,r); 55 build_sec(p,1,1,n,0); 56 } 57 int query_mi2(int lct,int p,int l,int r,int x,int y) 58 { 59 if (l==x&&y==r) return tr[lct][p].mi; 60 int mid=(l+r)>>1; 61 if (y<=mid) return query_mi2(lct,p<<1,l,mid,x,y); 62 else if (x>mid) return query_mi2(lct,p<<1|1,mid+1,r,x,y); 63 else return min(query_mi2(lct,p<<1,l,mid,x,mid),query_mi2(lct,p<<1|1,mid+1,r,mid+1,y)); 64 } 65 int query_mi1(int p,int l,int r,int x,int y,int x1,int y1) 66 { 67 if (l==x&&r==y) return query_mi2(p,1,1,n,x1,y1); 68 int mid=(l+r)>>1; 69 if (y<=mid) return query_mi1(p<<1,l,mid,x,y,x1,y1); 70 else if (x>mid) return query_mi1(p<<1|1,mid+1,r,x,y,x1,y1); 71 else return min(query_mi1(p<<1,l,mid,x,mid,x1,y1),query_mi1(p<<1|1,mid+1,r,mid+1,y,x1,y1)); 72 } 73 int query_mx2(int lct,int p,int l,int r,int x,int y) 74 { 75 if (l==x&&y==r) return tr[lct][p].mx; 76 int mid=(l+r)>>1; 77 if (y<=mid) return query_mx2(lct,p<<1,l,mid,x,y); 78 else if (x>mid) return query_mx2(lct,p<<1|1,mid+1,r,x,y); 79 else return max(query_mx2(lct,p<<1,l,mid,x,mid),query_mx2(lct,p<<1|1,mid+1,r,mid+1,y)); 80 } 81 int query_mx1(int p,int l,int r,int x,int y,int x1,int y1) 82 { 83 if (l==x&&r==y) return query_mx2(p,1,1,n,x1,y1); 84 int mid=(l+r)>>1; 85 if (y<=mid) return query_mx1(p<<1,l,mid,x,y,x1,y1); 86 else if (x>mid) return query_mx1(p<<1|1,mid+1,r,x,y,x1,y1); 87 else return max(query_mx1(p<<1,l,mid,x,mid,x1,y1),query_mx1(p<<1|1,mid+1,r,mid+1,y,x1,y1)); 88 } 89 void update(int lct,int p,int l,int r,int x) 90 { 91 tr[lct][p].mi=min(tr[lct<<1][p].mi,tr[lct<<1|1][p].mi); 92 tr[lct][p].mx=max(tr[lct<<1][p].mx,tr[lct<<1|1][p].mx); 93 if (l==r)return; 94 int mid=(l+r)>>1; 95 if (x<=mid) update(lct,p<<1,l,mid,x); 96 else update(lct,p<<1|1,mid+1,r,x); 97 } 98 void modify_sec(int lct,int p,int l,int r,int x,int z) 99 { 100 if (l==r){tr[lct][p].mi=tr[lct][p].mx=z;return;} 101 int mid=(l+r)>>1; 102 if (x<=mid) modify_sec(lct,p<<1,l,mid,x,z); 103 else modify_sec(lct,p<<1|1,mid+1,r,x,z); 104 tr[lct][p].mi=min(tr[lct][p<<1].mi,tr[lct][p<<1|1].mi); 105 tr[lct][p].mx=max(tr[lct][p<<1].mx,tr[lct][p<<1|1].mx); 106 } 107 void modify_fir(int p,int l,int r,int x,int y,int z) 108 { 109 if(l==r){modify_sec(p,1,1,n,y,z);return;} 110 int mid=(l+r)>>1; 111 if (x<=mid)modify_fir(p<<1,l,mid,x,y,z); 112 else modify_fir(p<<1|1,mid+1,r,x,y,z); 113 update(p,1,1,n,y); 114 } 115 int main() 116 { 117 freopen("fzy.in","r",stdin); 118 freopen("fzy.out","w",stdout); 119 120 int T=read(),Case=0; 121 while(T--) 122 { 123 printf("Case #%d: ",++Case); 124 n=read(); 125 for (int i=1;i<=n;i++) 126 for (int j=1;j<=n;j++) 127 a[i][j]=read(); 128 build_fir(1,1,n); 129 m=read(); 130 for (int i=1;i<=m;i++) 131 { 132 int x=read(),y=read(),z=read()/2; 133 int x1=max(x-z,1),x2=min(x+z,n),y1=max(y-z,1),y2=min(y+z,n); 134 int ansmi=query_mi1(1,1,n,x1,x2,y1,y2); 135 int ansmx=query_mx1(1,1,n,x1,x2,y1,y2); 136 int ans=(ansmx+ansmi)/2; 137 printf("%d ",ans); 138 modify_fir(1,1,n,x,y,ans); 139 } 140 } 141 }