QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#795880#9804. Guess the Polygonucup-team4435#RE 0ms3612kbC++236.2kb2024-12-01 03:14:462024-12-01 03:14:46

Judging History

This is the latest submission verdict.

  • [2024-12-01 03:14:46]
  • Judged
  • Verdict: RE
  • Time: 0ms
  • Memory: 3612kb
  • [2024-12-01 03:14:46]
  • Submitted

answer

#include "bits/stdc++.h"


#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
#define rep1n(i, n) for (int i = 1; i <= (n); ++i)
#define repr(i, n) for (int i = (n) - 1; i >= 0; --i)
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define each(x, a) for (auto &x : a)
#define ar array
#define vec vector
#define range(i, n) rep(i, n)

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;

using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vi>;

int Bit(int mask, int b) { return (mask >> b) & 1; }

template<class T>
bool ckmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool ckmax(T &a, const T &b) {
    if (b > a) {
        a = b;
        return true;
    }
    return false;
}

// [l, r)
template<typename T, typename F>
T FindFirstTrue(T l, T r, const F &predicat) {
    --l;
    while (r - l > 1) {
        T mid = l + (r - l) / 2;
        if (predicat(mid)) {
            r = mid;
        } else {
            l = mid;
        }
    }
    return r;
}


template<typename T, typename F>
T FindLastFalse(T l, T r, const F &predicat) {
    return FindFirstTrue(l, r, predicat) - 1;
}

const int INFi = 2e9;
const ll INF = 2e18;

template<class T>
class Frac {
public:
    T num;
    T den;
    Frac(T num, T den) : num(num), den(den) {
        if (den < 0) {
            den = -den;
            num = -num;
        }
    }
    Frac() : Frac(0, 1) {}
    Frac(T num) : Frac(num, 1) {}
    double toDouble() const {
        return 1.0 * num / den;
    }
    void normalize() {
        assert(den > 0);
        ll g = abs(gcd(num, den));
        num /= g;
        den /= g;
    }

    Frac &operator+=(const Frac &rhs) {
        num = num * rhs.den + rhs.num * den;
        den *= rhs.den;
        return *this;
    }
    Frac &operator-=(const Frac &rhs) {
        num = num * rhs.den - rhs.num * den;
        den *= rhs.den;
        return *this;
    }
    Frac &operator*=(const Frac &rhs) {
        num *= rhs.num;
        den *= rhs.den;
        return *this;
    }
    Frac &operator/=(const Frac &rhs) {
        num *= rhs.den;
        den *= rhs.num;
        if (den < 0) {
            num = -num;
            den = -den;
        }
        return *this;
    }
    friend Frac operator+(Frac lhs, const Frac &rhs) {
        return lhs += rhs;
    }
    friend Frac operator-(Frac lhs, const Frac &rhs) {
        return lhs -= rhs;
    }
    friend Frac operator*(Frac lhs, const Frac &rhs) {
        return lhs *= rhs;
    }
    friend Frac operator/(Frac lhs, const Frac &rhs) {
        return lhs /= rhs;
    }
    friend Frac operator-(const Frac &a) {
        return Frac(-a.num, a.den);
    }
    friend bool operator==(const Frac &lhs, const Frac &rhs) {
        return lhs.num * rhs.den == rhs.num * lhs.den;
    }
    friend bool operator!=(const Frac &lhs, const Frac &rhs) {
        return lhs.num * rhs.den != rhs.num * lhs.den;
    }
    friend bool operator<(const Frac &lhs, const Frac &rhs) {
        return lhs.num * rhs.den < rhs.num * lhs.den;
    }
    friend bool operator>(const Frac &lhs, const Frac &rhs) {
        return lhs.num * rhs.den > rhs.num * lhs.den;
    }
    friend bool operator<=(const Frac &lhs, const Frac &rhs) {
        return lhs.num * rhs.den <= rhs.num * lhs.den;
    }
    friend bool operator>=(const Frac &lhs, const Frac &rhs) {
        return lhs.num * rhs.den >= rhs.num * lhs.den;
    }
};

using Fraction = Frac<ll>;

Fraction ask(ll p, ll q) {
    cout << "? " << p << " " << q << endl;
    ll r, s; cin >> r >> s;
    return {r, s};
}

void solve() {
    map<int, int> cnt;
    int n; cin >> n;
    rep(_, n) {
        int x, y; cin >> x >> y;
        cnt[x]++;
    }

    vector<pi> a(all(cnt));
    Fraction result;

    auto Add = [&] (Fraction len1, Fraction len2, Fraction xdiff, Fraction ladd, Fraction radd) {
        Fraction L = len1;
        if (ladd.num != 0) {
            auto df = len1 - len2;
            df.normalize();
            df /= xdiff;
            df.normalize();
            df *= (xdiff + ladd);
            df.normalize();
            L = df + len2;
            L.normalize();
        }
        Fraction R = len2;
        if (radd.num != 0) {
            auto df = len2 - len1;
            df.normalize();
            df /= xdiff;
            df.normalize();
            df *= (xdiff + radd);
            df.normalize();
            R = df + len1;
            R.normalize();
        }
        auto h = xdiff + ladd + radd;
        h /= 2;
        h.normalize();
        auto res = (L + R) * h;
        res.normalize();
        result += res;
        result.normalize();
    };


    Fraction len1 = {0, 1};
    Fraction ladd = 0;

    Fraction prv = a[0].first;

    for(int i = 1; i + 1 < a.size(); ++i) {
        if (a[i].second == 1) {
            auto cur = ask(a[i].first, 1);

            Add(len1, cur, a[i].first - prv, ladd, 0);

            prv = a[i].first;
            ladd = 0;
            len1 = cur;
            continue;
        }
        Fraction radd = {1, 3};
        Fraction rp = a[i].first - radd;
        rp.normalize();

        Fraction len2 = ask(rp.num, rp.den);

        Fraction xdiff = rp - prv;
        Add(len1, len2, xdiff, ladd, radd);


        prv = a[i].first + radd;
        ladd = radd;
        ladd.normalize();
        prv.normalize();
        len1 = ask(prv.num, prv.den);
    }
    assert(a.size() >= 2);
    {
        Fraction cur = {0, 1};
        Add(len1, cur, a.back().first - prv, ladd, 0);
    }
    cout << "! " << result.num << ' ' << result.den << endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(12) << fixed;
    int t = 1;
    cin >> t;

    rep(i, t) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3612kb

input:

2
4
3 0
1 3
1 1
0 0
4 3
5 3
3
0 0
999 1000
1000 999
1999 1000

output:

? 2 3
? 4 3
! 3 1
? 999 1
! 1999 2

result:

ok correct! (2 test cases)

Test #2:

score: -100
Runtime Error

input:

9
4
1 1
1 3
3 0
0 0
2 1
5 6
4
0 0
1 3
1 1
3 0
2 3
5 2
4
0 0
3 0
1 2
1 1
2 3
5 6
4
0 0
3 0
1 2
1 1
4 3
5 6
4
0 0
3 0
1 1
1 2
2 3
5 3
3
1000 0
0 0
0 1000

output:

? 2 3
? 4 3
! 5 2
? 2 3
? 4 3
! 7 2
? 2 3
? 4 3
! 3 2
? 2 3
? 4 3
! 2 1
? 2 3
? 4 3
! 5 2
! 0 1

result: