【模板】交互题

Codeforces的交互题有点难以调试,写了一个模板方便本地调试。

struct Oracle {

  private:
    static const int MAXN = 2e5 + 10;

    int n;
    ll a[MAXN];
    ll query_cost;

#ifdef LOCAL
    static const bool IS_LOCAL = true;
#else
    static const bool IS_LOCAL = false;
#endif // LOCAL


  public:
    int GetN() {
        RD (n);
        query_cost = 0;
        if (IS_LOCAL) {
            RDN (a, n);
//        WT (n);
//        WTN (a, n);
        }
        return n;
    }

    ll Query (int l, int r) {
        printf ("? %d %d\n", l, r);
        fflush (stdout);
        query_cost += (r - l) * (r - l);

        ll res;
        if (IS_LOCAL) {
            res = GetRes (l, r);
        } else {
            RD (res);
        }
        return res;
    }

    void Answer (ll x) {
        printf ("! %lld\n", x);
        fflush (stdout);

        if (IS_LOCAL) {
            ll query_cost_lim = 5 * n * n;
            D2 (query_cost, query_cost_lim);
            ASSERT (query_cost <= query_cost_lim);
            ASSERT (CheckAnswer (x));
        }
    }

  private:
    ll GetRes (int l, int r) {
        ll res = 0;
        for (int i = l; i <= r; ++i) {
            for (int j = i + 1; j <= r; ++j) {
                res += (a[i] > a[j]);
            }
        }
        return res;
    }

    bool CheckAnswer (int x) {
        return (max_element (a + 1, a + 1 + n) - a) == x;
    }

} oracle;
posted @ 2024-02-26 14:19  purinliang  阅读(16)  评论(0编辑  收藏  举报