QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#622647#7816. 种树tiankonguse100 ✓925ms18940kbC++204.0kb2024-10-08 23:41:242024-10-08 23:41:25

Judging History

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

  • [2024-10-08 23:41:25]
  • 评测
  • 测评结果:100
  • 用时:925ms
  • 内存:18940kb
  • [2024-10-08 23:41:24]
  • 提交

answer


/*
ID: tiankonguse
TASK: tree
LANG: C++
CONTEST: CSP-S 2023
qoj: https://qoj.ac/contest/1428/problem/7816
luogu: https://www.luogu.com.cn/problem/P9755
*/
#define TASK "tree3"

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

ll debug = 0;
#define myprintf(...)                        \
  do {                                       \
    if (debug) fprintf(stdout, __VA_ARGS__); \
  } while (0)

void InitIO() {
  // #ifndef USACO_LOCAL_JUDGE
  // freopen(TASK ".in", "r", stdin);
  // freopen(TASK ".out", "w", stdout);
  // #endif
}

vector<tuple<ll, ll, ll>> points;
vector<vector<ll>> g;
ll n;

vector<ll> lastDay;

/*
h=max(b+xc,1)
ci<0: b-c, b-2c, b-3c, ...,     1
ci=0:   b,    b,    b, ...,     b
ci>0: b+c, b+2c, b+3c, ..., b+k*c

b+xc >= 1
b - 1 >= -cx
(b - 1) / (-c) >= x

*/
bool FixRangeSum(const ll i, const ll x0, const ll xn, ll A) {
  const auto [a, b, c] = points[i];
  const ll down = xn - x0 + 1;
  const ll low = min(x0 * c + b, xn * c + b);
  const ll high = max(x0 * c + b, xn * c + b);
  if (i == 0) {
    // printf(
    //     "fix: i[%lld] x0[%lld] xn[%lld] A[%lld] down[%lld] low[%lld] "
    //     "high[%lld] A/down[%lld]\n",
    //     i, x0, xn, A, down, low, high, A / down);
  }
  if (A / down < low) {  //
    return true;
  }
  A -= low * down;

  return (high - low) * down / 2 >= A;
}
bool CheckRangeSum(const ll i, const ll x0, const ll xn) {
  const auto [a, b, c] = points[i];
  ll A = a;
  if (c >= 0) {
    return FixRangeSum(i, x0, xn, a);
  }
  // c < 0
  const ll one = (b - 1) / (-c);
  if (i == 58099) {
    myprintf("i[%lld] one[%lld]\n", i, one);
  }
  if (x0 > one) {  // 全是 1
    return xn - x0 + 1 >= A;
  } else if (xn <= one) {  // 斜线
    return FixRangeSum(i, x0, xn, a);
  } else {  // 一半斜线 + 一半 1
    if (xn - one >= A) {
      return true;
    }
    return FixRangeSum(i, x0, one, a - (xn - one));
  }
}
ll Cal(const ll i, const ll d) {  //
  // [1, x] [x, d]
  const auto [a, b, c] = points[i];
  ll l = 1, r = d + 1;
  while (l < r) {                // [l, r)
    const ll mid = (l + r) / 2;  // sum(mid, d)
    if (CheckRangeSum(i, mid, d)) {
      l = mid + 1;
    } else {
      r = mid;
    }
  }
  if (r == 1) {  // 不满足要求
    // myprintf("Cal: i[%lld] a[%lld] b[%lld] c[%lld] d[%lld]\n", i, a, b, c,
    // d);
    return -1;
  }
  return r - 1;
}

bool Dfs(const ll u, const ll maxDay, const ll pre = -1) {
  lastDay[u] = Cal(u, maxDay);
  if (lastDay[u] < 1) {
    // myprintf("Dfs u[%lld] lastDay[%lld]\n", u, lastDay[u]);
    return false;
  }
  for (auto v : g[u]) {
    if (v == pre) continue;
    if (!Dfs(v, maxDay, u)) {
      return false;
    }
    lastDay[u] = min(lastDay[u], lastDay[v] - 1);
    if (lastDay[u] < 1) {
      // myprintf("Dfs pre: u[%lld] lastDay[%lld] v[%lld] lastDay[%lld]\n", u,
      //          lastDay[u], v, lastDay[v]);
      return false;
    }
  }
  return true;
}
bool Check(ll maxDay) {
  if (!Dfs(0, maxDay)) {
    // myprintf("dfs err: maxDay[%lld]\n", maxDay);
    return false;
  }
  sort(lastDay.begin(), lastDay.end());
  for (ll i = 1; i <= n; i++) {
    if (lastDay[i - 1] < i) {
      // myprintf("sort err: i[%lld] > lastDay[%lld]\n", i, lastDay[i - 1]);
      return false;
    }
  }
  return true;
}
void Solver() {  //
  scanf("%lld", &n);
  points.reserve(n);
  lastDay.resize(n);
  g.resize(n);
  for (ll i = 0; i < n; i++) {
    ll a, b, c;
    scanf("%lld%lld%lld", &a, &b, &c);
    points.push_back({a, b, c});
  }
  for (ll i = 1; i < n; i++) {
    ll u, v;
    scanf("%lld%lld", &u, &v);
    u--, v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  ll l = 1, r = 10e9 + 1;
  while (l < r) {  //[l,r)
    ll mid = (l + r) / 2;
    if (Check(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  printf("%lld\n", r);
}

int main() {
  InitIO();
  Solver();
  return 0;
}
/*
4
12 1 1
2 4 -1
10 3 0
7 10 -2
1 2
1 3
3 4

5




*/

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 5
Accepted
time: 1ms
memory: 3900kb

input:

19
17504574400613383 804340056 0
11404434099221265 524036995 0
4501303182232576 206836143 0
16722432340969341 768400608 0
9242175242163545 424680724 0
5982424224154348 274894135 0
13497298340940242 620204607 0
12416917749109322 570561236 0
19824012344280862 910919463 0
10845352638600258 498347020 0
...

output:

21762657

result:

ok single line: '21762657'

Test #2:

score: 5
Accepted
time: 1ms
memory: 3948kb

input:

20
610287562490950674 885730665 524863980
6465154937106138 125644983 10
25526148 86884815 -418052299
4878566359808986 698467722 -50
4346217306420974 456748134 -24
410451661036151236 866022801 555949789
296430095754312 185435049 -58
25779019751029177 231358373 61
9670756993083570 595829152 -17
431552...

output:

25526156

result:

ok single line: '25526156'

Test #3:

score: 5
Accepted
time: 0ms
memory: 3856kb

input:

20
8711196809241382 866431622 -26
12338190 56486490 -751942578
1592174753724827 319217044 -32
4992314209553308 484821039 -13
10779700847116122 404833961 76
2403722922189903 293525604 -16
873521103768048 347197602 -69
10178465446375981 769434836 9
2900422388953204 637228359 -70
53813051603 668492735 ...

output:

12338199

result:

ok single line: '12338199'

Test #4:

score: 5
Accepted
time: 0ms
memory: 3948kb

input:

19
33367539931450991 663203498 32
29424676 841039921 -405390549
29424671 179593001 -448755475
8599917315457196 27446701 18
1079974575 419422498 -54295744
767944283972635088 107310085 968205371
302566933069622961 143198850 277590648
23977603746286878 770743890 3
1625519517805099 99380443 -3
235074643...

output:

29424682

result:

ok single line: '29424682'

Test #5:

score: 5
Accepted
time: 3ms
memory: 3948kb

input:

483
13367341539676287 733769679 0
4211118081815869 231160151 0
10197866403063976 559790303 0
6121300699525614 336020709 0
1103617512499360 60581275 0
15425441323132413 846757437 0
4878271049470331 267784905 0
17931772118332105 984324337 0
9665280520811971 530561282 0
11403806139090960 625992042 0
16...

output:

18217358

result:

ok single line: '18217358'

Test #6:

score: 5
Accepted
time: 3ms
memory: 3908kb

input:

490
2984828998817324 291578429 0
6105552015228263 596444244 0
8525091497379773 832803177 0
9287427407126683 907295837 0
4445810810895144 434305864 0
7527368227697447 735333521 0
6496864359949274 634673243 0
1284767431414830 125506098 0
47991310760061 4688288 0
2581423123082609 252172843 0
4486538761...

output:

10236801

result:

ok single line: '10236801'

Test #7:

score: 5
Accepted
time: 675ms
memory: 16244kb

input:

98068
24736182085358185 936645750 0
3510371316587025 133264930 0
2292917642571107 86967621 0
20385507317903643 771954166 0
13497637843860286 511668127 0
23197997540820201 879450171 0
23107965873843730 877572279 0
5404839393304701 205069005 0
9234433283255686 349883761 0
16387675341715268 620654033 0...

output:

26409329

result:

ok single line: '26409329'

Test #8:

score: 5
Accepted
time: 708ms
memory: 16372kb

input:

98625
8390277748057767 747232900 0
2922897641415590 261954905 0
3203348914382647 286279270 0
3319100720474264 297738327 0
7629697862631957 680867145 0
7306521637407375 656425140 0
1438161774708376 128132185 0
6579576501710004 590320232 0
8940869930138178 799840795 0
5024215393336122 449233870 0
9747...

output:

11228465

result:

ok single line: '11228465'

Test #9:

score: 5
Accepted
time: 585ms
memory: 18940kb

input:

96952
6752323744453177 965308625 -69
4328169696116843 767223202 -68
17241656022550226 179775166 33
624155531180678 297708510 -71
187285846375965 139562908 -52
716579557551091036 414386386 547464358
18504131411192813 294297003 28
331637334883155207 948891999 379540451
27333757 390096717 -239512996
15...

output:

27333767

result:

ok single line: '27333767'

Test #10:

score: 5
Accepted
time: 475ms
memory: 18928kb

input:

96211
607050328098364 287330784 -68
424923421938 848483264 -842956
19786614719574227 728012369 36
7207630418730999 848977870 -50
7265447765886168 185596644 22
18613088 283225150 -220190926
7217233837552132 294684966 10
59831235573 815400612 -4994334
739994286920756 163217240 -18
32561133980 70984813...

output:

18613096

result:

ok single line: '18613096'

Test #11:

score: 5
Accepted
time: 877ms
memory: 15488kb

input:

97936
501442597788017836 97491864 516969001
21310552507704442 86363298 87
4547058185513703 705949370 -54
13187606873963218 900066454 -26
156294061414667 166101056 -82
1261403599318125 91370413 -3
601380658699048 256596875 -53
354430568524104830 269053350 832773317
12417268719533656 536045569 5
17134...

output:

21163203

result:

ok single line: '21163203'

Test #12:

score: 5
Accepted
time: 890ms
memory: 15772kb

input:

99503
83912131469047 46708859 -13
24160650459704263 725210751 -6
12248665607976536 991188394 -40
39859669 666496843 -369125808
9271625079448749 893423481 -43
34608071669415768 547418845 16
39346347291405991 866815614 6
23281993942850822 343658301 12
39869908 623077978 -2412934
3952712282806621 60504...

output:

39946379

result:

ok single line: '39946379'

Test #13:

score: 5
Accepted
time: 925ms
memory: 15128kb

input:

95591
333631479752719982 762809876 988690423
955856016313241845 856473148 38983018
15391869170844710 385163009 57
17434115 100601220 -804313
3300371942714826 180514304 1
12531097778773676 492457374 26
18127636259479227 820384904 25
17378632 425077684 -3883921
266485566978787379 910881610 11610984
17...

output:

17456324

result:

ok single line: '17456324'

Test #14:

score: 5
Accepted
time: 693ms
memory: 12676kb

input:

99722
28561842 316582396 -667867051
27359515427141082 860294481 7
47879901036919767 676764277 70
282969588868690 47902813 -4
13767161291064061 941731495 -32
14854658466549314 291972928 16
2515185989836 14495043 -39
40282022971868802 525243399 62
207112824723291568 560630689 868063396
28541899 132469...

output:

28561843

result:

ok single line: '28561843'

Test #15:

score: 5
Accepted
time: 670ms
memory: 12184kb

input:

96017
14193966715188693 936932959 1
13408120247693489 663369599 31
17392984679823849 956807908 27
1137625652734643 220559080 -21
15954679482681309 826038863 32
4253092435762905 442906604 -21
2913653950744919 352238815 -21
10202984698515340 665245293 2
1299085998655680 276228881 -29
43876261915902355...

output:

15028861

result:

ok single line: '15028861'

Test #16:

score: 5
Accepted
time: 749ms
memory: 12256kb

input:

97496
3399779480019378 583653359 28
2780779927926144 580176357 -15
393262568993511 45280150 12
2392453796784863 370410852 36
2238588345098511 515712055 -29
2193015172347022 469257248 -17
5157951 83795105 -792838592
5103282 690421968 -258727292
774041769065757266 495167170 87528302
4383299122073811 8...

output:

5181103

result:

ok single line: '5181103'

Test #17:

score: 5
Accepted
time: 789ms
memory: 16068kb

input:

97035
11138323876909669 858066681 -33
24927721 850332261 -3485476
13095087399763577 712788459 -15
21914965933872537 840546214 3
495836160603889519 211898658 226718454
24163138508550108 494755725 38
990455643700458015 965964130 717707292
12940889831380886 644004346 -10
23310168142949555 346823584 47
...

output:

24974881

result:

ok single line: '24974881'

Test #18:

score: 5
Accepted
time: 877ms
memory: 15988kb

input:

96904
47632912 461929503 -328698266
8560397922741897 945311099 -52
849971118104174667 174394009 949337793
612829294672280192 87547121 862577605
47547057 816886283 -1078226
2830467015314551 560575468 -55
977593027741067947 43812040 383130593
46080307672333653 967940186 0
45713386039150 66618812 -45
3...

output:

47632915

result:

ok single line: '47632915'

Test #19:

score: 5
Accepted
time: 819ms
memory: 16092kb

input:

98080
68084211018787514 517652918 77
24263075532003869 372227138 17
35862754 831800310 -718706296
23002196975998622 984591855 -19
6252941799598187 536771574 -23
44930975964002451 517659032 41
12544556688953478 511575465 -9
35767083 854443081 -883902575
35827924 569679806 -2369294
46970767015388971 8...

output:

35863814

result:

ok single line: '35863814'

Test #20:

score: 5
Accepted
time: 763ms
memory: 15996kb

input:

97616
7919030463690887 871910096 -48
58190337825439230 751836072 39
1077825882470901 212787459 -21
38594331 67280490 -365424
18374272772606903 610905948 -7
3282084729174615 575054091 -50
4432559463800591 798233315 -71
67585198658027 65388317 -31
54386737938232642 787901749 32
38594981 98843983 -6804...

output:

38678960

result:

ok single line: '38678960'