QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#718035#9492. 树上简单求和yaoyanfeng22 5438ms52404kbC++143.5kb2024-11-06 19:33:052024-11-06 19:33:06

Judging History

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

  • [2024-11-06 19:33:06]
  • 评测
  • 测评结果:22
  • 用时:5438ms
  • 内存:52404kb
  • [2024-11-06 19:33:05]
  • 提交

answer

#include <bits/stdc++.h>
#define pii pair<int, int>
#define eb emplace_back
#define ull unsigned long long
using namespace std;
const int N = 200200, B = 470;
int n, m, s, t;
int p[N], q[N];
struct Tree{
    vector<int> to[N];
    int dfn[N], rnk[N], fa[N], nw, siz[N], son[N], top[N], dep[N];
    void add(int u, int v) {
        to[u].eb(v), to[v].eb(u);
    }
    void dfs1(int u, int dp) {
        siz[u] = 1, dep[u] = dp;
        for (int v : to[u]) if (v != fa[u]) {
            fa[v] = u, dfs1(v, dp + 1);
            if (!son[u] || siz[v] > siz[son[u]]) son[u] = v;
        }
        return;
    }
    void dfs2(int u, int tp) {
        top[u] = tp, dfn[u] = ++nw, rnk[nw] = u;
        if (son[u]) dfs2(son[u], tp);
        else return;
        for (int v : to[u]) if (v != fa[u] && v != son[u]) dfs2(v, v);
        return;
    }
    vector<pii> qr(int u, int v) {
        vector<pii> ans;
        while (top[u] != top[v]) {
            if (dep[top[u]] < dep[top[v]]) swap(u, v);
            ans.emplace_back(dfn[top[u]], dfn[u]), u = fa[top[u]];
        }
        if (dep[u] < dep[v]) swap(u, v);
        ans.emplace_back(dfn[v], dfn[u]);
        return ans;
    }
    void solve() {
        dfs1(1, 1), dfs2(1, 1);
    }
} tr1, tr2;
ull a[N], k, add[B], ct[B], sm[B][B], bl[B], br[B], frm[N], ret[N];
// add是对整块的贡献,ret是散块对散块,ct是整块对散块
void init() {
    for (int i = 1, x, y; i < n; ++i) scanf("%d%d", &x, &y), tr1.add(x, y);
    for (int i = 1, x, y; i < n; ++i) scanf("%d%d", &x, &y), tr2.add(x, y);
    tr1.solve(), tr2.solve();
    for (int i = 1; i <= n; ++i) p[i] = tr2.dfn[tr1.rnk[i]], q[tr2.dfn[tr1.rnk[i]]] = i;
    s = sqrt(n);
    for (int i = 1, j = s; i <= n; i = j + 1, j = min(n, j + s)) {
        bl[++t] = i, br[t] = j;
        for (int k = i; k <= j; ++k) frm[k] = t;
    }
    for (int i = 1; i <= n; ++i) ++sm[frm[i]][frm[p[i]]];
    for (int i = 1; i <= t; ++i) for (int j = 1; j <= t; ++j) sm[j][i] += sm[j - 1][i];
    for (int i = 1; i <= n; ++i) ret[tr2.dfn[i]] = a[i], add[frm[tr2.dfn[i]]] += a[i];
    return;
}
void modify(int l, int r, ull x) {
    if (frm[l] == frm[r]) {
        for (int i = l; i <= r; ++i) ret[p[i]] += x, add[frm[p[i]]] += x;
        return;
    }
    int L = frm[l], R = frm[r];
    for (int i = l; i <= br[L]; ++i) ret[p[i]] += x, add[frm[p[i]]] += x;
    for (int i = bl[R]; i <= r; ++i) ret[p[i]] += x, add[frm[p[i]]] += x;
    for (int i = L + 1; i < R; ++i) ct[i] += x;
    for (int i = 1; i <= t; ++i) add[i] += (sm[R - 1][i] - sm[L][i]) * x;
    return;
}
ull query(int l, int r) {
    ull ans = 0;
    if (frm[l] == frm[r]) {
        for (int i = l; i <= r; ++i) ans += ret[i] + ct[frm[q[i]]];
        return ans;
    }
    int L = frm[l], R = frm[r];
    for (int i = l; i <= br[L]; ++i) ans += ret[i] + ct[frm[q[i]]];
    for (int i = bl[R]; i <= r; ++i) ans += ret[i] + ct[frm[q[i]]];
    for (int i = L + 1; i < R; ++i) ans += add[i];
    return ans;
}
void solve(int x, int y, ull k) {
    vector<pii> o = tr1.qr(x, y);
    for (pii r : o) modify(r.first, r.second, k);
    ull ans = 0;
    o = tr2.qr(x, y);
    for (pii r : o) ans += query(r.first, r.second);
    printf("%llu\n", ans);
    return;
}
int main() {
    int tm1 = clock();
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i) scanf("%llu", a + i);
    init();
    for (int i = 1, x, y; i <= m; ++i) scanf("%d%d%llu", &x, &y, &k), solve(x, y, k);
    cerr << "use " << (clock() - tm1) << " ms\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 5
Accepted

Test #1:

score: 5
Accepted
time: 4ms
memory: 30728kb

input:

3000 3000
7236742292501328495 17973811477309806363 16075782662531676171 17971236571771878676 11392080645527132110 3685563455925680459 9773593720088356683 8313828403245053795 7736401634567449043 1634817828009987181 6951124933529719486 12775126714635387213 15460977209223753216 397573676785925632 31372...

output:

12105153858659381124
18367442707572066757
11668241962484097878
11288238120352358472
1742468310074622166
9942835997686093671
3305677510569607477
17741602000425004088
14984128302052618266
1075081718074605786
6509217537832509095
16750513627843273113
8569443169249732820
14475184194298579044
156111071108...

result:

ok 3000 lines

Test #2:

score: 5
Accepted
time: 11ms
memory: 26600kb

input:

3000 3000
1612333876155866602 8538417838700679227 6080765231437578796 17905224638340228394 12270907925903144224 17944105326358594564 17302041033966840611 1006351124625222126 496336153231744288 9393087977687876980 9553975238547373621 9361882717200384390 15051881329169144319 9757999873162420435 882725...

output:

11133131376095771981
7909873024850695144
16250639243139481926
14562550655578101207
8274205996508264973
178549413271904466
2368406276743327913
7464009386554813982
9439464815411774627
1471756740732097060
15201641099137019227
6774030298556871576
18156105511913219667
1553508745644446823
4225137078364117...

result:

ok 3000 lines

Test #3:

score: 5
Accepted
time: 18ms
memory: 28948kb

input:

3000 3000
9709246061666095435 1861649101703072889 10620139893353930613 17635186539135419482 710209455559527146 6075101384669982511 1120305006358459674 9703156967435388252 1397046737759839382 5259056712870179169 8253156305433022999 710199693203327302 15130650033641744675 10720111924616886955 15543351...

output:

7834604406305153073
5037061270969117785
16481572776620825702
15177894197606565804
3120320619896892806
18008650876379132344
7417108723176816402
13515164814425439399
3299769942258542105
15897528270699011770
11642805469843844638
16764682282380318054
4824039114054405772
4859834102876213962
1234210473247...

result:

ok 3000 lines

Test #4:

score: 5
Accepted
time: 13ms
memory: 30800kb

input:

3000 3000
16538965545220923528 18062192327708400751 10422465150728338588 3471522151129113073 1236650672072793692 1942240200040301168 13090729759591037952 15335798523677372669 9912100622761466753 11177948788405690381 3710859061697501523 4984944638666762977 17278589713462878008 6371292801024547050 868...

output:

8182453933067329108
13535217473847106938
17067385337010269798
3806121648880466130
11322569288575153037
11079197311131660121
9670138330007803226
6554062218199796758
965954569567598779
18055887214749050688
6142620503089407421
8690117812667761187
9547139298346295115
8890987597519353054
1755036654049586...

result:

ok 3000 lines

Test #5:

score: 5
Accepted
time: 12ms
memory: 30864kb

input:

3000 3000
17759588706587888497 10550000524636484378 11601004513528075994 7150322911283804521 4459707248078569712 10692395730842402625 8940418793863522991 12967068928670540447 9954278250450015940 13702413838608801301 10598390500439869870 15110245227553613794 490862872212325709 15164980555660957366 94...

output:

9743736929788175512
16812303667256960040
14694223512340829897
550204232580650311
1175342872438242313
17622261358285047637
7413682703975031220
12643066512274062227
1868985217436232595
5471830334855681322
8070132260376389587
3970361922096052085
218281824643752746
991917103472727104
2960248244218479023...

result:

ok 3000 lines

Subtask #2:

score: 0
Time Limit Exceeded

Dependency #1:

100%
Accepted

Test #6:

score: 12
Accepted
time: 6ms
memory: 28480kb

input:

5 7
0 3 2 6 4
1 2
2 4
1 5
5 3
3 4
4 2
2 5
5 1
5 3 0
3 2 5
4 4 4
4 4 3
5 2 0
3 4 3
5 5 6

output:

15
21
10
13
17
26
18

result:

ok 7 lines

Test #7:

score: 12
Accepted
time: 445ms
memory: 35568kb

input:

70000 70000
3805295436278888199 9842309351516174725 1566744796319231180 2206519284152256579 2715928675931950447 6346821976624501261 16020972671480798719 14702021753902144915 17127828773798978442 15779168055669690475 4964561323934614661 9395102787554964450 6377076753365184543 15167378195767668817 288...

output:

5971729064136092190
6457394048987305727
13604212649915736394
8639973959364892219
437861319070967556
16133076880026962355
5384937395694479961
4591478439775690843
16071919565966962790
15485626634068969082
10235993901046758372
3449528613427081475
8064280362779764074
12784984512326434905
424951714880051...

result:

ok 70000 lines

Test #8:

score: 12
Accepted
time: 5438ms
memory: 37660kb

input:

70000 70000
17769190865915081913 3772925482507158804 10559962993069063712 16307277356502651642 12014171661057147061 1923543107882042577 13408785599350410314 17786178374951015816 2038922879833426794 2540043772647346461 15419977514837351390 5175974305273838292 16815288359165841441 6295059675346852046 ...

output:

16215781699519408534
17067966839552063165
1639359200259068228
1157756671621253300
12850966537933214537
13917563606289473282
11146906493479190751
869141055866285398
529460535280965984
11437720548737856517
12321579881011015953
4005153170897692243
10217866116994297464
8892403813874757974
12520505236760...

result:

ok 70000 lines

Test #9:

score: 12
Accepted
time: 5063ms
memory: 36276kb

input:

70000 70000
1322605819855709761 1534349070722535975 3956030287626175223 12996546673549161162 7258680666490714729 15591023033141410544 11626890152249303179 7745771567168540351 5535931029756133379 11840793767439557739 6286106656048048381 9490665709724541446 4561258384162386434 2460318488748442222 1303...

output:

7565012138645637258
1080785033897684285
4000254219257999844
8727142139647715419
1784876728989450460
2474052717732723820
5108017366064709316
5232698473118606856
7893212823648229982
6449010654774296779
16571818815110297674
603759348329356530
7364528294111530037
4667545362378304836
3039728935129459889
...

result:

ok 70000 lines

Test #10:

score: 0
Time Limit Exceeded

input:

70000 70000
2918414982140182939 1004760492603077644 7526656799259998488 6665485253854847449 7752199419154649757 12763267823077347079 11745132191692540338 6726116817426709990 15550876907005962464 9760509858122842638 684733892856965421 10077915441058780247 8380400329996723109 16920573433866702239 3069...

output:

8230389499860859172
16425656898047941538
107743004356580170
9778122844868660722
11068387722102791183
13252614309136720348
15937842372230698728
12777338070107774364
17974062134369145560
3740400391792770609
7367804332878038809
14236246024207211797
5659238205278608512
10550373456364765526
3478082332928...

result:


Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Time Limit Exceeded

Test #21:

score: 14
Accepted
time: 2508ms
memory: 45184kb

input:

200000 200000
622783158027686223 2242697872372232537 8481648430436878777 10092474834140799044 15403999682625301609 12614289513474949582 9180944589267018841 7823784919308285798 8257785171198951273 5134508521895120821 8041682272181381093 3835432206618893170 2653803171409877650 5589823419153460372 1007...

output:

9042998055336671259
11611293489264521142
5835924579879681322
9187084356907537870
17810346410706951073
565636160725988981
837626748701483168
16059573289829807099
7246210357888652619
7725251776483176497
17088098442183693937
9042305714006927228
10907378739216215456
6526772063609981609
51578202456469609...

result:

ok 200000 lines

Test #22:

score: 0
Time Limit Exceeded

input:

200000 200000
13175752638648662841 17926176333479943540 18069418271192836667 7662981418770774166 17432280672869071045 9361466030141569604 17336291298429915451 758279154724011577 10229986883918215412 16695796270233481895 1104033984864960726 9768530369533627193 7121962912997584423 8574667967472399164 ...

output:

761007177180158471
99932139211644879
9085452500188024811
10579196290428182519
9823187704909577710
18023302821814112676
12490017484705421441
12628966555486388857
14265121989865566834
6520346880672680237
13101459183526308131
999924043939340162
18263995506773932901
5204528109864295202
12531805215875429...

result:


Subtask #5:

score: 17
Accepted

Test #27:

score: 17
Accepted
time: 1052ms
memory: 50800kb

input:

200000 200000
1958469220619413759 14991498002015735322 6054491201406941902 18206143187746582567 15082377615826460430 2936248617457291604 10073577150351675920 16534472678586906457 2207599132486246393 10301540360769075442 1492580560381080472 551692353431379140 13238280352539145808 8462626987240986565 ...

output:

11479812171669345085
7612644482907856514
7664363696211351499
10419050713553268082
7115244954460011045
9683711549165598600
15714069303067445091
5098969076555779384
17312050420753525411
13302302653999024684
15237835478514966949
1011923303415334401
15280591493481885526
11613220426756932450
109080667232...

result:

ok 200000 lines

Test #28:

score: 17
Accepted
time: 916ms
memory: 52404kb

input:

200000 200000
16779443803326674772 15639579352489825289 13969981610137062426 16505376510381344640 9806280160602498118 1419045977670621133 7677707894082613460 11957033082833096140 2771983498938827257 14706498164400422706 9378952791453329569 1930132295950861416 16111764090559108273 8905137311835789422...

output:

16293566981830212911
10264859857591337484
8994663676867609646
2754771179506593788
4767946170760042771
742819530451319825
2803285017809086810
3484130454853725644
7697600623078121691
5084812233611883732
3280787561128521192
1047502191383912852
14082348302073768112
2601331176748190671
117450452631909696...

result:

ok 200000 lines

Test #29:

score: 17
Accepted
time: 708ms
memory: 51676kb

input:

200000 200000
12029385893748061284 9172822681287286332 11779114870282057106 4849897936395853889 6103529715098530443 8797239236736271231 11618134370305604768 10908476427113524251 3933201111692274240 4499796948331538987 4232975400910660164 10415557794345312125 17360908993089799508 13708880030692860522...

output:

13101691483811142037
7858940297704268921
9131311782659594969
3979986497647305855
131040009215643795
10080455913202724993
13237896591702170157
4007300413168456185
10099014266690222675
16590528230541839393
10133194446659869919
16360073169922464941
15194261798246191631
5464643399868946391
1634883990603...

result:

ok 200000 lines

Test #30:

score: 17
Accepted
time: 1041ms
memory: 51376kb

input:

200000 200000
12279899037614140058 8888114948527866653 13928233147675322734 12454101025136386040 5288759580423671283 8776652717163613021 16449880332412915864 17287034922899709125 10532733829212727135 10764189288575553991 13953267646406847995 13908054688602304211 5827028562849173311 10886964702131020...

output:

16698510136139978210
16369094547606910925
1326550504815919356
15571631364851754526
15142726028067532669
16433307477502505346
2288796203808772846
14834334808701484355
11332372387379739662
16882038025761766711
15880577744881855798
3482331692875200083
3909893051643604341
17525221195783380334
1592129559...

result:

ok 200000 lines

Test #31:

score: 17
Accepted
time: 634ms
memory: 51112kb

input:

200000 200000
4893160298280825903 18198846065921817748 16343447781007974282 15723743432312964985 12871862738540236681 9972578954818260019 125153552836276808 5361924347833293579 11971958522882153451 6606998668153528639 9846700557419796517 924197471899377062 16510652970019285610 13527675083280389351 1...

output:

12217733345977585604
15090345895939064308
8800682550100929240
8418299123801754016
9906718252280231096
3019478245971491784
9496949828298647194
17471225676572414614
11441204915622920024
5759065639620250580
3072007946120216696
17839333347550122908
11855291417460815970
1574976372415972484
15048267748478...

result:

ok 200000 lines

Test #32:

score: 17
Accepted
time: 876ms
memory: 50824kb

input:

200000 200000
16269080113100378653 4484500057558985491 3155111047194780915 1955124878557628793 1131676785723263995 15670458790089679131 12642775067469645913 16058994799171671871 11128893081798077921 17630174838696618160 10509057764997030178 2736352128209806460 11214180975026520052 990613932293281058...

output:

12064924625133015846
16862321744372560028
1994646811747150992
10140722851099240116
2557503422311666754
15259878734280971978
3955576474907811510
3673505041702176640
7400377071018962046
12622228824601464114
10514727287268994992
17046945994663165568
3131278608817391298
7110396577833899144
2955850235837...

result:

ok 200000 lines

Test #33:

score: 17
Accepted
time: 1055ms
memory: 51244kb

input:

200000 200000
7793598924465250299 11320616179551810509 4514956344247028440 13595219380199005841 9022300443122747211 5306720789215014051 12829201090248291001 17484907120965865432 4426007998575926549 16938003364463208590 8456717373555158888 9856394533639307403 4263328592244098713 4497412961128822026 1...

output:

10753764993328700009
18007939111928197938
9749484091345535325
5504011101575545606
13062921586637652283
11960644823021452066
5934883515296641825
14816522537666728261
11155114586067595959
7338645965836498696
6416169899635462788
11575170211633743517
11478197945758314236
5225462134046697629
164046911152...

result:

ok 200000 lines

Subtask #6:

score: 0
Time Limit Exceeded

Test #34:

score: 0
Time Limit Exceeded

input:

200000 200000
6794776813641982926 1561596256197101737 10910039723053043515 7892247858295192798 12233819960547881004 17695389034783066733 9173201689566865598 17626618141377486739 7358781671024283919 6787559733384974662 3884392438269280436 14872846228351316833 9037842441501571648 14299818404271084016 ...

output:

5519324519442957729
13462861144392030499
8898301730697138469
4148979398311169421
15090246851327208067
8628707816538336707
13867297907038176506
10296428352441857103
15654304415409320656
7404566644919251615
9870876264015800597
6356224262148620783
249874952637342736
9023132497407647441
1416175985367538...

result:


Subtask #7:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%