fork(1) download
  1. // iostream is too mainstream
  2. #include <cstdio>
  3. // bitch please
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <cstdlib>
  7. #include <vector>
  8. #include <set>
  9. #include <map>
  10. #include <queue>
  11. #include <stack>
  12. #include <list>
  13. #include <cmath>
  14. #include <iomanip>
  15. #define dibs reserve
  16. #define OVER9000 123456789
  17. #define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
  18. #define tisic 47
  19. #define soclose 1e-8
  20. #define chocolate win
  21. // so much chocolate
  22. #define patkan 9
  23. #define ff first
  24. #define ss second
  25. #define abs(x) ((x < 0)?-(x):x)
  26. #define uint unsigned int
  27. #define dbl long double
  28. using namespace std;
  29. // mylittledoge
  30.  
  31. int main() {
  32. cin.sync_with_stdio(0);
  33. cin.tie(0);
  34. int N;
  35. cin >> N;
  36. long long mod =1000000007;
  37. vector<string> V(N);
  38. for(int i =0; i < N; i++) cin >> V[i];
  39.  
  40. vector< vector< pair<int,long long> > > A(N+2,vector<pair<int,long long> >(N+2,make_pair(0,0)));
  41. A[1][1] =make_pair((int)(V[0][0] == 'C'),1);
  42. for(int i =1; i <= N; i++) for(int j =1; j <= N; j++) if(i != 1 || j != 1) {
  43. pair<int,long long> p1 =A[i-1][j], p2 =A[i][j-1];
  44. if(p1.ff < p2.ff) swap(p1,p2);
  45. if(p1.ff == p2.ff) p1.ss +=p2.ss;
  46. if(p1.ss >= mod) p1.ss -=mod;
  47. p1.ff +=(int)(V[i-1][j-1] == 'C');
  48. A[i][j] =p1;}
  49.  
  50. vector< vector< pair<int,long long> > > B(N+2,vector<pair<int,long long> >(N+2,make_pair(0,0)));
  51. B[N][N] =make_pair((int)(V[N-1][N-1] == 'C'),1);
  52. for(int i =N; i > 0; i--) for(int j =N; j > 0; j--) if(i < N || j < N) {
  53. pair<int,long long> p1 =B[i+1][j], p2 =B[i][j+1];
  54. if(p1.ff < p2.ff) swap(p1,p2);
  55. if(p1.ff == p2.ff) p1.ss +=p2.ss;
  56. if(p1.ss >= mod) p1.ss -=mod;
  57. p1.ff +=(int)(V[i-1][j-1] == 'C');
  58. B[i][j] =p1;}
  59.  
  60. int Q;
  61. cin >> Q;
  62. for(int q =0; q < Q; q++) {
  63. int r1,r2,c1,c2;
  64. cin >> r1 >> c1 >> r2 >> c2;
  65. pair<int,long long> p =make_pair(0,0);
  66. for(int i =r2+2; i <= N; i++) {
  67. pair<int,long long> p1 =A[i][c1], p2 =B[i][c1+1];
  68. p1.ff +=p2.ff;
  69. p1.ss =(p1.ss*p2.ss)%mod;
  70. if(p1.ss < 0) p1.ss +=mod;
  71. if(p1.ss == 0) p1.ff =0;
  72. if(p1.ff > p.ff) {
  73. p.ff =p1.ff;
  74. p.ss =0;}
  75. if(p1.ff == p.ff) p.ss +=p1.ss;
  76. if(p.ss >= mod) p.ss -=mod;}
  77. for(int j =c2+2; j <= N; j++) {
  78. pair<int,long long> p1 =A[r1][j], p2 =B[r1+1][j];
  79. p1.ff +=p2.ff;
  80. p1.ss =(p1.ss*p2.ss)%mod;
  81. if(p1.ss < 0) p1.ss +=mod;
  82. if(p1.ss == 0) p1.ff =0;
  83. if(p1.ff > p.ff) {
  84. p.ff =p1.ff;
  85. p.ss =0;}
  86. if(p1.ff == p.ff) p.ss +=p1.ss;
  87. if(p.ss >= mod) p.ss -=mod;}
  88. if(p.ss == 0) p.ff =0;
  89. cout << p.ff << " " << p.ss << "\n";}
  90. return 0;}
  91.  
  92. // look at my code
  93. // my code is amazing
  94.  
Time limit exceeded #stdin #stdout 5s 528896KB
stdin
Standard input is empty
stdout
Standard output is empty