QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#539128#8939. Permutationucup-team008#RE 77ms8900kbC++176.3kb2024-08-31 14:04:192024-08-31 14:04:20

Judging History

This is the latest submission verdict.

  • [2024-08-31 14:04:20]
  • Judged
  • Verdict: RE
  • Time: 77ms
  • Memory: 8900kb
  • [2024-08-31 14:04:19]
  • Submitted

answer

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>

using namespace std;

// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
#define derr if(0) cerr
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << flush;
// END NO SAD

template<class Fun>
class y_combinator_result {
  Fun fun_;
public:
  template<class T>
  explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}

  template<class ...Args>
  decltype(auto) operator()(Args &&...args) {
    return fun_(std::ref(*this), std::forward<Args>(args)...);
  }
};

template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
  return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}

template<class T>
bool updmin(T& a, T b) {
  if(b < a) {
    a = b;
    return true;
  }
  return false;
}
template<class T>
bool updmax(T& a, T b) {
  if(b > a) {
    a = b;
    return true;
  }
  return false;
}
typedef int64_t ll;

int full[1000001]; // assumes no info
int expensive[1000001]; // assumes has info
void init() {
  full[2] = 1;
  expensive[2] = 1;
  for(int i = 3; i <= 1000000; i++) {
    // 1 to figure out the side, ceil(log to figure out the rest)
    expensive[i] = 1 + ceil(log2(i-1));
  }
}
void rsolve() {
  int n;
  cin >> n;
  int qcnt = ceil(log2(n) * 1.5);
  map<array<int, 2>, int> dp;
  auto qry = [&](int lhs, int rhs) -> int { 
    assert(lhs < rhs);
    array<int, 2> key = {lhs, rhs};
    if(dp.count(key)) return dp[key];
    cout << "? " << lhs << " " << rhs << endl;
    int ret;
    cin >> ret;
    dp[key] = ret;
    qcnt--;
    return ret;
  };
  auto dfs = y_combinator([&](auto self, int lhs, int rhs, int secondlargest) -> int {
    if(lhs == rhs) return lhs;
    assert(rhs > lhs);
    if(rhs-lhs == 1) {
      if(secondlargest >= 0) return rhs + lhs - secondlargest;
      return rhs + lhs - qry(lhs, rhs);
    }
    if(secondlargest == -1) secondlargest = qry(lhs, rhs);
    assert(lhs <= secondlargest && secondlargest <= rhs);
    int nxtsz = (rhs-lhs+1) - (rhs-lhs+1)/2;
    if(rhs-lhs + 1 < n && max(expensive[nxtsz] + 1, expensive[(rhs-lhs+1)-nxtsz] + 2) > qcnt) {
      // go hard
      bool leftside = false;
      bool confirm = false; // true only if must be
      if(lhs == secondlargest) {
        confirm = true;
      }
      else if(rhs == secondlargest) {
        leftside = true;
        confirm = true;
      }
      else if(lhs+1 == secondlargest);
      else if(rhs-1 == secondlargest) {
        leftside = true;
      }
      else {
        confirm = true;
        if(secondlargest - lhs < rhs - secondlargest) {
          if(qry(lhs, secondlargest) == secondlargest) leftside = true;
        }
        else {
          if(qry(secondlargest, rhs) != secondlargest) leftside = true;
        }
      }
      if(leftside) {
        // check [lhs, secondlargest]
        int ans = -1;
        if(confirm) {
          ans = lhs++;
        }
        rhs = secondlargest-1;
        while(lhs <= rhs) {
          int mid = (lhs+rhs)/2;
          if(qry(mid, secondlargest) == secondlargest) {
            ans = mid;
            lhs = mid+1;
          }
          else rhs = mid-1;
        }
        if(ans == -1) return secondlargest+1;
        return ans;
      }
      else {
        // check [secondlargest, rhs]
        int ans = -1;
        if(confirm) {
          ans = rhs--;
        }
        lhs = secondlargest+1;
        while(lhs <= rhs) {
          int mid = (lhs+rhs)/2;
          if(qry(secondlargest, mid) == secondlargest) {
            ans = mid;
            rhs = mid-1;
          }
          else lhs = mid+1;
        }
        if(ans == -1) return secondlargest-1;
        return ans;
      }
    }
    else {
      int mid = (lhs+rhs)/2;
      if(secondlargest <= mid) {
        if(lhs < mid && qry(lhs, mid) == secondlargest) return self(lhs, mid, secondlargest);
        else return self(mid+1, rhs, -1);
      }
      else {
        mid = (lhs+rhs-1)/2;
        assert(mid < secondlargest && secondlargest <= rhs);
        if(mid+1 < rhs && qry(mid+1, rhs) == secondlargest) return self(mid+1, rhs, secondlargest);
        else return self(lhs, mid, -1);
      }
    }
  });
  int ret = dfs(1, n, -1);
  cout << "! " << ret << endl;
}
void solve() {
  init();
  int t;
  cin >> t;
  while(t--) rsolve();
}

// what would chika do
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
// are you doing geometry in floating points
// are you not using modint when you should

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 11ms
memory: 8272kb

input:

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

output:

? 1 5
? 1 3
? 4 5
! 4
? 1 6
? 4 6
? 1 3
? 2 3
! 2
? 1 4
? 3 4
! 4

result:

ok Correct (3 test cases)

Test #2:

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

input:

10000
10
2
2
3
5
10
10
10
8
7
10
5
1
10
9
10
10
4
4
4
4
10
10
6
3
5
2
10
3
3
3
2
10
1
5
9
9
9
10
1
3
8
10
8
10
2
4
9
8
8
10
3
3
1
5
10
4
1
7
8
7
10
8
7
1
1
2
10
4
1
9
8
8
10
7
8
2
4
2
10
5
1
7
7
8
10
8
8
6
9
10
2
1
7
8
8
10
6
6
8
10
10
1
3
8
9
8
10
7
9
5
5
4
10
7
8
4
4
4
10
3
4
7
7
8
10
4
4
4
3
10
8...

output:

? 1 10
? 1 5
? 1 3
? 4 5
! 4
? 1 10
? 6 10
? 8 10
? 6 7
! 6
? 1 10
? 1 5
? 6 10
? 8 10
? 7 10
! 7
? 1 10
? 1 5
? 3 5
? 3 4
! 3
? 1 10
? 6 10
? 1 5
? 3 5
? 2 3
! 1
? 1 10
? 1 5
? 1 3
? 2 3
! 1
? 1 10
? 1 5
? 6 10
? 7 9
? 8 9
! 8
? 1 10
? 1 5
? 6 10
? 8 10
? 7 8
! 7
? 1 10
? 1 5
? 6 10
? 7 9
? 6 9
! 1...

result:

ok Correct (10000 test cases)

Test #3:

score: -100
Runtime Error

input:

10000
3
1
2
11
5
5
5
4
2
2
19
3
3
4
6
8
9
7
5
7
1
2
3
3
3
19
6
6
10
1
1
2
2
2
15
11
11
11
11
10
14
1
1
1
2
3
16
4
4
1
4
5
3
3
2
19
13
17
5
6
5
4
2
2
4
1
2
3
7
2
2
2
3
2
2
17
1
1
1
2
4
14
9
9
9
8
11
20
9
3
18
19
17
18

output:

? 1 3
? 1 2
! 3
? 1 11
? 1 6
? 4 6
? 4 5
! 6
? 1 2
! 1
? 1 19
? 1 10
? 1 5
? 6 10
? 6 8
? 9 10
! 10
? 1 7
? 4 7
? 1 3
? 1 2
! 3
? 1 3
? 2 3
! 2
? 1 19
? 1 10
? 6 10
? 1 5
? 1 3
? 1 2
! 3
? 1 2
! 1
? 1 15
? 8 15
? 8 11
? 9 11
? 10 11
! 9
? 1 14
? 1 7
? 1 4
? 1 2
? 3 4
! 4
? 1 16
? 1 8
? 1 4
? 4 6
? 4...

result: