QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#393201#8553. Exchanging Kubichos_lyricAC ✓77ms28436kbC++146.1kb2024-04-18 11:55:082024-04-18 11:55:08

Judging History

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

  • [2024-04-18 11:55:08]
  • 评测
  • 测评结果:AC
  • 用时:77ms
  • 内存:28436kb
  • [2024-04-18 11:55:08]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


#ifdef LOCAL
// #define DEBUG
#endif

int N;

#ifdef DEBUG
vector<vector<Int>> calc(const vector<Int> &as) {
  assert((int)as.size() == N);
  vector<vector<Int>> dp(N + 1, vector<Int>(N + 1, 0));
  for (int l = 0; l < N; ++l) {
    for (int i = l; i < N; ++i) {
      dp[l][i + 1] = dp[l][i] + as[i];
    }
  }
  for (int l = N; --l >= 0; ) {
    for (int r = l + 1; r <= N; ++r) {
      chmax(dp[l][r], dp[l + 1][r]);
      chmax(dp[l][r], dp[l][r - 1]);
    }
  }
  return dp;
}

mt19937_64 rng;
vector<Int> secret;
vector<vector<Int>> secretDP;
int Q;

void gen() {
  N = 1 + rng() % 10;
  secret.resize(N);
  for (int i = 0; i < N; ++i) {
    secret[i] = -10 + rng() % 21;
  }
  secretDP = calc(secret);
  Q = 0;
}
#endif

Int ask(int l, int r) {
  assert(0 <= l); assert(l < r); assert(r <= N);
#ifdef DEBUG
  ++Q;
  return secretDP[l][r];
#else
  printf("? %d %d\n", l + 1, r);
  fflush(stdout);
  Int ret;
  scanf("%lld", &ret);
  return ret;
#endif
}

vector<Int> rec(const vector<int> &xs, const vector<Int> &as) {
// cerr<<"[rec] "<<xs<<" "<<as<<endl;
  const int len = as.size();
  assert(len >= 1);
  assert((int)xs.size() == len + 1);
  if (len == 1) {
    return as;
  }
  auto ys = xs;
  auto bs = as;
  auto merge = [&](int l, int r) -> void {
    ys.erase(ys.begin() + (l + 1), ys.begin() + r);
    bs.erase(bs.begin() + (l + 1), bs.begin() + r);
  };
  vector<Int> ret;
  auto split = [&](int l, int r) -> void {
    ret = rec(ys, bs);
    ret.insert(ret.begin() + (l + 1), r - (l + 1), 0);
  };
// #define re cerr<<"[rec:"<<__LINE__<<"] "<<xs<<" "<<as<<": "<<ret<<endl; return ret
#define re return ret
  // merge ++
  for (int i = 1; i < len; ++i) if (as[i - 1] > 0 && as[i] > 0) {
    merge(i - 1, i + 1);
    bs[i - 1] = as[i - 1] + as[i];
    split(i - 1, i + 1);
    ret[i - 1] = as[i - 1];
    ret[i] = as[i];
    re;
  }
  // merge --
  for (int i = 1; i < len; ++i) if (as[i - 1] == 0 && as[i] == 0) {
    merge(i - 1, i + 1);
    bs[i - 1] = 0;
    split(i - 1, i + 1);
    re;
  }
  // remove [-
  if (as[0] == 0) {
    ys.erase(ys.begin());
    bs.erase(bs.begin());
    ret = rec(ys, bs); ret.insert(ret.begin(), 0);
    re;
  }
  // remove -]
  if (as[len - 1] == 0) {
    ys = xs; ys.pop_back();
    bs = as; bs.pop_back();
    ret = rec(ys, bs); ret.push_back(0);
    re;
  }
  // min +
  int im = -1;
  for (int i = 0; i < len; i += 2) {
    assert(as[i] > 0);
    if (!~im || as[im] > as[i]) im = i;
  }
  if (im == 0) {
    // merge [+-+ or remove [+-
    merge(0, 3);
    // Q += 1, len -= 2
    bs[0] = ask(xs[0], xs[3]);
    split(0, 3);
    ret[0] = as[0];
    ret[1] = bs[0] - (as[0] + as[2]);
    ret[2] = as[2];
    re;
  }
  if (im == len - 1) {
    // merge [+-+ or remove [+-
    merge(len - 3, len);
    // Q += 1, len -= 2
    bs[len - 3] = ask(xs[len - 3], xs[len]);
    split(len - 3, len);
    ret[len - 3] = as[len - 3];
    ret[len - 2] = bs[len - 3] - (as[len - 3] + as[len - 1]);
    ret[len - 1] = as[len - 1];
    re;
  }
  // merge +-+ or +-+-+
  // Q += 2, len -= 2
  const Int bL = ask(xs[im - 2], xs[im + 1]);
  const Int bR = ask(xs[im], xs[im + 3]);
  if (bL > as[im - 2] && bL > as[im]) {
    merge(im - 2, im + 1);
    bs[im - 2] = bL;
    split(im - 2, im + 1);
    ret[im - 2] = as[im - 2];
    ret[im - 1] = bL - (as[im - 2] + as[im]);
    ret[im    ] = as[im];
    re;
  }
  if (bR > as[im] && bR > as[im + 2]) {
    merge(im, im + 3);
    bs[im] = bR;
    split(im, im + 3);
    ret[im    ] = as[im];
    ret[im + 1] = bR - (as[im] + as[im + 2]);
    ret[im + 2] = as[im + 2];
    re;
  }
  merge(im - 1, im + 2);
  bs[im - 1] = 0;
  split(im - 1, im + 2);
  ret[im    ] = as[im];
  ret[im + 1] = -as[im];
  re;
#undef re
}

vector<Int> solve() {
  vector<int> xs(N + 1);
  for (int i = 0; i <= N; ++i) {
    xs[i] = i;
  }
  vector<Int> as(N);
  for (int i = 0; i < N; ++i) {
    as[i] = ask(i, i + 1);
  }
  return rec(xs, as);
}

int main() {
#ifdef DEBUG
  for (; ; ) {
    gen();
    const auto ans = solve();
    cerr << COLOR("33") << secret << " " << ans << " " << Q << COLOR() << endl;
    const auto ansDP = calc(ans);
    for (int w = 0; w <= N; ++w) {
      for (int l = 0, r; (r = l + w) <= N; ++l) {
        if (secretDP[l][r] != ansDP[l][r]) {
          cerr << COLOR("91") << "FAIL [" << l << ", " << r << "): " << secretDP[l][r] << " " << ansDP[l][r] << COLOR() << endl;
          assert(false);
        }
      }
    }
    assert(Q <= 2 * N);
  }
#endif
  
  int numCases;
  scanf("%d", &numCases);
  for (int caseId = 1; caseId <= numCases; ++caseId) {
    scanf("%d", &N);
    const auto ans = solve();
    printf("!");
    for (int i = 0; i < N; ++i) {
      printf(" %lld", ans[i]);
    }
    puts("");
    fflush(stdout);
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
3
1
0
1
1
5
2
0
3
0
5
4
5

output:

? 1 1
? 2 2
? 3 3
? 1 3
! 1 -1 1
? 1 1
? 2 2
? 3 3
? 4 4
? 5 5
? 1 3
? 1 5
! 2 -1 3 -4 5

result:

ok ok (2 test cases)

Test #2:

score: 0
Accepted
time: 22ms
memory: 3836kb

input:

10000
1
718876398
1
0
1
0
1
0
1
938356223
1
857157125
1
0
1
0
1
0
1
0
1
0
1
965894497
1
0
1
626061677
1
642094813
1
107398046
1
0
1
0
1
0
1
287188651
1
0
1
345108193
1
0
1
0
1
714952783
1
0
1
325760098
1
0
1
800487422
1
322806679
1
0
1
0
1
0
1
866952779
1
741483102
1
492063609
1
0
1
833448280
1
0
1
...

output:

? 1 1
! 718876398
? 1 1
! 0
? 1 1
! 0
? 1 1
! 0
? 1 1
! 938356223
? 1 1
! 857157125
? 1 1
! 0
? 1 1
! 0
? 1 1
! 0
? 1 1
! 0
? 1 1
! 0
? 1 1
! 965894497
? 1 1
! 0
? 1 1
! 626061677
? 1 1
! 642094813
? 1 1
! 107398046
? 1 1
! 0
? 1 1
! 0
? 1 1
! 0
? 1 1
! 287188651
? 1 1
! 0
? 1 1
! 345108193
? 1 1
! ...

result:

ok ok (10000 test cases)

Test #3:

score: 0
Accepted
time: 6ms
memory: 4112kb

input:

1052
9
167536100
0
373372185
0
427949326
442758705
102715118
0
0
373372185
973423149
9
248442934
306962195
570791475
593033322
0
582850731
559429390
0
120053133
1142280121
2780526396
10
785691778
0
981032824
0
0
592503870
0
0
0
0
1112480382
1112480382
10
0
737563509
985502704
427600980
0
805973591
7...

output:

? 1 1
? 2 2
? 3 3
? 4 4
? 5 5
? 6 6
? 7 7
? 8 8
? 9 9
? 1 3
? 1 7
! 167536100 -167536100 373372185 -373372185 427949326 442758705 102715118 0 0
? 1 1
? 2 2
? 3 3
? 4 4
? 5 5
? 6 6
? 7 7
? 8 8
? 9 9
? 6 9
? 1 9
! 248442934 306962195 570791475 593033322 -80983651 582850731 559429390 -120053133 1200531...

result:

ok ok (1052 test cases)

Test #4:

score: 0
Accepted
time: 8ms
memory: 4204kb

input:

105
98
0
622130364
0
603542943
491665548
0
535594695
169182905
269002770
437838930
534070706
783210752
0
914335037
560159875
0
216552904
666995724
0
0
0
753649796
0
0
0
779352417
0
121063647
0
496743470
0
4104890
0
648149367
0
965790695
732089017
0
0
0
0
6701195
0
0
0
0
750231085
0
0
511641740
36964...

output:

? 1 1
? 2 2
? 3 3
? 4 4
? 5 5
? 6 6
? 7 7
? 8 8
? 9 9
? 10 10
? 11 11
? 12 12
? 13 13
? 14 14
? 15 15
? 16 16
? 17 17
? 18 18
? 19 19
? 20 20
? 21 21
? 22 22
? 23 23
? 24 24
? 25 25
? 26 26
? 27 27
? 28 28
? 29 29
? 30 30
? 31 31
? 32 32
? 33 33
? 34 34
? 35 35
? 36 36
? 37 37
? 38 38
? 39 39
? 40 4...

result:

ok ok (105 test cases)

Test #5:

score: 0
Accepted
time: 20ms
memory: 8764kb

input:

10
911
0
0
318490282
367703800
0
447141340
683983129
0
319014522
385248618
0
0
0
453686281
0
0
503449442
0
451161866
0
422033116
892391115
0
0
0
0
0
0
116949378
0
305018897
441460055
390798833
643328028
0
0
785497871
0
0
0
0
702685168
0
177748037
150437291
920782161
719254975
0
519494673
555035366
0...

output:

? 1 1
? 2 2
? 3 3
? 4 4
? 5 5
? 6 6
? 7 7
? 8 8
? 9 9
? 10 10
? 11 11
? 12 12
? 13 13
? 14 14
? 15 15
? 16 16
? 17 17
? 18 18
? 19 19
? 20 20
? 21 21
? 22 22
? 23 23
? 24 24
? 25 25
? 26 26
? 27 27
? 28 28
? 29 29
? 30 30
? 31 31
? 32 32
? 33 33
? 34 34
? 35 35
? 36 36
? 37 37
? 38 38
? 39 39
? 40 4...

result:

ok ok (10 test cases)

Test #6:

score: 0
Accepted
time: 41ms
memory: 25088kb

input:

5
1952
0
207059033
0
846665449
247683562
650126777
348616690
570194875
727419904
504212241
0
0
491140658
0
876684021
0
8207113
0
0
0
0
0
0
325502144
0
78879155
0
828506541
357830073
0
831431906
864603826
0
0
656125431
582335892
651256373
0
640787570
0
103421615
0
0
0
0
0
481682299
493673601
93577949...

output:

? 1 1
? 2 2
? 3 3
? 4 4
? 5 5
? 6 6
? 7 7
? 8 8
? 9 9
? 10 10
? 11 11
? 12 12
? 13 13
? 14 14
? 15 15
? 16 16
? 17 17
? 18 18
? 19 19
? 20 20
? 21 21
? 22 22
? 23 23
? 24 24
? 25 25
? 26 26
? 27 27
? 28 28
? 29 29
? 30 30
? 31 31
? 32 32
? 33 33
? 34 34
? 35 35
? 36 36
? 37 37
? 38 38
? 39 39
? 40 4...

result:

ok ok (5 test cases)

Test #7:

score: 0
Accepted
time: 77ms
memory: 28436kb

input:

5
2000
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0...

output:

? 1 1
? 2 2
? 3 3
? 4 4
? 5 5
? 6 6
? 7 7
? 8 8
? 9 9
? 10 10
? 11 11
? 12 12
? 13 13
? 14 14
? 15 15
? 16 16
? 17 17
? 18 18
? 19 19
? 20 20
? 21 21
? 22 22
? 23 23
? 24 24
? 25 25
? 26 26
? 27 27
? 28 28
? 29 29
? 30 30
? 31 31
? 32 32
? 33 33
? 34 34
? 35 35
? 36 36
? 37 37
? 38 38
? 39 39
? 40 4...

result:

ok ok (5 test cases)

Test #8:

score: 0
Accepted
time: 17ms
memory: 3812kb

input:

1052
9
911133045
0
526984535
0
931320524
0
928006811
0
176872302
928006811
1331374524
931320524
1340686664
1808307998
9
916585987
0
423565782
0
615681673
0
935399347
0
661248049
1275831339
615681673
1377399889
1517066200
935399347
2278784416
10
587217915
0
129148010
0
316225345
0
935126806
0
6419014...

output:

? 1 1
? 2 2
? 3 3
? 4 4
? 5 5
? 6 6
? 7 7
? 8 8
? 9 9
? 7 9
? 1 3
? 3 5
? 5 9
? 1 9
! 911133045 -106743056 526984535 -863753190 931320524 -518640671 928006811 -176872302 176872302
? 1 1
? 2 2
? 3 3
? 4 4
? 5 5
? 6 6
? 7 7
? 8 8
? 9 9
? 1 3
? 3 5
? 1 5
? 5 7
? 7 9
? 1 9
! 916585987 -64320430 42356578...

result:

ok ok (1052 test cases)

Test #9:

score: 0
Accepted
time: 24ms
memory: 3876kb

input:

105
98
488529703
0
468492922
0
802901522
0
475641005
0
635144928
0
319324280
0
312095441
0
637285947
0
863514749
0
567178596
0
785897142
0
706104708
0
913552861
0
580235462
0
86293259
0
945400504
0
549373433
0
807677852
0
115856051
0
376484061
0
984127422
0
202884700
0
516705542
0
286842060
0
517315...

output:

? 1 1
? 2 2
? 3 3
? 4 4
? 5 5
? 6 6
? 7 7
? 8 8
? 9 9
? 10 10
? 11 11
? 12 12
? 13 13
? 14 14
? 15 15
? 16 16
? 17 17
? 18 18
? 19 19
? 20 20
? 21 21
? 22 22
? 23 23
? 24 24
? 25 25
? 26 26
? 27 27
? 28 28
? 29 29
? 30 30
? 31 31
? 32 32
? 33 33
? 34 34
? 35 35
? 36 36
? 37 37
? 38 38
? 39 39
? 40 4...

result:

ok ok (105 test cases)

Test #10:

score: 0
Accepted
time: 35ms
memory: 6700kb

input:

10
911
190008868
0
267501671
0
615470163
0
133513401
0
314315377
0
660548519
0
555314918
0
941289689
0
231059371
0
56848945
0
294509086
0
423676574
0
947717893
0
631853488
0
420480249
0
447345907
0
368504182
0
757380250
0
498988264
0
155354017
0
832730679
0
982153581
0
779911649
0
228619121
0
524075...

output:

? 1 1
? 2 2
? 3 3
? 4 4
? 5 5
? 6 6
? 7 7
? 8 8
? 9 9
? 10 10
? 11 11
? 12 12
? 13 13
? 14 14
? 15 15
? 16 16
? 17 17
? 18 18
? 19 19
? 20 20
? 21 21
? 22 22
? 23 23
? 24 24
? 25 25
? 26 26
? 27 27
? 28 28
? 29 29
? 30 30
? 31 31
? 32 32
? 33 33
? 34 34
? 35 35
? 36 36
? 37 37
? 38 38
? 39 39
? 40 4...

result:

ok ok (10 test cases)

Test #11:

score: 0
Accepted
time: 41ms
memory: 15860kb

input:

5
1952
270943944
0
427827622
0
706467567
0
657378898
0
336632977
0
646373280
0
330175227
0
636169757
0
614552419
0
752377204
0
964998361
0
223064174
0
402544250
0
855778411
0
374152994
0
278416329
0
988879952
0
151167996
0
775855140
0
366009963
0
63772275
0
189944036
0
123776551
0
636001304
0
860458...

output:

? 1 1
? 2 2
? 3 3
? 4 4
? 5 5
? 6 6
? 7 7
? 8 8
? 9 9
? 10 10
? 11 11
? 12 12
? 13 13
? 14 14
? 15 15
? 16 16
? 17 17
? 18 18
? 19 19
? 20 20
? 21 21
? 22 22
? 23 23
? 24 24
? 25 25
? 26 26
? 27 27
? 28 28
? 29 29
? 30 30
? 31 31
? 32 32
? 33 33
? 34 34
? 35 35
? 36 36
? 37 37
? 38 38
? 39 39
? 40 4...

result:

ok ok (5 test cases)

Extra Test:

score: 0
Extra Test Passed