Advertisement
PGSStas

Untitled

Mar 23rd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define F first
  4. #define S second
  5. #define ll long long
  6. #define ull unsigned long long
  7. #define ld long double
  8. #define SELO ""
  9. #define openfiles ifstream cin("input"  SELO  ".txt"); ofstream cout("output"  SELO  ".txt");
  10. #define faster ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
  11. #define all(x) x.begin(), x.end()
  12.  
  13. using namespace std;
  14.  
  15. struct point
  16. {
  17.     int stx = 0, sty = 0, fx = 0, fy = 0;
  18.     int it;
  19. };
  20.  
  21. int n, m, q;
  22. vector <vector <int> > a;
  23. vector <point> Queries;
  24. vector <int> ans;
  25. priority_queue <pair <int, pair <int, int> > > s;
  26. vector <vector <int> > len;
  27.  
  28. void dfs(int l, int r, vector <point> cur)
  29. {
  30.     int mid = (l + r) / 2;
  31.     for(int zzz = 0; zzz < n; zzz++)
  32.     {
  33.         s.push({-a[zzz][mid], {zzz, mid}});
  34.         len.assign(n, vector <int> (m, 1000000000));
  35.         len[zzz][mid] = a[zzz][mid];
  36.         while(s.size())
  37.         {
  38.             int x = s.top().S.F;
  39.             int y = s.top().S.S;
  40.             s.pop();
  41.             int px[]{-1, 1, 0, 0};
  42.             int py[]{0, 0, 1, -1};
  43.             for(int i = 0; i < 4; i++)
  44.                 if(x + px[i] >= 0 && x + px[i] < n && y + py[i] >= l && y + py[i] <= r && len[x + px[i]][y + py[i]] > len[x][y] + a[x + px[i]][y + py[i]])
  45.                 {
  46.                     len[x + px[i]][y + py[i]] = len[x][y] + a[x + px[i]][y + py[i]];
  47.                     s.push({-len[x + px[i]][y + py[i]], {x + px[i], y + py[i]}});
  48.                 }
  49.         }
  50.         for(int i = 0; i < cur.size(); i++)
  51.         {
  52.             ans[cur[i].it] = min(ans[cur[i].it], len[cur[i].stx][cur[i].sty] + len[cur[i].fx][cur[i].fy] - a[zzz][mid]);
  53.         }
  54.     }
  55.     vector <point> Left, Right;
  56.     Left.reserve(cur.size());
  57.     Right.reserve(cur.size());
  58.     for(int i = 0; i < cur.size(); i++)
  59.     {
  60.         if(cur[i].sty >= l && cur[i].sty < mid && cur[i].fy >= l && cur[i].fy < mid)
  61.             Left.push_back(cur[i]);
  62.         if(cur[i].sty > mid && cur[i].sty <= r && cur[i].fy > mid && cur[i].fy <= r)
  63.             Right.push_back(cur[i]);
  64.     }
  65.     if(l == r)
  66.         return;
  67.     if(Left.size() != 0)
  68.         dfs(l, mid - 1, Left);
  69.     if(Right.size() != 0)
  70.         dfs(mid + 1, r, Right);
  71. }
  72.  
  73. /**
  74. 3 3 3
  75. 7 5 7
  76. 2 6 1
  77. 4 8 4
  78. 1 2 1 1
  79. 2 1 2 3
  80. 1 3 1 1
  81. **/
  82.  
  83. int main()
  84. {
  85.     openfiles
  86.     faster
  87.     cin >> n >> m >> q;
  88.     a.assign(n, vector <int> (m, 0));
  89.     for(int i = 0; i < m; i++)
  90.     {
  91.         for(int j = 0; j < n; j++)
  92.         {
  93.             cin >> a[j][i];
  94.         }
  95.     }
  96.     ans.assign(q, 1000000000);
  97.     for(int i = 0; i < q; i++)
  98.     {
  99.         int l, r, x, y;
  100.         cin >> l >> r >> x >> y;
  101.         point not_Used;
  102.         not_Used.stx = l - 1;
  103.         not_Used.fx = x - 1;
  104.         not_Used.sty = r - 1;
  105.         not_Used.fy = y - 1;
  106.         not_Used.it = i;
  107.         Queries.push_back(not_Used);
  108.     }
  109.     dfs(0, m - 1, Queries);
  110.     for(int i = 0; i < q; i++)
  111.         cout << ans[i] << '\n';
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement