Submission #1326702

#TimeUsernameProblemLanguageResultExecution timeMemory
1326702adscodingBulldozer (JOI17_bulldozer)C++20
25 / 100
2101 ms190072 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define FOR(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for (int i = a, _b = b; i >= _b; --i)
#define FORLL(i, a, b) for (ll i = a, _b = b; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a, _b = b; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)

template<typename T>
void __prine_one(const char *&s, const T &x)
{
    while (*s == ' ') ++s;
    const char *p = s;
    int bal = 0;
    while (*s)
    {
        if (*s == '(') ++bal;
        else if (*s == ')') --bal;
        else if (*s == ',' && bal == 0) break;
        ++s;
    }
    cerr.write(p, s - p) << " = " << x;
    if (*s == ',')
    {
        cerr << "  ,  ";
        ++s;
    }
}

template<typename... Args>
void debug(const char *s, Args... args)
{
    cerr << "[  ";
    int dummy[] = {0, (__prine_one(s, args), 0)...};
    (void)dummy;
    cerr << "  ]\n\n";
}

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

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

// --------------------------------------------------------------------------------------------

const int maxn = 2e3 + 3;
int n;

ll gcd(ll a, ll b)
{
    if (b == 0) return a;
    return gcd(b, a % b);
}

struct Geo
{
    ll x, y, val;
    Geo() {}
    Geo(ll x, ll y) : x(x), y(y) {}
} a[maxn];

struct Frac
{
    ll x, y;
    Frac() {}
    Frac(ll x, ll y) : x(x), y(y) {}

    bool operator < (const Frac &other) const
    {
        if (y == 0 && other.y == 0) return x < other.x;
        if (y == 0) return true;
        if (other.y == 0) return false;
        ll p1 = x * other.y - other.x * y, p2 = y * other.y;
        if (p1 < 0) swap(p1, p2);
        return (p1 > 0) && (p2 < 0);
    }

    bool operator == (const Frac &other) const
    {
        return x == other.x && y == other.y;
    }
};

struct Node
{
    ll res, sumL, sumR, sum;
    Node() {res = sumL = sumR = sum = 0;}
};

vector<Frac> slopes;
vector<pair<Frac, ll>> vec[maxn * maxn];
int POS[maxn];

// --------------------------------------------------------------------------------------------

namespace SegTree
{
    Node seg[maxn << 2];

    Node combine(const Node &a, const Node &b)
    {
        Node c;
        c.res = max({a.res, b.res, a.sumR + b.sumL});
        c.sumL = max(a.sumL, a.sum + b.sumL);
        c.sumR = max(b.sumR, b.sum + a.sumR);
        c.sum = a.sum + b.sum;
        return c;
    }

    void upd(int id, int l, int r, int pos, ll val)
    {
        if (l == r)
        {
            seg[id].res = seg[id].sumL = seg[id].sumR = max(0ll, val);
            seg[id].sum = val;
            return;
        }
        int mid = l + r >> 1;
        if (pos <= mid) upd(id << 1, l, mid, pos, val);
        else upd(id << 1 | 1, mid + 1, r, pos, val);
        seg[id] = combine(seg[id << 1], seg[id << 1 | 1]);
    }

    ll get(int id, int l, int r, int pos)
    {
        if (l == r) return seg[id].sum;
        int mid = l + r >> 1;
        if (pos <= mid) return get(id << 1, l, mid, pos);
        return get(id << 1 | 1, mid + 1, r, pos);
    }

    void DBG()
    {
        FOR(i, 1, n)
            cerr << get(1, 1, n, i) << ' ';
        cerr << "\n\n";
    }
}

int lower(Frac bruh) {return lower_bound(all(slopes), bruh) - slopes.begin() + 1;}


void encode(ll &x, ll &y)
{
    ll g = gcd(x, y);
    if (g == 0) return;
    x /= g;
    y /= g;
    if (x < 0) x *= -1, y *= -1;
}

void solve()
{
    cin >> n;
    FOR(i, 1, n)
        cin >> a[i].x >> a[i].y >> a[i].val;

    sort(a + 1, a + n + 1, [&](const Geo &a, const Geo &b) {
        if (a.x != b.x) return a.x < b.x;
        return a.y < b.y;
    });

    FOR(i, 1, n - 1)
        FOR(j, i + 1, n)
        {
            Frac V(a[j].y - a[i].y, a[j].x - a[i].x);
            if (V.y == 0) continue;
            encode(V.x, V.y);
            // dbg(i, j, V.x, V.y);
            slopes.push_back({V.x, V.y});
        }

    uni(slopes);

    FOR(i, 1, n - 1)
        FOR(j, i + 1, n)
        {
            Frac V(a[j].y - a[i].y, a[j].x - a[i].x);
            if (V.y == 0) continue;
            encode(V.x, V.y);
            ll tu = a[i].y * V.y - a[i].x * V.x, mau = V.y;
            encode(tu, mau);
            vec[lower(V)].push_back( {{tu, mau}, i});
            vec[lower(V)].push_back( {{tu, mau}, j});
            // dbg(tu, mau, i, j);
        }

    ll res = 0;

    // Initialize
    FOR(i, 1, n)
    {
        SegTree :: upd(1, 1, n, i, a[i].val);
        POS[i] = i;
    }
    res = max(res, SegTree :: seg[1].res);

    // FOR(i, 1, n)
    //     dbg(a[i].x, a[i].y, a[i].val);

    // SegTree :: DBG();


    // for (const auto &e : slopes)
    //     dbg(e.x, e.y);


    FOR(id, 1, (int)slopes.size())
    {
        // dbg(slopes[id - 1].x, slopes[id - 1].y);
        uni(vec[id]);
        FOR(i, 0, (int)vec[id].size() - 1)
        {
            int j = i;
            while (j + 1 < (int)vec[id].size() && vec[id][j + 1].fi.x == vec[id][i].fi.x && vec[id][j + 1].fi.y == vec[id][i].fi.y)
                ++j;
            FOR(k, 0, j - i - 1 >> 1)
            {
                int idone = vec[id][i + k].se, idtwo = vec[id][j - k].se;
                // dbg(idone, idtwo);
                SegTree :: upd(1, 1, n, POS[idone], a[idtwo].val);
                SegTree :: upd(1, 1, n, POS[idtwo], a[idone].val);
                swap(POS[idone], POS[idtwo]);

            }
            // SegTree :: DBG();
            // dbg(res, vec[id][i].fi.x, vec[id][i].fi.y);
            res = max(res, SegTree :: seg[1].res);
            i = j;
        }
    }

    cout << res;
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    #define TASK "TEST"
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        freopen(TASK".OUT", "w", stdout);
    }
    solve();
    return 0;
}

Compilation message (stderr)

bulldozer.cpp: In function 'int main()':
bulldozer.cpp:268:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  268 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bulldozer.cpp:269:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  269 |         freopen(TASK".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...