mr_dot_convict

12192-Grapevine-UVa-mr.convict

Jun 10th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  2.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  3.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  4.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  5.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  6. When I wrote this, only God and I understood what I was doing
  7.  ** * * * * * * * Now, only God knows * * * * * * */
  8. #include         <bits/stdc++.h>
  9. using namespace std;
  10. #pragma GCC      optimize ("Ofast")
  11. #pragma GCC      optimize ("unroll-loops")
  12. #pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  13.  
  14. #define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
  15. #define PREC     cout.precision (10); cout << fixed
  16. #define bg(x)    " [ " << #x << " : " << (x) << " ]"
  17. #define x        first
  18. #define y        second
  19.  
  20. #define debug(args...) { \
  21.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  22.    stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); \
  23. }
  24. void err(istream_iterator<string> it) { it->empty();
  25.    cerr << " (Line : " << __LINE__ << ")" << '\n';
  26. }
  27. template<typename T, typename... Args>
  28. void err(istream_iterator<string> it, T a, Args... args) {
  29.     cerr << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  30.     err(++it, args...);
  31. }
  32. const int N = 504;
  33. int mat[N][N];
  34. int n, m, L, U, mx_sz;
  35.  
  36. void solve() {
  37.    int q; cin >> q;
  38.    while(q--) {
  39.       cin >> L >> U;
  40.       assert(L <= U);
  41.       mx_sz = INT_MIN;
  42.  
  43.       for (int row = 0; row < n; ++row) {
  44.          int l = 0, h = m - 1, g, col, sz;
  45.          while (l <= h) {
  46.             g = (l + h)/2;
  47.             if (mat[row][g] >= L) h = g - 1;
  48.             else l = g + 1;
  49.          }
  50.          if (l == m) continue;
  51.          col = l;
  52.  
  53.          l = 0, h = min(n - row, m - col) - 1; //size = ans + 1
  54.          while (l <= h) {
  55.             g = (l + h)/2;
  56.             if (mat[row + g][col + g] <= U) l = g + 1;
  57.             else h = g - 1;
  58.          }
  59.          sz = h + 1;
  60.          mx_sz = max(mx_sz, sz);
  61.          // debug(l, g, h, row, col, mat[row][col], mat[row+h][col+h]);
  62.       }
  63.       if (mx_sz == INT_MIN) mx_sz = 0;
  64.       cout << mx_sz << "\n";
  65.    }
  66.    cout << "-\n";
  67. }
  68.  
  69. void read() {
  70.    for (int i = 0; i < n; ++i)
  71.       for (int j = 0; j < m; ++j)
  72.          cin >> mat[i][j];
  73. }
  74.  
  75. signed main() {
  76.    IOS; PREC;
  77.    while (cin >> n >> m, n || m) {
  78.       read();
  79.       solve();
  80.    }
  81.  
  82.    return EXIT_SUCCESS;
  83. }
Add Comment
Please, Sign In to add comment