QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#71862 | #4102. 游戏 | He_Ren | 100 ✓ | 252ms | 24524kb | C++98 | 4.5kb | 2023-01-12 02:06:54 | 2023-01-12 02:06:58 |
Judging History
answer
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 1e5 + 5;
const int MAXM = 1e5 + 5;
struct Edge
{
int next,to,w;
}e[MAXN<<1];
int head[MAXN],etot=0;
inline void add(int u,int v,int w)
{
e[++etot] = (Edge){ head[u],v,w};
head[u]=etot;
}
template<typename Line,const int MAXN>
struct Segment_Tree
{
Line *p[MAXN<<2];
ll mn[MAXN<<2];
int n;
#define ls(u) ((u)<<1)
#define rs(u) ((u)<<1|1)
#define lson(u) ls(u),l,mid
#define rson(u) rs(u),mid+1,r
inline void push_up(int u,int l,int r){ mn[u] = min(p[u] -> calc(l,r), min(mn[ls(u)], mn[rs(u)]));}
void build(int n_,Line &k){ n=n_; build(1,1,n,&k);}
void build(int u,int l,int r,Line *k)
{
p[u] = k; mn[u] = p[u] -> calc(l,r);
if(l==r) return;
int mid = (l+r)>>1;
build(lson(u),k); build(rson(u),k);
}
void update(int ql,int qr,Line &k){ update(1,1,n,ql,qr,&k);}
void update(int u,int l,int r,int ql,int qr,Line *k)
{
if(ql<=l && r<=qr)
{
mn[u] = min(mn[u], k -> calc(l,r));
bool wl = k -> calc(l) < p[u] -> calc(l), wr = k -> calc(r) < p[u] -> calc(r);
if(wl && wr){ p[u] = k; return;}
if(!wl && !wr) return;
int mid = (l+r)>>1;
if(k -> calc(mid) < p[u] -> calc(mid))
{
if(k -> k < p[u] -> k) update(lson(u),ql,qr,p[u]);
else update(rson(u),ql,qr,p[u]);
p[u] = k;
}
else
{
if(k -> k < p[u] -> k) update(rson(u),ql,qr,k);
else update(lson(u),ql,qr,k);
}
return;
}
int mid = (l+r)>>1;
if(ql<=mid) update(lson(u), ql,qr,k);
if(mid<qr) update(rson(u), ql,qr,k);
push_up(u,l,r);
}
ll query(int q){ return query(1,1,n,q);}
ll query(int u,int l,int r,int q)
{
if(l==r) return mn[u];
int mid = (l+r)>>1;
if(q<=mid) return min(p[u] -> calc(q), query(lson(u),q));
else return min(p[u] -> calc(q), query(rson(u),q));
}
ll query(int ql,int qr){ return query(1,1,n,ql,qr);}
ll query(int u,int l,int r,int ql,int qr)
{
if(ql<=l && r<=qr) return mn[u];
int mid = (l+r)>>1; ll res = p[u] -> calc(max(ql,l), min(qr,r));
if(ql<=mid) res = min(res, query(lson(u),ql,qr));
if(mid<qr) res = min(res, query(rson(u),ql,qr));
return res;
}
};
int dep[MAXN], size[MAXN], son[MAXN], pre[MAXN], prew[MAXN];
ll dis[MAXN];
void dfs_size(int u,int fa)
{
size[u]=1; son[u]=-1;
for(int i=head[u]; i; i=e[i].next)
{
int v=e[i].to;
if(v==fa) continue;
dep[v] = dep[u] + 1; dis[v] = dis[u] + e[i].w;
pre[v] = u; prew[v] = e[i].w;
dfs_size(v,u);
size[u] += size[v];
if(son[u]==-1 || size[v] > size[son[u]]) son[u] = v;
}
}
int dfn[MAXN], seq[MAXN], cur = 0, top[MAXN];
void dfs_dfn(int u,int fa,int tp)
{
dfn[u] = ++cur; seq[cur] = u;
top[u] = tp;
if(son[u] == -1) return;
dfs_dfn(son[u],u, tp);
for(int i=head[u]; i; i=e[i].next)
{
int v = e[i].to;
if(v==fa || v==son[u]) continue;
dfs_dfn(v,u,v);
}
}
int lca(int u,int v)
{
while(top[u] != top[v])
if(dep[top[u]] > dep[top[v]]) u = pre[top[u]];
else v = pre[top[v]];
return dep[u] > dep[v]? v: u;
}
ll cor[MAXN];
struct Line
{
int k;
ll b;
Line(int k=0,ll b=0): k(k), b(b) {}
Line(int i,ll beg,int kk): k(-kk), b(beg + cor[i] * kk) {}
inline ll calc(int x){ return k * cor[x] + b;}
inline ll calc(int l,int r){ return k<0? calc(r): calc(l);}
};
Segment_Tree<Line,MAXN> tree;
void update(int u,int v, ll beg,int k)
{
while(top[u] != top[v])
{
tree.update(dfn[top[u]], dfn[u], *new Line(dfn[u],beg,k));
beg += (dis[u] - dis[pre[top[u]]]) * k;
u = pre[top[u]];
}
tree.update(dfn[v], dfn[u], *new Line(dfn[u],beg,k));
}
ll query(int u,int v)
{
ll res = 123456789123456789;
while(top[u] != top[v])
{
res = min(res, tree.query(dfn[top[u]], dfn[u]));
u = pre[top[u]];
}
return min(res, tree.query(dfn[v], dfn[u]));
}
int main(void)
{
int n,Q;
scanf("%d%d",&n,&Q);
for(int i=1; i<n; ++i)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w); add(v,u,w);
}
dfs_size(1,0);
dfs_dfn(1,0,1);
for(int i=1; i<=n; ++i)
{
int u = seq[i];
if(u == top[u]) cor[i] = cor[i-1] + 1;
else cor[i] = cor[i-1] + prew[u];
}
tree.build(n,*new Line(0,123456789123456789));
while(Q--)
{
int oper,u,v;
scanf("%d%d%d",&oper,&u,&v);
if(oper==1)
{
int a,b;
scanf("%d%d",&a,&b);
int uv = lca(u,v);
update(u,uv,b,a); update(v,uv,(dis[u] + dis[v] - 2 * dis[uv]) * a + b,-a);
}
else
{
int uv = lca(u,v);
printf("%lld\n",min(query(u,uv), query(v,uv)));
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 5
Accepted
time: 2ms
memory: 3188kb
input:
10 9 1 2 414764164 2 3 97887995 2 4 490352459 2 5 768162126 1 6 535120978 4 7 900218778 6 8 326337243 5 9 732360384 5 10 214886247 2 10 3 1 3 7 -6545 -393628632 1 9 4 792 960571738 1 4 9 3346 574390524 2 6 3 1 4 1 -2208 269163764 1 9 4 -8656 -157968875 2 10 7 2 9 6
output:
123456789123456789 -641070555907 -17233171700539 -12988680815435
result:
ok 4 lines
Test #2:
score: 5
Accepted
time: 0ms
memory: 3120kb
input:
8 9 1 2 642283469 2 3 273577369 3 4 817572202 3 5 51550466 1 6 988162712 3 7 109939542 4 8 619344858 2 5 6 1 8 1 8652 208871973 2 1 6 2 5 7 2 7 4 2 8 6 1 5 5 2992 -48250761 2 4 2 1 6 3 455 963067700
output:
123456789123456789 20356443245469 12432415275093 5358780583389 208871973 5358780583389
result:
ok 6 lines
Test #3:
score: 5
Accepted
time: 0ms
memory: 3400kb
input:
860 718 1 2 332880865 1 3 697416917 2 4 357423698 4 5 562415050 4 6 611105059 4 7 41153336 1 8 548418133 4 9 472361150 5 10 492426537 10 11 748871999 2 12 286738976 5 13 975712988 12 14 974993658 8 15 796965108 11 16 370834105 12 17 500081707 13 18 628627358 14 19 800085087 15 20 523679018 18 21 927...
output:
123456789123456789 123456789123456789 16628909729837 123456789123456789 -178215967853329 -200708636613541 -129585402300697 -167646861328135 -291662594383644 -178215967853329 -177636578644861 -300064978291223 -203347623674629 -300064978291223 -258071997235178 -167646861328135 -238220081799159 -134460...
result:
ok 349 lines
Test #4:
score: 5
Accepted
time: 0ms
memory: 3332kb
input:
740 892 1 2 167504413 2 3 476394973 3 4 535919319 2 5 955110531 5 6 48283790 2 7 610642774 6 8 627302000 7 9 982785317 7 10 819533040 5 11 996425218 8 12 424047039 7 13 985969983 6 14 92263374 7 15 758540651 10 16 206627661 16 17 542068313 12 18 528481197 13 19 425175833 16 20 414259616 12 21 387850...
output:
123456789123456789 123456789123456789 123456789123456789 4804637734087 -703592128339903 -326033245427783 -703592128339903 -698172333961283 -703592128339903 -657448436757723 -377553894334383 -219522147821183 -676977856903203 -158673884129383 -676977856903203 -437122966336583 -451594027843323 -5363706...
result:
ok 439 lines
Test #5:
score: 5
Accepted
time: 125ms
memory: 23404kb
input:
88131 84470 1 2 721832540 2 3 117639849 3 4 775294978 4 5 366304138 5 6 206436566 6 7 771736689 7 8 585274968 8 9 90462335 9 10 191294283 10 11 124084246 11 12 279272366 12 13 587772118 13 14 970494659 14 15 813070240 15 16 386660619 16 17 826598038 17 18 554299463 18 19 854577432 19 20 509484407 20...
output:
123456789123456789 81365001 123456789123456789 81365001 -332098236 -332098236 -332098236 425226692 -74894657 -890662358 -890662358 -890662358 -890662358 -890662358 -890662358 -890662358 -890662358 -890662358 -890662358 -890662358 -890662358 -812478238 -890662358 -890662358 -812478238 -890662358 -812...
result:
ok 42267 lines
Test #6:
score: 5
Accepted
time: 169ms
memory: 20704kb
input:
96522 72863 1 2 612719089 1 3 968169699 1 4 398645957 2 5 516975723 5 6 31371358 4 7 690410841 2 8 837868237 1 9 676115806 2 10 425558371 7 11 103506794 9 12 789921969 7 13 41979822 9 14 464308873 7 15 697800604 10 16 759506938 15 17 433635301 17 18 115937622 14 19 760803962 15 20 463301679 18 21 81...
output:
123456789123456789 123456789123456789 -921228753 -921228753 -921228753 -921228753 -921228753 -921228753 -921228753 -921228753 -921228753 -921228753 -921228753 -917161119 -921228753 -921228753 -921228753 -921228753 -921228753 -917161119 -921228753 -921228753 -921228753 -921228753 -950348247 -95034824...
result:
ok 36463 lines
Test #7:
score: 5
Accepted
time: 246ms
memory: 21920kb
input:
95849 93578 1 2 724213915 2 3 632056275 2 4 633870634 3 5 159738121 5 6 570365964 2 7 522964354 4 8 156704752 7 9 778646925 6 10 950786247 5 11 908124349 9 12 530371 7 13 521718754 10 14 293333333 10 15 401810189 11 16 86490413 15 17 71133582 16 18 808926269 13 19 57632248 15 20 367851189 19 21 5962...
output:
-393393430 123456789123456789 -393393430 -393393430 420372902 -393393430 -403584063 -403584063 -20111772 -132429409 -393393430 -403584063 -729755391 -729755391 -729755391 -729755391 -729755391 -810282964 -810282964 -895571947 -895571947 -734654570 -895571947 -895571947 -895571947 -895571947 -8955719...
result:
ok 46826 lines
Test #8:
score: 5
Accepted
time: 137ms
memory: 22524kb
input:
85186 72426 1 2 113616527 2 3 570303262 3 4 75899361 4 5 902309418 5 6 58595263 6 7 28429146 7 8 791667497 8 9 874196303 9 10 276064865 10 11 451578346 11 12 515423464 12 13 723491917 13 14 452459391 14 15 360603864 15 16 400924199 16 17 625314605 17 18 733674016 18 19 588829228 19 20 238623725 20 2...
output:
123456789123456789 52763019 52763019 855423329 52763019 855423329 855423329 92874112 1138106824174 2511432335350 -623168284 -340532911 -794222328 -473800988 -794222328 -716577517 -794222328 -794222328 -794222328 -623168284 697561324375 -579783547 -716577517 -473800988 -716577517 -623168284 143924957...
result:
ok 36247 lines
Test #9:
score: 5
Accepted
time: 218ms
memory: 22096kb
input:
99054 86785 1 2 825491795 1 3 704229814 1 4 736875953 2 5 988031658 1 6 66068031 2 7 990625472 5 8 451912113 7 9 552789577 5 10 643190262 6 11 450377942 8 12 226250303 10 13 727016249 10 14 909096702 14 15 502923649 11 16 112413156 12 17 211715328 17 18 245198305 17 19 175353928 19 20 995512615 17 2...
output:
123456789123456789 123456789123456789 229917247836 7828431236373 2326193051 997341880 940486372735 138461159 138461159 720513347024 138461159 1738183729422 2411433020206 -79169544 -79169544 3240039867612 -79169544 -150718363 997341880 838582658773 -106849718 991436451 -947429785 -628944785 -94742978...
result:
ok 43411 lines
Test #10:
score: 5
Accepted
time: 203ms
memory: 19200kb
input:
69293 78616 1 2 312295367 2 3 870342640 1 4 953893827 3 5 506731286 2 6 188498935 4 7 858458916 2 8 988886675 2 9 18334699 6 10 200623369 10 11 691470460 7 12 514488841 8 13 298030888 8 14 305577861 14 15 446524289 9 16 850996398 16 17 68662358 15 18 822619124 10 19 766293945 15 20 7895817 13 21 415...
output:
123456789123456789 675764223 2006647444 168310698 206798118761 675764223 2006647444 2865190628 44932917592 2006647444 -797293871 168310698 2006647444 168310698 1707036313 168310698 168310698 1162104914 630417226390 -39963121 -39963121 58363281 1932087551 -39963121 -39963121 633444636398 -39963121 -3...
result:
ok 39344 lines
Test #11:
score: 5
Accepted
time: 147ms
memory: 24216kb
input:
92208 89719 1 2 162186044 2 3 401617070 3 4 815251901 4 5 549306046 5 6 545779964 6 7 939501365 7 8 384460827 8 9 260382932 9 10 700665722 10 11 484682651 11 12 963993786 12 13 762794189 13 14 239420093 14 15 583313641 15 16 23904675 16 17 476668337 17 18 767647949 18 19 497490470 19 20 967526621 20...
output:
-68536083023216873 -233877009962513949 -162459111915902393 -140373439645411091 -233877009962513949 -162383169294359033 -159213690348701591 -243274269473439177 -241790874223911865 -252353550966273769 -209059094989100905 -233877009962513949 -233877009962513949 -233877009962513949 -282333757955750271 -...
result:
ok 44906 lines
Test #12:
score: 5
Accepted
time: 151ms
memory: 23452kb
input:
88347 78381 1 2 758156996 2 3 428685186 3 4 537748497 4 5 686982893 5 6 924976644 6 7 741189430 7 8 740655836 8 9 77436990 9 10 688870938 10 11 908398372 11 12 798673537 12 13 663752267 13 14 691770897 14 15 359702868 15 16 911644170 16 17 111314206 17 18 532770542 18 19 88174540 19 20 341334142 20 ...
output:
-141790329245667193 -141790329245667193 -114357127148372863 -36265088180874225 -6512564325226777 -141790329245667193 -81731252769182335 -62341387942769140 -97265697491941177 -119238217192666720 -78791589367155616 -81067570966655548 -90563756894786543 -141790329245667193 -90563756894786543 -133827550...
result:
ok 39223 lines
Test #13:
score: 5
Accepted
time: 146ms
memory: 24524kb
input:
98791 71578 1 2 425618902 2 3 632992803 3 4 101675445 4 5 528910211 5 6 693136247 6 7 619317998 7 8 68160315 8 9 94134820 9 10 56833124 10 11 430006312 11 12 201144210 12 13 531576121 13 14 936690342 14 15 766260953 15 16 59750594 16 17 281900489 17 18 903220806 18 19 146098185 19 20 495698644 20 21...
output:
123456789123456789 -462937417 63608637218295787 3912972464505638 123456789123456789 23317719787419612 7608296071877027 -7145317222929336 -7145317222929336 -462937417 -139954416215246753 -139954416215246753 16289363648891625 -139954416215246753 -153492646716134073 -121798341376343153 -139954416215246...
result:
ok 35840 lines
Test #14:
score: 5
Accepted
time: 252ms
memory: 21620kb
input:
81948 99630 1 2 978363130 2 3 590488360 2 4 356534116 4 5 543349366 3 6 87081307 2 7 287709634 2 8 209831056 8 9 847383133 8 10 599770613 4 11 910125785 9 12 350376379 9 13 119866629 10 14 710042451 14 15 305087476 11 16 918362476 10 17 798119413 10 18 156663290 13 19 22340763 15 20 290042412 20 21 ...
output:
123456789123456789 123456789123456789 123456789123456789 123456789123456789 123456789123456789 37238892059110217 1044271645228089 -1792456556620680 -1451538897332137 -1451538897332137 -1996459460362462 -1703869188742006 9232159456171852 21578500762992 -11035949780710033 -11035949780710033 -110359497...
result:
ok 49842 lines
Test #15:
score: 5
Accepted
time: 146ms
memory: 19096kb
input:
75607 67861 1 2 140958323 2 3 648629370 3 4 578885922 3 5 568891409 3 6 115000801 3 7 203078305 2 8 772395090 7 9 30307691 6 10 348766549 9 11 134698735 11 12 664689419 9 13 964300015 12 14 261610198 11 15 348298349 12 16 897552726 11 17 99299239 14 18 841575532 18 19 958292001 15 20 923747642 16 21...
output:
-14820775250805260 -14820775250805260 -183290739687917 -7148208280668060 -103434563402120 -7963337166842616 -6776860047355912 145680536187256 123456789123456789 -11335736192681 -5659996929574264 -50617223557871067 -50617223557871067 -40827915265231019 -37110647245154997 -48056796975839467 -371106472...
result:
ok 33955 lines
Test #16:
score: 5
Accepted
time: 172ms
memory: 18916kb
input:
72470 68330 1 2 682557977 2 3 182714025 1 4 269276963 2 5 71336297 4 6 881041362 6 7 365830567 7 8 223173032 7 9 405767813 6 10 375389541 4 11 864749667 7 12 938910988 6 13 139210466 9 14 463003612 9 15 387283172 10 16 342132823 12 17 966519945 15 18 318277182 17 19 728680754 14 20 178049170 20 21 6...
output:
123456789123456789 123456789123456789 123456789123456789 123456789123456789 21228434279118433 350874041799271 -25035164659470263 854263378637 854263378637 -37847670136438215 -20629099623427090 -36008770605421514 -27742344034194339 -37847670136438215 -21766537104430864 123456789123456789 -34799584808...
result:
ok 34213 lines
Test #17:
score: 5
Accepted
time: 229ms
memory: 20072kb
input:
70103 87506 1 2 957510498 1 3 234988918 2 4 992514215 4 5 429459788 1 6 64628211 3 7 977914842 4 8 246166391 3 9 954340594 9 10 635088549 10 11 413735331 9 12 892407854 9 13 329527359 6 14 392080899 5 15 908402264 6 16 711407443 9 17 463999165 11 18 170759314 14 19 322599809 10 20 854367599 12 21 77...
output:
123456789123456789 -15030537934900554 -15030537934900554 -15030537934900554 -15030537934900554 -15030537934900554 -5499764705237459 -15030537934900554 -15030537934900554 -5018043240290699 -5884856071131309 123456789123456789 -15030537934900554 -15643414671086740 -7917808196752289 -23520786117032914 ...
result:
ok 43800 lines
Test #18:
score: 5
Accepted
time: 211ms
memory: 21068kb
input:
84542 90602 1 2 748385455 2 3 45577762 3 4 302811481 1 5 430049900 2 6 616566507 2 7 158936152 4 8 46700712 4 9 648926917 7 10 596701455 6 11 468533206 9 12 790024162 9 13 738983660 8 14 919272994 10 15 580748454 13 16 439535120 13 17 897803495 16 18 655198508 17 19 637916880 17 20 533408312 15 21 7...
output:
-54436574243627714 -54436574243627714 -26417848098431214 -54436574243627714 -54436574243627714 -49692048324088713 -54436574243627714 -54436574243627714 -54436574243627714 -24233278534004464 -28539149807823464 -54436574243627714 -45047006344891714 -54436574243627714 -54436574243627714 -54436574243627...
result:
ok 45337 lines
Test #19:
score: 5
Accepted
time: 213ms
memory: 19548kb
input:
68920 84288 1 2 899373396 1 3 440607300 3 4 618404037 2 5 151683817 2 6 819856882 6 7 227853529 7 8 797716169 4 9 740399525 9 10 56313470 7 11 445215262 7 12 733645184 7 13 823566421 12 14 329493376 11 15 625627035 14 16 532050801 12 17 703448705 15 18 416812417 16 19 191969497 14 20 993227556 18 21...
output:
-20435160017126160 25093484842396299 -36060568977559810 -19816343998544449 -42989316559088049 -19578877733166915 -44242647194151293 -20187149998577262 -47193820510193277 -47193820510193277 -21305385675606254 28015736920152 -21069286843567200 -39204140607153273 -35083426353697074 -23709242026411150 -...
result:
ok 42184 lines
Test #20:
score: 5
Accepted
time: 224ms
memory: 20428kb
input:
92498 72436 1 2 237971197 2 3 94333458 1 4 711259332 1 5 269886923 1 6 140112215 3 7 450644873 7 8 812337400 1 9 892512558 5 10 351042856 2 11 157308890 9 12 616270504 7 13 703985646 5 14 335543161 6 15 957705995 11 16 493526235 16 17 551129619 16 18 768591697 11 19 939104038 17 20 918657178 15 21 6...
output:
-992973731 -992973731 -992973731 -992973731 26221351049583 5325629596199544 26221351049583 26221351049583 -5071564377432752 -1431360856483247 -11551357062557316 -15415449564832937 -16172098268576012 -14441177297558532 -16172098268576012 -16172098268576012 -5071564377432752 -16172098268576012 -161720...
result:
ok 36249 lines