-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimosDim2.cpp
More file actions
89 lines (79 loc) · 2.6 KB
/
Copy pathimosDim2.cpp
File metadata and controls
89 lines (79 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <numeric>
using namespace std;
int main(void){
// 2次元imos
// 指定された範囲に加算命令を数回行う。
// その時の行列を求める。
const int H = 3;
const int W = 4;
/* 一般的な解法
+1 +3 -2
初期状態 [1,1] ~ [2,2] [2,2] ~ [3,3] [1,2] ~ [3,4]
| 0| 0| 0| 0| | 1| 1| 0| 0| | 1| 1| 0| 0| | 1|-1|-2|-2|
| 0| 0| 0| 0| => | 1| 1| 0| 0| => | 1| 4| 3| 0| => | 1| 2| 1|-2|
| 0| 0| 0| 0| | 0| 0| 0| 0| | 0| 3| 3| 0| | 0| 1| 1|-2|
*/
/* 2次元imos
基本は1次元の時と同じ。
ただし、斜め下は加算し縦を減算する。outOfBoundsの場合は無視。
例) +1
| 0| 0| => |+1| 0|-1|
| 0| 0| | 0| 0|
-1| +1|
+1 +3 -2
初期状態 [1,1] ~ [2,2] [2,2] ~ [3,3] [1,2] ~ [3,4]
| 0| 0| 0| 0| | 1| 0|-1| 0| | 1| 0|-1| 0| | 1|-2|-1| 0|
| 0| 0| 0| 0| => | 0| 0| 0| 0| => | 0| 3| 0|-3| => | 0| 3| 0|-3|
| 0| 0| 0| 0| |-1| 0| 1| 0| |-1| 0|-1| 0| |-1| 0|-1| 0|
*/
int a[H+1][W+1];
fill(a[0],a[H+1],0);
// +1 +3 -2
a[1][1] += 1; a[2][2] += 3; a[1][2] += -2;
a[1][3] -= 1; a[2][4] -= 3; //a[1][5] += -2;
a[3][1] -= 1; //a[4][2] += 3; a[4][2] += -2;
a[3][3] += 1; //a[4][4] += 3; a[4][5] += -2;
// for(int i = 1; i <= H;i++){
// for(int k = 1; k <= W;k++){
// printf("%2d ",a[i][k]);
// }
// cout << endl;
// }
/*
横の累積和を求める
| 1|-1|-2|-2|
| 0| 3| 3| 0|
|-1|-1| 0| 0|
*/
// partial_sum(x,y,a[])の範囲がx <= index < y であることに注意
for(int i = 1; i <= H;i++){
partial_sum(&a[i][0],&a[i][W+1],&a[i][0]);
}
cout << "partial_sum of row" << endl;
for(int i = 1; i <= H;i++){
for(int k = 1; k <= W;k++){
printf("%2d ",a[i][k]);
}
cout << endl;
}
/*
縦の累積和を求める
| 1|-1|-2|-2|
| 1| 2| 1|-2|
| 0| 1| 1|-2|
*/
cout << "partial_sum of column" << endl;
for(int i = 1; i <= H;i++){
for(int k = 1; k <= W;k++){
a[i][k] += a[i-1][k];
}
}
for(int i = 1; i <= H;i++){
for(int k = 1; k <= W;k++){
printf("%2d ",a[i][k]);
}
cout << endl;
}
return 0;
}