Advertisement
pb_jiang

CF1722E

Aug 30th, 2022
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. using ll = long long;
  6. using pii = pair<int, int>;
  7.  
  8. int t, n, q;
  9. vector<int> rs[1 << 10];
  10. vector<ll> acc[1 << 10];
  11.  
  12. int main(int argc, char **argv)
  13. {
  14.     scanf("%d", &t);
  15.     while (t--) {
  16.         scanf("%d%d", &n, &q);
  17.         int h, w;
  18.         for (int i = 1; i <= 1000; ++i)
  19.             rs[i].clear(), acc[i].clear();
  20.  
  21.         for (int i = 0; i < n; ++i)
  22.             scanf("%d%d", &h, &w), rs[h].push_back(w);
  23.         for (int i = 1; i <= 1000; ++i) {
  24.             sort(rs[i].begin(), rs[i].end());
  25.             int rn = rs[i].size();
  26.             acc[i] = vector<ll>(rn + 1);
  27.             for (int j = 0; j < rn; ++j) {
  28.                 acc[i][j + 1] = acc[i][j] + ll(i) * rs[i][j];
  29.             }
  30.         }
  31.  
  32.         for (int i = 0; i < q; ++i) {
  33.             int hs, ws, hb, wb;
  34.             scanf("%d%d%d%d", &hs, &ws, &hb, &wb);
  35.             ll total = 0;
  36.             for (int j = hs + 1; j < hb; ++j) {
  37.                 int lb = upper_bound(rs[j].begin(), rs[j].end(), ll(ws)) - rs[j].begin();
  38.                 int ub = lower_bound(rs[j].begin(), rs[j].end(), ll(wb)) - rs[j].begin();
  39.                 if (ub < lb) {
  40.                     continue;
  41.                 }
  42.                 total += acc[j][ub] - acc[j][lb];
  43.             }
  44.             printf("%lld\n", total);
  45.         }
  46.     }
  47.     return 0;
  48. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement