QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#760303#4246. Cactus is MoneyWuyanruWA 5ms12276kbC++144.5kb2024-11-18 16:05:342024-11-18 16:05:35

Judging History

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

  • [2024-11-18 16:05:35]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:12276kb
  • [2024-11-18 16:05:34]
  • 提交

answer

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f3f3f3f3fll
#define debug(x) cerr<<#x<<"="<<x<<endl
using namespace std;
using ll=long long;
using ld=long double;
using pli=pair<ll,int>;
using pi=pair<int,int>;
template<typename A>
using vc=vector<A>;
using pl=pair<ll,ll>;
inline int read()
{
    int s=0,w=1;char ch;
    while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1;
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    return s*w;
}
inline ll lread()
{
    ll s=0,w=1;char ch;
    while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1;
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    return s*w;
}
template<const int N,const int M>
struct graph
{
    int head[N+5];
    int ww[M+5];
    int t[M+5];
    int x[M+5];
    int cntm;
    graph(){ cntm=1;}
    inline void clear(int n=N)
    {
        cntm=1;
        for(int i=1;i<=n;i++) head[i]=0;
    }
    inline void ad(int u,int v,int w=0)
    {
        cntm++;
        t[cntm]=v;
        x[cntm]=head[u];
        ww[cntm]=w;
        head[u]=cntm;
    }
    inline void add(int u,int v,int w=0)
    {
        ad(u,v,w);
        ad(v,u,w);
    }
    inline int st(int num){ return head[num];}
    inline int to(int num){ return t[num];}
    inline int nx(int num){ return x[num];}
    inline int w(int num){ return ww[num];}
};
graph<50000,100000>g;
priority_queue<pi,vc<pi>,greater<pi> >que;
vc<pl>nod[200005];
int vis[100005];
int u[100005];
int v[100005];
int a[100005];
int b[100005];
int dep[50005];
int fa[50005];
int faw[50005];
int f[50005];
int n,m,c;
inline int find(int num)
{
    if(f[num]==num) return num;
    return f[num]=find(f[num]);
}
void dfs(int num)
{
    for(int i=g.st(num);i;i=g.nx(i))
    {
        int p=g.to(i);
        if(p==fa[num]){ faw[num]=g.w(i);continue;}
        fa[p]=num,dep[p]=dep[num]+1,dfs(p);
    }
}
inline void run(int e)
{
    vc<pl>edge;ll sa=a[e],sb=b[e];
    edge.push_back(pl(a[e],b[e]));
    int u=::u[e],v=::v[e];
    while(u!=v)
    {
        if(dep[u]<dep[v]) swap(u,v);
        // printf("u=%d : v=%d : %d\n",u,v,faw[u]);
        int id=faw[u];u=fa[u];vis[id]=2;
        edge.push_back(pl(a[id],b[id]));
        sa+=a[id],sb+=b[id];
    }
    for(auto &i:edge) i.first=sa-i.first,i.second=sb-i.second;
    sort(edge.begin(),edge.end(),[](pl a,pl b)
    {
        if(a.first!=b.first) return a.first<b.first;
        return a.second>b.second;
    });

    c++;
    for(auto i:edge)
    {
        while(nod[c].size()>=2)
        {
            auto p=nod[c].back(),q=nod[c].end()[-2];
            // q   p   i
            if((p.second-q.second)*(i.first-p.first)>(i.second-p.second)*(p.first-q.first)) nod[c].pop_back();
            else break;
        }
        nod[c].push_back(i);
    }
    que.push(pi(nod[c].size(),c));
}
inline bool check(int u,unsigned x,int v,unsigned y)
{
    if(x+1>=nod[u].size()) return false;
    if(y+1>=nod[v].size()) return true;
    int x1,x2,x3,x4,y1,y2,y3,y4;
    tie(x1,y1)=nod[u][x],tie(x2,y2)=nod[u][x+1];
    tie(x3,y3)=nod[v][y],tie(x4,y4)=nod[v][y+1];
    return (y2-y1)*(x4-x3)<(y4-y3)*(x2-x1);
}
inline void merge(int u,int v)
{
    c++;unsigned x=0,y=0;
    // printf("merge %d %d = %d\n",u,v,c);
    while(true)
    {
        ll x1,y1,x2,y2;
        tie(x1,y1)=nod[u][x];
        tie(x2,y2)=nod[v][y];
        nod[c].push_back(pl(x1+x2,y1+y2));
        if(x+1>=nod[u].size()&&y+1>=nod[v].size()) break;
        if(check(u,x,v,y)) x++;else y++;
    }
    que.push(pi(nod[c].size(),c));
}
int main()
{
    n=read(),m=read();
    for(int i=1;i<=n;i++) f[i]=i;
    for(int i=1;i<=m;i++)
    {
        u[i]=read(),v[i]=read(),a[i]=read(),b[i]=read();
        int U=find(u[i]),V=find(v[i]);
        if(U!=V) f[U]=V,g.add(u[i],v[i],i),vis[i]=1;
    }
    dfs(1);
    for(int i=1;i<=m;i++) if(!vis[i]) run(i);
    // for(int i=1;i<=m;i++) printf("%d%c",vis[i]," \n"[i==m]);
    for(int i=1;i<=m;i++) if(vis[i]==1)
    {
        c++;
        nod[c].push_back(pi(a[i],b[i]));
        que.push(pi(1,c));
    }

    // printf("c=%d\n",c);
    // for(int i=1;i<=c;i++)
    // {
    //     for(auto j:nod[i]) printf("(%lld,%lld) ",j.first,j.second);
    //     putchar('\n');
    // }

    while(que.size()>=2)
    {
        int u=que.top().second;que.pop();
        int v=que.top().second;que.pop();
        merge(u,v);
    }

    int P=que.top().second;ll ans=2*inf;
    for(auto i:nod[P]) ans=min(ans,i.first*i.second);
    printf("%lld\n",ans);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 12136kb

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: 9860kb

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: 2ms
memory: 10200kb

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: 2ms
memory: 10148kb

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: 2ms
memory: 10044kb

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: 0ms
memory: 9864kb

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: 0ms
memory: 9936kb

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: 0
Accepted
time: 2ms
memory: 12136kb

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:

39767313936

result:

ok 1 number(s): "39767313936"

Test #9:

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

input:

10 13
10 1 28967 29646
9 1 45852 47509
6 1 2590 36591
5 4 21715 40134
1 5 44167 42132
2 10 27289 12132
5 7 26720 1258
1 3 2762 28223
3 6 6966 48096
5 8 23536 33618
7 8 28326 9456
1 2 4885 15100
5 9 41414 32957

output:

43567826244

result:

ok 1 number(s): "43567826244"

Test #10:

score: 0
Accepted
time: 2ms
memory: 10228kb

input:

20 28
18 19 35583 99
15 18 30705 19256
14 18 20288 26882
18 4 39622 18793
11 7 47613 25425
8 18 33445 7046
18 13 37838 47190
18 1 47524 18152
8 10 20627 40858
18 6 49396 47654
9 18 9453 36001
20 18 33280 38985
11 18 47686 42244
1 20 8726 35210
14 6 20998 13452
18 10 10397 48525
19 4 45811 48722
5 17...

output:

196145757738

result:

ok 1 number(s): "196145757738"

Test #11:

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

input:

20 28
1 11 29270 12003
11 14 39731 27230
4 11 43657 44868
13 10 37887 17666
8 6 12758 4662
12 18 34016 15740
12 11 1123 44690
1 14 1866 31755
17 7 12441 26285
3 10 29159 11042
4 19 22612 12647
2 3 43899 27826
17 3 26727 7304
20 5 32807 49683
16 3 40800 14397
3 5 21244 13387
11 3 39919 35610
15 9 944...

output:

135255186000

result:

ok 1 number(s): "135255186000"

Test #12:

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

input:

500 627
218 441 9330 38888
394 403 39336 6683
81 319 10978 28288
157 204 27638 7127
58 38 7029 7446
168 100 23915 46064
180 151 42120 1549
18 238 15877 34786
37 259 37301 2159
463 185 4155 13849
77 355 5111 14848
62 486 19786 48858
132 408 41592 45835
245 378 46373 21496
291 20 36847 18682
435 213 3...

output:

115468900928208

result:

ok 1 number(s): "115468900928208"

Test #13:

score: 0
Accepted
time: 2ms
memory: 10500kb

input:

500 655
118 73 5102 36606
387 191 18264 17089
462 226 6016 40383
215 303 3543 6165
95 192 19518 19250
37 430 21505 26348
406 484 45283 8029
446 64 32626 30683
432 146 22720 12184
1 145 4822 43486
169 355 44960 12596
399 372 1339 28076
452 387 11774 33758
186 72 47444 20235
145 61 30207 39522
87 419 ...

output:

126690228324902

result:

ok 1 number(s): "126690228324902"

Test #14:

score: 0
Accepted
time: 2ms
memory: 11964kb

input:

500 499
11 127 37123 28858
439 253 39067 7779
148 142 5215 35775
441 354 15081 38415
145 463 8601 8508
400 332 23071 25635
193 124 33687 31694
154 449 45482 46872
499 469 49487 28780
24 246 48364 48917
458 397 3906 33392
130 108 13072 11058
235 406 11881 17681
20 172 35252 25687
81 24 41900 11751
49...

output:

159542365665488

result:

ok 1 number(s): "159542365665488"

Test #15:

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

input:

500 748
307 162 27455 28463
264 251 6256 153
393 228 41230 20255
251 213 27015 29368
60 446 39647 38740
335 462 29065 41130
347 310 37880 860
251 314 13609 26570
164 80 835 21262
164 216 460 5584
84 251 804 1471
455 293 7746 17437
164 496 37793 9885
164 287 31791 14404
164 94 33272 27145
164 395 273...

output:

105301950132005

result:

ok 1 number(s): "105301950132005"

Test #16:

score: 0
Accepted
time: 2ms
memory: 9976kb

input:

500 615
470 264 2116 6483
440 140 29470 23326
72 110 43214 5293
196 51 12514 42244
71 280 47085 45943
72 156 25064 13548
78 316 49949 12907
487 404 47809 27698
97 73 28640 7923
314 144 20781 47165
215 186 29540 33017
40 6 1702 36877
100 448 13492 5457
134 312 34982 45427
26 456 3457 39634
467 15 487...

output:

122025377634420

result:

ok 1 number(s): "122025377634420"

Test #17:

score: 0
Accepted
time: 2ms
memory: 10144kb

input:

500 520
384 23 14167 37928
451 70 27730 45983
361 187 1885 14513
263 474 20862 14647
405 55 37858 43463
172 115 45107 42769
261 497 31084 14910
270 68 10529 29535
462 447 38119 31465
360 265 9025 28960
30 486 21538 19889
226 115 39349 7682
336 145 39522 25071
59 101 325 44293
210 425 18047 30563
38 ...

output:

153696331934238

result:

ok 1 number(s): "153696331934238"

Test #18:

score: 0
Accepted
time: 2ms
memory: 12276kb

input:

500 748
2 309 1955 7472
466 309 12731 3830
309 202 10683 40372
309 424 12313 21845
226 309 5799 201
309 223 48026 30523
309 316 1995 16318
284 491 33672 40151
136 92 6914 40439
374 309 30718 8284
309 388 26978 46456
309 358 49433 7807
309 357 37345 27104
309 77 41751 12637
247 218 14296 49893
126 30...

output:

102303174347192

result:

ok 1 number(s): "102303174347192"

Test #19:

score: 0
Accepted
time: 3ms
memory: 11940kb

input:

496 592
444 101 9126 21347
162 314 39250 32069
47 66 49451 247
48 405 6810 48526
27 103 37728 28763
33 224 3694 25966
482 331 35767 14789
321 166 2277 34839
172 126 45593 7174
253 373 1791 31940
296 32 44668 41739
405 237 14070 6387
173 96 31495 9016
117 93 22934 579
138 60 32187 11485
39 51 21377 2...

output:

127478515292964

result:

ok 1 number(s): "127478515292964"

Test #20:

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

input:

5000 6251
2635 2866 9485 41656
3395 12 48029 28753
1355 4756 8596 41769
4409 824 14906 21781
4910 798 16065 18757
2446 236 48837 49259
250 3184 17031 3633
1282 3498 26172 32327
2917 2922 10599 11306
1851 4625 35802 9097
4677 4747 34879 46343
3183 4977 48133 33354
2573 4376 18409 5593
3747 3446 11414...

output:

12527521887194124

result:

ok 1 number(s): "12527521887194124"

Test #21:

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

input:

5000 6145
1313 3222 4495 29920
23 2998 30246 28243
4150 3094 9039 21283
1186 660 30630 41879
1763 1812 32424 20989
3687 647 22633 47318
220 3430 18320 7034
4305 218 19909 38502
1655 2152 36607 23407
4013 685 248 49464
4111 744 45162 25334
3394 1506 19103 37547
2264 694 1441 11602
2270 894 11696 4150...

output:

12904728674927010

result:

ok 1 number(s): "12904728674927010"

Test #22:

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

input:

5000 5132
3134 2216 21573 49710
1693 1306 2221 3976
736 2792 8454 22980
4451 4048 974 32205
753 3706 46077 27080
2252 4635 23240 40818
2329 4526 3018 44305
1011 2993 30907 15099
2444 4904 38134 44194
180 1855 23695 36515
805 858 6274 39268
679 472 18444 21139
2695 1566 32679 28801
1937 4421 13150 11...

output:

15210187350056955

result:

ok 1 number(s): "15210187350056955"

Test #23:

score: -100
Wrong Answer
time: 5ms
memory: 11192kb

input:

5000 7498
4078 3807 3030 46583
1205 930 42669 31127
2905 4903 45548 47824
3975 1006 22937 20197
4052 3807 9480 13036
3205 3807 15806 36037
3348 3807 43774 24203
3807 1139 30022 28974
2336 3807 5941 42084
872 317 29856 3240
4999 3807 16501 7125
16 995 30723 44558
2832 3807 28831 614
1967 2860 42889 4...

output:

10711899555895920

result:

wrong answer 1st numbers differ - expected: '10708082290367826', found: '10711899555895920'