fork(1) download
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6.  
  7. #define mod 1000000007
  8. #define pb push_back
  9. #define mp make_pair
  10. #define PI 3.1415926535
  11. #define pf push_front
  12. #define ll long long int
  13. #define ull unsigned long long int
  14. typedef pair<int,int> pii;
  15. uint64_t seed = reinterpret_cast<uint64_t>(new char);
  16. mt19937_64 rd(seed);
  17. //uniform_int_distribution<int> rg(1, n);
  18. //int RandomNumber = rg(rd);
  19. // vector < int > v(N,-1); sqrtl for square root of ll
  20. // vector of size N with initial value -1
  21. typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
  22. typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> OST;
  23. #define gc getchar_unlocked
  24.  
  25. const int N=60005;
  26. const int M=1000005;
  27. int upd[2005];
  28. vector <pii> v[1005];
  29. int main ()
  30. {
  31. ios_base::sync_with_stdio(false);
  32. cin.tie(NULL);
  33. int i,j,k,l,n,m,p;
  34. cin>>n>>m>>k;
  35. int arr[n+5][m+5];
  36. for(i=0;i<n;i++)
  37. {
  38. for(j=0;j<m;j++)
  39. {
  40. cin>>arr[i][j];
  41. }
  42. }
  43. int cnt=0;
  44. for(i=0;i<n;i++)
  45. {
  46. for(auto pk : v[i]){
  47. upd[pk.first]--; upd[pk.second]++;
  48. }
  49. int pre=0;
  50. for(j=0;j<m;j++)
  51. {
  52. pre=pre+upd[j];
  53. int xd=pre;
  54. if(arr[i][j]==0){
  55. if(xd%2==0){
  56. if((j+k-1)>=m || (i+k-1)>=n){
  57. cout<<-1<<endl; return 0;
  58. }
  59. upd[j]++; pre++; upd[j+k]--;
  60. v[i+k].pb({j,j+k});
  61. cnt++;
  62. }
  63. }
  64. if(arr[i][j]==1){
  65. if(xd%2){
  66. if((j+k-1)>=m || (i+k-1)>=n){
  67. cout<<-1<<endl; return 0;
  68. }
  69. upd[j]++; pre++; upd[j+k]--;
  70. cnt++; v[i+k].pb({j,j+k});
  71. }
  72. }
  73. }
  74. }
  75. cout<<cnt<<endl;
  76. return 0;
  77. }
  78.  
Success #stdin #stdout 0s 15272KB
stdin
Standard input is empty
stdout
0