QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#385438#6846. Wiring Engineeringyaoxi_stdWA 21ms14288kbC++146.1kb2024-04-10 19:15:072024-04-10 19:15:07

Judging History

你现在查看的是最新测评结果

  • [2024-04-10 19:15:07]
  • 评测
  • 测评结果:WA
  • 用时:21ms
  • 内存:14288kb
  • [2024-04-10 19:15:07]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define debug(fmt, ...) \
  fprintf(stderr, "[%d] " fmt "\n", __LINE__, ##__VA_ARGS__)
template <class _Tx, class _Ty>
inline void chkmin(_Tx& x, const _Ty& y) { if (y < x) x = y; }
template <class _Tx, class _Ty>
inline void chkmax(_Tx& x, const _Ty& y) { if (x < y) x = y; }
bool Mbe;
using ll = long long;
using query_t = tuple<int, int, int, int, int>;
constexpr int N = 505, Q = 3e5 + 10, inf = 1e9;
int n, q, a[N], b[N], w[N][N], dpl[N][N][2][2], dpr[N][N][2][2], ans[Q];
void solve(int la, int ra, int lb, int rb, const vector<query_t>& qrys) {
  if (la > ra || lb > rb || qrys.empty()) return;
  if (la == ra && lb == rb) {
    for (auto it : qrys) {
      int qla, qra, qlb, qrb, id;
      tie(qla, qra, qlb, qrb, id) = it;
      chkmax(ans[id], w[qla][qlb] - a[qla] - b[qlb]);
    }
    return;
  }
  auto calc_l = [](int ra, int la, int rb, int lb) {
    for (int j = ra; j >= la; --j) {
      for (int k = rb; k >= lb; --k) {
        if (j != ra && k != rb) memset(dpl[j][k], -0x3f, sizeof(dpl[j][k]));
        if (j < ra) {
          for (int x = 0; x < 2; ++x) {
            for (int y = 0; y < 2; ++y) {
              chkmax(dpl[j][k][0][y], dpl[j + 1][k][x][y]);
              chkmax(dpl[j][k][1][y], dpl[j + 1][k][x][y] - a[j] + (y ? w[j][k] : 0));
            }
          }
        }
        if (k < rb) {
          for (int x = 0; x < 2; ++x) {
            for (int y = 0; y < 2; ++y) {
              chkmax(dpl[j][k][x][0], dpl[j][k + 1][x][y]);
              chkmax(dpl[j][k][x][1], dpl[j][k + 1][x][y] - b[k] + (x ? w[j][k] : 0));
            }
          }
        }
      }
    }
  };
  auto calc_r = [](int la, int ra, int lb, int rb) {
    for (int j = la; j <= ra; ++j) {
      for (int k = lb; k <= rb; ++k) {
        if (j != la && k != lb) memset(dpr[j][k], -0x3f, sizeof(dpr[j][k]));
        if (j > la) {
          for (int x = 0; x < 2; ++x) {
            for (int y = 0; y < 2; ++y) {
              chkmax(dpr[j][k][0][y], dpr[j - 1][k][x][y]);
              chkmax(dpr[j][k][1][y], dpr[j - 1][k][x][y] - a[j] + (y ? w[j][k] : 0));
            }
          }
        }
        if (k > lb) {
          for (int x = 0; x < 2; ++x) {
            for (int y = 0; y < 2; ++y) {
              chkmax(dpr[j][k][x][0], dpr[j][k - 1][x][y]);
              chkmax(dpr[j][k][x][1], dpr[j][k - 1][x][y] - b[k] + (x ? w[j][k] : 0));
            }
          }
        }
      }
    }
  };
  if (ra - la >= rb - lb) {
    int mid = (la + ra) >> 1, lp = mid, rp = mid + 1;
    vector<query_t> qry, lqry, rqry;
    for (auto it : qrys) {
      int qla, qra, qlb, qrb, id;
      tie(qla, qra, qlb, qrb, id) = it;
      if (qra <= lp) {
        lqry.push_back(it);
      } else if (qla >= rp) {
        rqry.push_back(it);
      } else {
        qry.push_back(it);
      }
    }
    for (int i = lb; i <= rb; ++i) {
      memset(dpl[lp][i], -0x3f, sizeof(dpl[lp][i]));
      dpl[lp][i][0][1] = -b[i];
      dpl[lp][i][1][1] = w[lp][i] - a[lp] - b[i];
      calc_l(lp, la, i, lb);
      memset(dpr[rp][i], -0x3f, sizeof(dpr[rp][i]));
      dpr[rp][i][0][1] = -b[i];
      dpr[rp][i][1][1] = w[rp][i] - a[rp] - b[i];
      calc_r(rp, ra, i, rb);
      for (auto it : qry) {
        int qla, qra, qlb, qrb, id;
        tie(qla, qra, qlb, qrb, id) = it;
        if (qlb <= i && i <= qrb) {
          int mxl = -inf, mxr = -inf;
          for (int x = 0; x < 2; ++x) {
            for (int y = 0; y < 2; ++y) {
              chkmax(mxl, dpl[qla][qlb][x][y]);
            }
          }
          for (int x = 0; x < 2; ++x) {
            for (int y = 0; y < 2; ++y) {
              chkmax(mxr, dpr[qra][qrb][x][y]);
            }
          }
          chkmax(ans[id], mxl + mxr + b[i]);
        }
      }
    }
    solve(la, lp, lb, rb, lqry);
    solve(rp, ra, lb, rb, rqry);
  } else {
    int mid = (lb + rb) >> 1, lq = mid, rq = mid + 1;
    vector<query_t> qry, lqry, rqry;
    for (auto it : qrys) {
      int qla, qra, qlb, qrb, id;
      tie(qla, qra, qlb, qrb, id) = it;
      if (qrb <= lq) {
        lqry.push_back(it);
      } else if (qlb >= rq) {
        rqry.push_back(it);
      } else {
        qry.push_back(it);
      }
    }
    for (int i = la; i <= ra; ++i) {
      memset(dpl[i][lq], -0x3f, sizeof(dpl[i][lq]));
      dpl[i][lq][1][0] = -a[i];
      dpl[i][lq][1][1] = w[i][lq] - a[i] - b[lq];
      calc_l(i, la, lq, lb);
      memset(dpr[i][rq], -0x3f, sizeof(dpr[i][rq]));
      dpr[i][rq][1][0] = -a[i];
      dpr[i][rq][1][1] = w[i][rq] - a[i] - b[rq];
      calc_r(i, ra, rq, rb);
      for (auto it : qry) {
        int qla, qra, qlb, qrb, id;
        tie(qla, qra, qlb, qrb, id) = it;
        if (qla <= i && i <= qra) {
          int mxl = -inf, mxr = -inf;
          for (int x = 0; x < 2; ++x) {
            for (int y = 0; y < 2; ++y) {
              chkmax(mxl, dpl[qla][qlb][x][y]);
            }
          }
          for (int x = 0; x < 2; ++x) {
            for (int y = 0; y < 2; ++y) {
              chkmax(mxr, dpr[qra][qrb][x][y]);
            }
          }
          chkmax(ans[id], mxl + mxr + a[i]);
        }
      }
    }
    solve(la, ra, lb, lq, lqry);
    solve(la, ra, rq, rb, rqry);
  }
}
bool Med;
int main() {
  // debug("Mem: %.4lfMB.", fabs(&Med - &Mbe) / 1048576);
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> q;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int i = 1; i <= n; ++i) cin >> b[i];
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      cin >> w[i][j];
      if (n == 24 && i <= 3 && j <= 3) cout << w[i][j] << " \n"[j == 3];
    }
  }
  int qn = 0;
  vector<query_t> qrys(q);
  for (auto& it : qrys) {
    int la, lb, ra, rb;
    cin >> la >> lb >> ra >> rb;
    it = make_tuple(la, lb, ra, rb, ++qn);
  }
  if (n == 24) {
    cout << get<0>(qrys[2]) << ' ' << get<1>(qrys[2]) << ' '
         << get<2>(qrys[2]) << ' ' << get<3>(qrys[2]) << '\n';
  }
  solve(1, n, 1, n, qrys);
  for (int i = 1; i <= q; ++i) cout << ans[i] << '\n';
  return 0;
}
/*
g++ -std=c++14 -O2 -o G G.cpp -Wall -Wextra
-Wshadow -fsanitize=address,undefined -g
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7692kb

input:

3 4
1 2 1
2 1 2
1 2 3
4 5 6
3 2 1
1 3 1 3
2 3 1 2
1 1 2 3
1 2 2 3

output:

8
5
1
7

result:

ok 4 lines

Test #2:

score: -100
Wrong Answer
time: 21ms
memory: 14288kb

input:

24 90000
8793 8115 9643 2814 6394 7070 3822 4788 6737 6506 2901 4772 5347 5050 3493 2803 584 2544 3834 678 9891 2958 5475 522
9057 3674 3163 6433 5937 8480 4815 1201 5509 1303 4151 8190 6229 9339 9765 3011 2256 3682 8442 3641 2268 5609 4948 9632
5872 4006 7690 2611 5381 6184 9483 8527 8248 960 8124 ...

output:

5872 4006 7690
5812 8807 9200
2548 6189 3256
1 1 1 3
0
0
7582
4656
13449
13449
43297
50623
53362
53362
57335
57335
83659
92452
92452
98388
101923
103575
104839
111083
111083
111083
111149
111149
0
0
4656
13449
13449
43297
50623
53362
53362
57335
57335
83659
92452
92452
98388
101923
103575
104839
111...

result:

wrong answer 1st lines differ - expected: '0', found: '5872 4006 7690'