QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#385172#6846. Wiring EngineeringJCY_WA 21ms14428kbC++145.1kb2024-04-10 16:12:452024-04-10 16:12:46

Judging History

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

  • [2024-04-10 16:12:46]
  • 评测
  • 测评结果:WA
  • 用时:21ms
  • 内存:14428kb
  • [2024-04-10 16:12:45]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using i128 = __int128;
using u128 = unsigned __int128;
template <typename T>
void chkmax(T &x, const T &y) {
  if (x < y) x = y;
}
template <typename T>
void chkmin(T &x, const T &y) {
  if (y < x) x = y;
}
struct node {
  pair<int, int> rg[2];
  int id;
};
constexpr int MAXN = 505, INF = 0x3f3f3f3f;
int n, cst[2][MAXN], val[2][MAXN][MAXN], ans[MAXN];
int dpl[MAXN][MAXN][2][2], dpr[MAXN][MAXN][2][2];
int get_max(int ar[2][2]) { return *max_element(ar[0], ar[0] + 4); }
void solve(int t, int l1, int r1, int l2, int r2, const vector<node> &qry) {
  if (r1 - l1 < r2 - l2) {
    swap(l1, l2);
    swap(r1, r2);
    t ^= 1;
  }
  if (l1 == r1) {
    for (auto &i : qry) {
      int mx = -cst[t][l1];
      for (int j = i.rg[!t].first; j <= i.rg[!t].second; ++j)
        mx += max(val[t][l1][j] - cst[!t][j], 0);
      ans[i.id] = max(mx, 0);
    }
    return;
  }
  int mid = (l1 + r1) >> 1;
  vector<node> qryl, qryr, now;
  for (auto &i : qry) {
    if (i.rg[t].second <= mid) {
      qryl.emplace_back(i);
    } else if (i.rg[t].first > mid) {
      qryr.emplace_back(i);
    } else {
      now.emplace_back(i);
    }
  }
  solve(t, l1, mid, l2, r2, qryl);
  solve(t, mid + 1, r1, l2, r2, qryr);
  if (now.empty()) return;
  for (int s = l2; s <= r2; ++s) {
    if (all_of(now.begin(), now.end(), [&](const node &x) {
          return x.rg[!t].first > s || x.rg[!t].second < s;
        })) {
      continue;
    }
    for (int have = 0; have < 2; ++have) {
      for (int x = l1; x <= mid; ++x)
        for (int y = l2; y <= s; ++y) fill_n(dpl[x][y][0], 4, -INF);
      dpl[mid][s][0][have] = 0;
      dpl[mid][s][1][have] = (have ? val[t][mid][s] : 0) - cst[t][mid];
      for (int x = mid; x >= l1; --x) {
        for (int y = s; y >= l2; --y) {
          for (int tx = 0; tx < 2; ++tx) {
            for (int ty = 0; ty < 2; ++ty) {
              if (dpl[x][y][tx][ty] == -INF) continue;
              if (x != l1) {
                chkmax(dpl[x - 1][y][0][ty], dpl[x][y][tx][ty]);
                chkmax(dpl[x - 1][y][1][ty], dpl[x][y][tx][ty] +
                                                 (ty ? val[t][x - 1][y] : 0) -
                                                 cst[t][x - 1]);
              }
              if (y != l2) {
                chkmax(dpl[x][y - 1][tx][0], dpl[x][y][tx][ty]);
                chkmax(dpl[x][y - 1][tx][1], dpl[x][y][tx][ty] +
                                                 (tx ? val[t][x][y - 1] : 0) -
                                                 cst[!t][y - 1]);
              }
            }
          }
        }
      }
      for (int x = mid + 1; x <= r1; ++x)
        for (int y = s; y <= r2; ++y) fill_n(dpr[x][y][0], 4, -INF);
      dpr[mid + 1][s][0][have] = 0;
      dpr[mid + 1][s][1][have] =
          (have ? val[t][mid + 1][s] : 0) - cst[t][mid + 1];
      for (int x = mid + 1; x <= r1; ++x) {
        for (int y = s; y <= r2; ++y) {
          for (int tx = 0; tx < 2; ++tx) {
            for (int ty = 0; ty < 2; ++ty) {
              if (dpr[x][y][tx][ty] == -INF) continue;
              if (x != r1) {
                chkmax(dpr[x + 1][y][0][ty], dpr[x][y][tx][ty]);
                chkmax(dpr[x + 1][y][1][ty], dpr[x][y][tx][ty] +
                                                 (ty ? val[t][x + 1][y] : 0) -
                                                 cst[t][x + 1]);
              }
              if (y != r2) {
                chkmax(dpr[x][y + 1][tx][0], dpr[x][y][tx][ty]);
                chkmax(dpr[x][y + 1][tx][1], dpr[x][y][tx][ty] +
                                                 (tx ? val[t][x][y + 1] : 0) -
                                                 cst[!t][y + 1]);
              }
            }
          }
        }
      }
      for (auto &i : now) {
        if (i.rg[!t].first <= s && i.rg[!t].second >= s) {
          chkmax(ans[i.id], get_max(dpl[i.rg[t].first][i.rg[!t].first]) +
                                get_max(dpr[i.rg[t].second][i.rg[!t].second]) -
                                (have ? cst[!t][s] : 0));
          if (i.id == 555) {
            cout << i.rg[0].first << " " << i.rg[1].first << " "
                 << i.rg[0].second << " " << i.rg[1].second << " QAQ\n";
          }
        }
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int q;
  cin >> n >> q;
  for (int t = 0; t < 2; ++t)
    for (int i = 1; i <= n; ++i) cin >> cst[t][i];
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      cin >> val[0][i][j];
      val[1][j][i] = val[0][i][j];
    }
  }
  vector<node> qry(q);
  for (int i = 0; i < q; ++i) {
    cin >> qry[i].rg[0].first >> qry[i].rg[0].second >> qry[i].rg[1].first >>
        qry[i].rg[1].second;
    qry[i].id = i;
  }
  solve(0, 1, n, 1, n, qry);
  for (int i = 0; i < q; ++i) cout << ans[i] << "\n";
  return 0;
}
/*
g++ L.cpp -o L -std=c++14 -O2 -Wall -Wextra -Wshadow -g
-fsanitize=address,undefined
*/

详细

Test #1:

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

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: 14428kb

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:

1 16 2 16 QAQ
1 16 2 16 QAQ
0
0
0
0
0
0
734
8060
10799
10799
14772
14772
14772
14772
14772
20708
24243
25895
27159
33403
33403
33403
33469
33469
0
0
0
0
0
734
8060
10799
10799
14772
14772
14772
14772
14772
20708
24243
25895
27159
33403
33403
33403
33469
33469
0
0
0
0
402
7728
10467
10467
14440
14440...

result:

wrong answer 1st lines differ - expected: '0', found: '1 16 2 16 QAQ'