QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#198333#4246. Cactus is Moneyhotboy2703ML 1ms6876kbC++144.8kb2023-10-03 13:01:212023-10-03 13:01:22

Judging History

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

  • [2023-10-03 13:01:22]
  • 评测
  • 测评结果:ML
  • 用时:1ms
  • 内存:6876kb
  • [2023-10-03 13:01:21]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
struct pt{
    ll x,y;
    pt operator + (pt p) const{
        return {x+p.x, y+p.y};
    }
    pt operator - (pt p) const{
        return {x-p.x, y-p.y};
    }
    ll operator * (pt p)const{
        return x * p.y - y * p.x;
    }
};
ll sq(pt a,pt b){
    return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
void reorder_polygon(vector<pt> & P){
    size_t pos = 0;
    for(size_t i = 1; i < P.size(); i++){
        if(P[i].y < P[pos].y || (P[i].y == P[pos].y && P[i].x < P[pos].x))
            pos = i;
    }
    rotate(P.begin(), P.begin() + pos, P.end());
}

vector<pt> sum(vector<pt> P, vector<pt> Q){
    reverse(P.begin(),P.end());
    reverse(Q.begin(),Q.end());
    // the first vertex must be the lowest
    reorder_polygon(P);
    reorder_polygon(Q);
    // we must ensure cyclic indexing
    P.push_back(P[0]);
    P.push_back(P[1]);
    Q.push_back(Q[0]);
    Q.push_back(Q[1]);
    // main part
    vector<pt> result;
    size_t i = 0, j = 0;
    while(i < P.size() - 2 || j < Q.size() - 2){
        result.push_back(P[i] + Q[j]);
        auto cross = (P[i + 1] - P[i]) * (Q[j + 1] - Q[j]);
        if(cross >= 0 && i < P.size() - 2)
            ++i;
        if(cross <= 0 && j < Q.size() - 2)
            ++j;
    }
    return result;
}ll orientation(pt a, pt b, pt c) {
    ll v = a.x*(b.y-c.y)+b.x*(c.y-a.y)+c.x*(a.y-b.y);
    if (v < 0) return -1; // clockwise
    if (v > 0) return +1; // counter-clockwise
    return 0;
}
bool cw(pt a, pt b, pt c, bool include_collinear) {
    int o = orientation(a, b, c);
    return o < 0 || (include_collinear && o == 0);
}

bool collinear(pt a, pt b, pt c) { return orientation(a, b, c) == 0; }

void convex_hull(vector<pt>& a, bool include_collinear = false) {
    pt p0 = *min_element(a.begin(), a.end(), [](pt a, pt b) {
        return make_pair(a.y, a.x) < make_pair(b.y, b.x);
    });
    sort(a.begin(), a.end(), [&p0](const pt& a, const pt& b) {
        int o = orientation(p0, a, b);
        if (o == 0)
            return (p0.x-a.x)*(p0.x-a.x) + (p0.y-a.y)*(p0.y-a.y)
                < (p0.x-b.x)*(p0.x-b.x) + (p0.y-b.y)*(p0.y-b.y);
        return o < 0;
    });
    if (include_collinear) {
        int i = (int)a.size()-1;
        while (i >= 0 && collinear(p0, a[i], a.back())) i--;
        reverse(a.begin()+i+1, a.end());
    }

    vector<pt> st;
    for (int i = 0; i < (int)a.size(); i++) {
        while (st.size() > 1 && !cw(st[st.size()-2], st.back(), a[i], include_collinear))
            st.pop_back();
        st.push_back(a[i]);
    }

    a = st;
}
const ll MAXN = 5e4;
ll n,m;
vector <ll> g[MAXN + 100];
ll pa[MAXN + 100];
bool in[MAXN + 100];
struct edge{
    ll u,v,a,b;
};
ll depth[MAXN + 100];
edge all[MAXN * 5 + 100];
ll nxt(ll u, edge x){
    return (x.u == u?x.v:x.u);
}
vector <pt> tmp;
vector <vector <pt> > sex;
struct pq{
    vector <pt> a;
    bool operator < (const pq &p)const {
        return a.size() > p.a.size();
    }
};
priority_queue <pq> q;
void dfs(ll u){
    //cout<<u<<'\n';
    for (auto id:g[u]){
        auto v = nxt(u,all[id]);
        if (!in[v]){
            pa[v] = id;
            in[v] = 1;
            depth[v] = depth[u] + 1;
            //cout<<id<<' '<<u<<' '<<v<<'\n';
            dfs(v);
        }
        if (in[v]){
            if (depth[v] > depth[u] + 1){
                ll cur = v;
                tmp.clear();
                tmp.push_back({all[id].a,all[id].b});
                while (cur != u){
                    tmp.push_back({all[pa[cur]].a,all[pa[cur]].b});
                    cur = nxt(cur,all[pa[cur]]);
                }
                convex_hull(tmp);
                sex.push_back(tmp);
            }
        }
    }
}ll sum_a,sum_b;
int main(){
    ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);

    cin>>n>>m;
    sum_a = sum_b = 0;
    for (ll i = 1;i <= m;i ++){
        cin>>all[i].u>>all[i].v>>all[i].a>>all[i].b;
        g[all[i].u].push_back(i);
        g[all[i].v].push_back(i);
        sum_a += all[i].a;
        sum_b += all[i].b;
    }
    in[1] = 1;
    dfs(1);
    ll ans = sum_a * sum_b;

    for (auto x:sex){
        q.push({x});
    }
    while (q.size() > 1){
        auto u = q.top();
        q.pop();
        auto v = q.top();
        q.pop();
        auto tmp = sum(u.a,v.a);
        /*for (auto x:tmp){
            cout<<x.x<<' '<<x.y<<'\n';
        }*/
        q.push({tmp});
    }
    if (q.size()){

        vector <pt> minkowski = q.top().a;
        /*for (auto x:minkowski){
            cout<<x.x<<' '<<x.y<<'\n';
        }*/
        for (auto x:minkowski){
            ans = min(ans,(sum_a - x.x) * (sum_b - x.y));
        }
    }
    cout<<ans<<'\n';
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3 3
1 2 0 1000
2 3 0 1000
3 1 1 1

output:

0

result:

ok 1 number(s): "0"

Test #2:

score: 0
Accepted
time: 0ms
memory: 6796kb

input:

5 5
1 2 666 10
2 3 555 1000
3 1 444 345
1 4 222 234
2 5 333 123

output:

1185480

result:

ok 1 number(s): "1185480"

Test #3:

score: 0
Accepted
time: 0ms
memory: 6628kb

input:

5 6
1 3 12316 13729
1 2 171 10309
5 1 47389 7369
2 4 43318 36867
1 4 30728 43835
5 3 24437 31639

output:

6732185824

result:

ok 1 number(s): "6732185824"

Test #4:

score: 0
Accepted
time: 0ms
memory: 5940kb

input:

6 6
5 2 36032 49949
4 5 8963 9741
3 4 4004 35098
4 1 14459 30350
4 6 39612 8568
1 5 8645 16098

output:

11617618224

result:

ok 1 number(s): "11617618224"

Test #5:

score: 0
Accepted
time: 0ms
memory: 6492kb

input:

6 6
6 1 1516 16290
3 5 47834 15228
5 1 14795 44496
2 6 21296 36977
6 3 42659 16631
4 3 9808 31313

output:

13124412318

result:

ok 1 number(s): "13124412318"

Test #6:

score: 0
Accepted
time: 1ms
memory: 6660kb

input:

7 7
1 7 42781 13704
7 3 48779 17985
6 4 27969 24986
4 7 33333 35700
5 7 4859 49960
6 2 23927 33500
6 1 11232 15327

output:

24803495714

result:

ok 1 number(s): "24803495714"

Test #7:

score: 0
Accepted
time: 1ms
memory: 6876kb

input:

10 11
7 3 43798 8096
5 7 36600 4126
2 6 20599 15893
9 6 7541 5621
4 9 22047 10608
5 10 21235 2700
1 10 19260 8862
4 3 22407 37504
5 1 12867 1738
1 8 48480 15236
2 5 43525 13679

output:

19944198324

result:

ok 1 number(s): "19944198324"

Test #8:

score: -100
Memory Limit Exceeded

input:

10 12
10 2 21765 14299
4 2 8316 29600
3 2 36018 33286
4 5 30741 46828
9 7 13354 18461
9 5 11896 14216
6 1 35705 34008
1 9 41496 21860
7 5 37709 34178
8 7 1595 27497
6 9 12139 37471
3 5 43310 12734

output:


result: