fork download
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4.  
  5. #define ll long long
  6. #define fi(i,l,n) for(int i=l; i<n; i++)
  7. #define fd(i,n,l) for(int i=n; i>=l; i--)
  8. #define gets(s) string s; cin>>s;
  9. #define getv(v,n) vector<ll> v(n+1); fi(i,1,n+1) cin>>v[i];
  10. #define all1(v) v.begin() + 1, v.end()
  11. #define all(v) v.begin(), v.end()
  12. #define seev(v,n) fr(i,1,n+1) {cout<<v[i]<<' ';} cout<<endl;
  13. #define vl vector<ll>
  14. #define vvl vector<vector<ll>>
  15. #define vp vector<pair<ll,ll>>
  16. #define vc vector<char>
  17. #define vvc vector<vector<char>>
  18.  
  19. #define pb push_back
  20. #define mp make_pair
  21. #define mse multiset<ll>
  22. #define se set<ll>
  23. #define ma map<ll,ll>
  24. #define getmat(v,n,m) vvl v(n+1,vl(m+1)); fi(i,1,n+1) {fi (j,1,m+1) cin>>v[i][j];}
  25. #define seemat(mat) fi(i,1,n+1) {fi (j,1,m+1) cout<<v[i][j]<<' '; cout<<endl;}
  26. #define YES cout<<"YES"<<endl;
  27. #define NO cout<<"NO"<<endl;
  28. using namespace std;
  29. using namespace __gnu_pbds;
  30.  
  31. // Define ordered multiset with long long
  32.  
  33. typedef tree<long long, long long, less<long>, rb_tree_tag, tree_order_statistics_node_update> ordered_multimap;
  34. typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
  35.  
  36. vl sieve;
  37. void SieveOfEratosthenes(int n)
  38. {
  39. // Create a boolean array "prime[0..n]" and initialize
  40. // all entries it as true. A value in prime[i] will
  41. // finally be false if i is Not a prime, else true.
  42. vl prime(n+1,true);
  43.  
  44. for (int p = 2; p * p <= n; p++) {
  45. // If prime[p] is not changed, then it is a prime
  46. if (prime[p] == true) {
  47. // Update all multiples of p greater than or
  48. // equal to the square of it numbers which are
  49. // multiple of p and are less than p^2 are
  50. // already been marked.
  51. for (int i = p * p; i <= n; i += p)
  52. prime[i] = false;
  53. }
  54. }
  55.  
  56. // Print all prime numbers
  57. for (int p = 2; p <= n; p++)
  58. if (prime[p])
  59. sieve.push_back(p);
  60. }
  61.  
  62. class DisjointSet{
  63. private:
  64. vector<int> parent;
  65.  
  66. public:
  67. DisjointSet(int n){
  68. parent.resize(n+1,-1);
  69. }
  70.  
  71. int findParent(int x){
  72. if(parent[x] < 0) return x;
  73. return parent[x] = findParent(parent[x]);
  74. }
  75.  
  76. void Union(int x, int z){
  77. int px = findParent(x);
  78. int pz = findParent(z);
  79.  
  80. if(px == pz) return;
  81.  
  82. if(parent[px] < parent[pz]){
  83. parent[px] += parent[pz];
  84. parent[pz] = px;
  85. }
  86. else {
  87. parent[pz] += parent[px];
  88. parent[px] = pz;
  89. }
  90.  
  91. }
  92.  
  93. int getSize(int x){
  94. int px = findParent(x);
  95. return ((-1)*parent[px]);
  96. }
  97. };
  98.  
  99. bool sortbysec(const pair<ll, ll>& a, const pair<ll, ll>& b){
  100. return (a.second < b.second);
  101. }
  102.  
  103.  
  104. ll gcd(ll a, ll b){
  105. if (b == 0) return a;
  106. return gcd(b,a%b);
  107. }
  108.  
  109.  
  110. ll pwr(ll a, ll b, ll mod = 0){
  111. ll result = 1;
  112. if(mod == 0){
  113. while(b){
  114. if(b & 1) result *= a;
  115. a *= a;
  116. b = b >> 1;
  117. }
  118. }
  119. else {
  120. while(b){
  121. if(b & 1){
  122. result *= a;
  123. result = result % mod;
  124. }
  125. a *= a;
  126. a = a % mod;
  127. b = b >> 1;
  128. }
  129. }
  130. return result;
  131. }
  132.  
  133.  
  134. ll modularinverse(ll a,ll p)// p is prime
  135. {
  136. return pwr(a,p-2,p);
  137. }
  138.  
  139. void solve(ll tc){
  140. int n,m;
  141. cin>>n>>m;
  142.  
  143. string s;
  144. cin>>s;
  145.  
  146. ll a[n][m];
  147.  
  148. fi(i,0,n) fi(j,0,m) cin>>a[i][j];
  149.  
  150. int r=0,c=0;
  151. int step = 0;
  152. a[r][c] = 0;
  153.  
  154. ll sum=0;
  155.  
  156. if(s[0] == 'D'){
  157. fi(col,0,m) sum+=a[0][col];
  158. }
  159. else{
  160. fi(row,0,n) sum+=a[row][0];
  161. }
  162.  
  163. a[r][c] = -1*sum;
  164. sum=0;
  165.  
  166.  
  167.  
  168. while(step<n+m-1){
  169. ll curr=0;
  170. step++;
  171.  
  172. if(s[step-1] == 'D'){
  173. r++;
  174. }
  175. else c++;
  176.  
  177. if(s[step] == 'D'){
  178. fi(col,0,m){
  179. if(col == c) continue;
  180. curr+=a[r][col];
  181. }
  182.  
  183. a[r][c] = sum - curr;
  184. }
  185. else{
  186. fi(row,0,n){
  187. if(row == r) continue;
  188. curr+=a[row][c];
  189. }
  190.  
  191. a[r][c] = sum - curr;
  192. }
  193.  
  194. }
  195.  
  196.  
  197. // ll curr = 0;
  198.  
  199. fi(i,0,n){
  200. fi(j,0,m) cout<<a[i][j]<<' ';
  201. cout<<endl;
  202. }
  203.  
  204. // fi(i,0,n){
  205. // fi(j,0,m) curr+=a[i][j];
  206. // if(curr!= 0 ) NO
  207. // curr = 0;
  208. // }
  209.  
  210. // fi(j,0,m){
  211. // fi(i,0,n) curr += a[i][j];
  212. // if(curr != 0) NO
  213. // curr = 0;
  214. // }
  215. // YES
  216. }
  217.  
  218. int main(){
  219. ios_base::sync_with_stdio(false);
  220. cin.tie(NULL);
  221. cout.tie(NULL);
  222.  
  223. int t=1;
  224. cin>>t;
  225. ll tc=0;
  226. while(t--){
  227. tc++;
  228. solve(tc);
  229. }
  230.  
  231. return 0;
  232. }
Success #stdin #stdout 0s 5288KB
stdin
4
3 3
DRRD
0 2 3
0 0 0
3 1 0
4 5
DRRRRDD
0 1 0 2 3
0 0 0 0 0
-1 0 -3 -3 0
0 0 0 -1 0
2 3
RRD
0 0 0
0 1 0
5 5
DDDDRRRR
0 25 2 9 11
0 6 13 20 22
0 17 24 1 8
0 3 10 12 19
0 0 0 0 0
stdout
-5 2 3 
2 -3 1 
3 1 -4 
-6 1 0 2 3 
7 -1 3 2 -11 
-1 0 -3 -3 7 
0 0 0 -1 1 
0 -1 1 
0 1 -1 
-47 25 2 9 11 
-61 6 13 20 22 
-50 17 24 1 8 
-44 3 10 12 19 
202 -51 -49 -42 -60