QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#47039#4377. BackpackAsukaKyle#TL 0ms0kbC++2.0kb2022-09-03 15:47:062022-09-03 15:47:09

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-09-03 15:47:09]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2022-09-03 15:47:06]
  • 提交

answer

// Author:  HolyK
// Created: Sat Sep  3 14:56:14 2022
#include <bits/stdc++.h>

template <class T, class U>
inline bool smin(T &x, const U &y) {
  return y < x ? x = y, 1 : 0;
}
template <class T, class U>
inline bool smax(T &x, const U &y) {
  return x < y ? x = y, 1 : 0;
}

using LL = long long;
using PII = std::pair<int, int>;

struct Node {
  int ch[2], tag;
} t[4 << 20];
int cnt;
void apply(int &o, int x) {
  if (!o) return;
  t[++cnt] = t[o], o = cnt;
  t[o].tag ^= x;
}
void pushdown(int o, int d) {
  if (t[o].tag) {
    apply(t[o].ch[0], t[o].tag);
    apply(t[o].ch[1], t[o].tag);
    if(t[o].tag >> d & 1) std::swap(t[o].ch[0], t[o].ch[1]);
    t[o].tag = 0;
  }
}

int merge(int x, int y, int d, int w) {
  if (!x) {
    apply(y, w);
    return y;
  }
  if (!y) return x;
  if (d == -1) return x;
  pushdown(x, d), pushdown(y, d);
  t[x].ch[0] = merge(t[x].ch[0], t[y].ch[w >> d & 1], d - 1, w);
  t[x].ch[1] = merge(t[x].ch[1], t[y].ch[(w >> d & 1) ^ 1], d - 1, w);
  return x;
}

void ins(int &o, int x, int d) {
  t[++cnt] = t[o], o = cnt;
  if (d == -1) return;
  ins(t[o].ch[x >> d & 1], x, d - 1);
}

void solve() {
  int n, m;
  std::cin >> n >> m;
  cnt = 0;
  std::vector<int> f(m + 1);
  ins(f[0], 0, 9);
  while (n--) {
    int v, w;
    std::cin >> v >> w;
    for (int i = m; i >= v; i--) {
      int x = f[i], y = f[i - v];
      if (!y) continue;
      // t[++cnt] = t[x];
      // x = cnt;
      f[i] = merge(x, y, 9, w);
    }
  }

  int o = f[m];
  if (!o) {
    std::cout << "-1\n";
    return;
  }

  int ans = 0;
  for (int i = 9; i >= 0; i--) {
    pushdown(o, i);
    
    if (t[o].ch[1]) {
      ans |= 1 << i;
      o = t[o].ch[1];
    } else {
      o = t[o].ch[0];
    }
  }

  std::cout << ans << "\n";
}

int main() {
  // freopen("t.in", "r", stdin);
  
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int t = 1;
  
  std::cin >> t;
  
  while (t--) {
    solve();
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

10
1023 401
179 441
416 951
420 984
1013 984
683 914
407 984
96 523
374 190
974 190
739 441
518 523
194 984
415 523
149 441
235 984
809 441
469 441
436 919
437 919
7 919
818 984
962 190
37 919
371 523
417 914
431 914
213 190
340 441
254 919
223 951
123 190
339 951
322 441
218 441
284 919
533 190
187...

output:


result: