QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#125913#6322. ForestryEnergy_is_not_overAC ✓1363ms86444kbC++1710.8kb2023-07-17 21:50:502023-07-17 21:50:52

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-17 21:50:52]
  • 评测
  • 测评结果:AC
  • 用时:1363ms
  • 内存:86444kb
  • [2023-07-17 21:50:50]
  • 提交

answer

//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")

#ifdef __APPLE__
#include <iostream>
#include <cmath>
#include <algorithm>
#include <stdio.h>
#include <cstdint>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <bitset>
#include <map>
#include <queue>
#include <ctime>
#include <stack>
#include <set>
#include <list>
#include <random>
#include <deque>
#include <functional>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <complex>
#include <numeric>
#include <cassert>
#include <array>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <thread>
#else
#include <bits/stdc++.h>
#endif

#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

template<typename T>
bool umin(T &a, T b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
template<typename T>
bool umax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

#if __APPLE__
#define D for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define D while (false)
#define LOG(...)
#endif

const int max_n = 3e5+10, inf = 1000111222;

const int md = 998244353;

void inc(int& a,int b)
{
    a+=b;
    if (a>=md){
        a-=md;
    }
}

int id_in_sorted[max_n];
int value_of_sorted[max_n];

struct segment_tree {
    int f[4 * max_n];
    int sum[4 * max_n];
    int cnt_non_zero[4 * max_n];
    int sum_value_cnt[4 * max_n];

    void init()
    {
        for (int i=0;i<4*max_n;i++){
            f[i]=1;
        }
    }

    void push(int v) {
        if (f[v] != 1) {
            sum[2 * v] = 1ll * sum[2 * v] * f[v] % md;
            sum[2 * v + 1] = 1ll * sum[2 * v + 1] * f[v] % md;
            sum_value_cnt[2 * v] = 1ll * sum_value_cnt[2 * v] * f[v] % md;
            sum_value_cnt[2 * v + 1] = 1ll * sum_value_cnt[2 * v + 1] * f[v] % md;
            f[2 * v] = 1ll * f[2 * v] * f[v] % md;
            f[2 * v + 1] = 1ll * f[2 * v + 1] * f[v] % md;
            f[v] = 1;
        }
    }

    void update_mult(int v, int tl, int tr, int l, int r, int value) {
        if (tl == l && tr == r) {
            f[v] = 1ll * f[v] * value % md;
            sum[v] = 1ll * sum[v] * value % md;
            sum_value_cnt[v] = 1ll * sum_value_cnt[v] * value % md;
            return;
        }
        int mid = (tl + tr) / 2;
        push(v);
        if (r <= mid) {
            update_mult(2 * v, tl, mid, l, r, value);
        } else if (l > mid) {
            update_mult(2 * v + 1, mid + 1, tr, l, r, value);
        } else {
            update_mult(2 * v, tl, mid, l, mid, value);
            update_mult(2 * v + 1, mid + 1, tr, mid + 1, r, value);
        }
        sum[v] = sum[2 * v] + sum[2 * v + 1];
        if (sum[v]>=md){
            sum[v]-=md;
        }
        sum_value_cnt[v] = sum_value_cnt[2 * v] + sum_value_cnt[2 * v + 1];
        if (sum_value_cnt[v]>=md){
            sum_value_cnt[v]-=md;
        }
    }

    void update_element(int v, int tl, int tr, int pos, int value) {
        if (tl == tr) {
            LOG("element_updated");
            sum[v] = value;
            sum_value_cnt[v] = 1ll * value_of_sorted[pos] * value % md;
            cnt_non_zero[v]++;
            return;
        }
        int mid = (tl + tr) / 2;
        push(v);
        if (pos <= mid) {
            update_element(2 * v, tl, mid, pos, value);
        } else {
            update_element(2 * v + 1, mid + 1, tr, pos, value);
        }
        sum[v] = sum[2 * v] + sum[2 * v + 1];
        if (sum[v]>=md){
            sum[v]-=md;
        }
        sum_value_cnt[v] = sum_value_cnt[2 * v] + sum_value_cnt[2 * v + 1];
        if (sum_value_cnt[v]>=md){
            sum_value_cnt[v]-=md;
        }
        cnt_non_zero[v] = cnt_non_zero[2 * v] + cnt_non_zero[2 * v + 1];
    }

    int get_sum(int v, int tl, int tr, int l, int r) {
        if (tl == l && tr == r) {
            return sum[v];
        }
        int mid = (tl + tr) / 2;
        push(v);
        if (r <= mid) {
            return get_sum(2 * v, tl, mid, l, r);
        } else if (l > mid) {
            return get_sum(2 * v + 1, mid + 1, tr, l, r);
        }
        int res = get_sum(2 * v, tl, mid, l, mid) + get_sum(2 * v + 1, mid + 1, tr, mid + 1, r);
        if (res>=md){
            res-=md;
        }
        return res;
    }

    int get_sum_value_cnt(int v, int tl, int tr, int l, int r) {
        if (tl == l && tr == r) {
            return sum_value_cnt[v];
        }
        int mid = (tl + tr) / 2;
        push(v);
        if (r <= mid) {
            return get_sum_value_cnt(2 * v, tl, mid, l, r);
        } else if (l > mid) {
            return get_sum_value_cnt(2 * v + 1, mid + 1, tr, l, r);
        }
        int res = get_sum_value_cnt(2 * v, tl, mid, l, mid) + get_sum_value_cnt(2 * v + 1, mid + 1, tr, mid + 1, r);
        if (res>=md){
            res-=md;
        }
        return res;
    }

    void build_into_vector_and_clear(int v,int tl,int tr,vector<pii>& res) {
        if (cnt_non_zero[v]==0){
            return;
        }
        cnt_non_zero[v]=0;
        if (tl == tr) {
            res.pb(mp(tl,sum[v]));
            sum[v]=0;
            sum_value_cnt[v] = 0;
            return;
        }
        int mid = (tl + tr) / 2;
        push(v);
        build_into_vector_and_clear(2 * v, tl, mid, res);
        build_into_vector_and_clear(2 * v + 1, mid + 1, tr, res);
        sum[v]=0;
        sum_value_cnt[v] = 0;
    }
};

segment_tree st;

int n;

int a[max_n];

vector<int> reb[max_n];

int sz[max_n];

void dfs0(int now,int pred)
{
    sz[now]=1;
    for (auto wh:reb[now]){
        if (wh!=pred){
            dfs0(wh,now);
            sz[now]+=sz[wh];
        }
    }
}

int pw2[max_n];

int ans[max_n];
vector<pii> dp[max_n];

void build_state_from_current_st(vector<pii>& dp)
{
    st.build_into_vector_and_clear(1,0,n-1,dp);
}

void merge_state_into_st(int v,int son)
{
    D{
        LOG("merge_state_into_st",v,son);
        cerr<<"dp :: ";
        for (auto i:dp[son]){
            cerr<<value_of_sorted[i.fir]<<","<<i.sec<<" ";
        }
        cerr<<"\n";
        cerr<<"dp raw indices :: ";
        for (auto i:dp[son]){
            cerr<<i.fir<<","<<i.sec<<" ";
        }
        cerr<<"\n";
    }
    inc(ans[v],1ll*ans[son]*pw2[(sz[v]-1)-(sz[son]-1)]%md);

    vector<int> ways_a(len(dp[son]));
    for (int ii=0;ii<len(dp[son]);ii++){
        pii i=dp[son][ii];
        if (i.fir!=n-1){
            ways_a[ii]=st.get_sum(1,0,n-1,i.fir+1,n-1);
        }
    }

    /// edge == 0
    {
        for (auto i:dp[son]){
            inc(ans[v],1ll*value_of_sorted[i.fir]*i.sec%md*pw2[(sz[v]-1)-(sz[son]-1)-1]%md);
        }
        LOG("edge == 0","finished");
    }

    /// edge == 1
    {
        int last_updated=n;
        reverse(all(dp[son]));
        int current_ways=pw2[sz[son]-1];
        for (auto i:dp[son]){
            LOG("edge == 1, doing first",i.fir,i.sec);
            if (i.fir+1<=last_updated-1){
                LOG("update_mult on subseg",i.fir+1,last_updated-1,current_ways);
                st.update_mult(1,0,n-1,i.fir+1,last_updated-1,current_ways);
                last_updated=i.fir+1;
            }
            inc(current_ways,i.sec);
        }
        if (0<=last_updated-1){
            LOG("update_mult on subseg",0,last_updated-1,current_ways);
            st.update_mult(1,0,n-1,0,last_updated-1,current_ways);
            last_updated=0;
        }
        reverse(all(dp[son]));
        LOG("edge == 1","finished");
    }


    for (int ii=0;ii<len(dp[son]);ii++){
        pii i=dp[son][ii];
        st.update_element(1,0,n-1,i.fir,1ll*ways_a[ii]*i.sec%md);
    }
}

void dfs1(int now,int pred)
{
    LOG("dfs1",now,pred);
    sort(all(reb[now]),[&](const int& lhs,const int& rhs){
        return sz[lhs]>sz[rhs];
    });
    if (pred!=-1){
        reb[now].erase(reb[now].begin());
    }

    if (len(reb[now])==0){
        LOG(now,"leaf",id_in_sorted[now]);
        st.update_element(1,0,n-1,id_in_sorted[now],1);
        return;
    }

    for (int i=1;i<len(reb[now]);i++){
        dfs1(reb[now][i],now);
        build_state_from_current_st(dp[reb[now][i]]);
    }

    dfs1(reb[now][0],now);
    /// update_root_into_st
    {
        const int v=now;
        const int son=reb[now][0];
        LOG("base v son",v,son);
        inc(ans[v],1ll*ans[son]*pw2[(sz[v]-1)-(sz[son]-1)]%md);

        {
            int st_sum_value_cnt=st.get_sum_value_cnt(1,0,n-1,0,n-1);
            LOG(v,son,st_sum_value_cnt);
            inc(ans[v],1ll*st_sum_value_cnt*pw2[(sz[v]-1)-(sz[son]-1)-1]%md);
            LOG("ans after this st_sum_value_cnt is ",ans[v]);
        }

        {
            int sum_greater_equal=st.get_sum(1,0,n-1,id_in_sorted[now],n-1);
            st.update_mult(1,0,n-1,id_in_sorted[now],n-1,0);
            LOG(v,(sum_greater_equal+pw2[sz[son]-1])%md);
            st.update_element(1,0,n-1,id_in_sorted[now],(sum_greater_equal+pw2[sz[son]-1])%md);
        }
    }

    LOG("before merges",now,ans[now]);

    for (int i=1;i<len(reb[now]);i++){
        merge_state_into_st(now,reb[now][i]);
        LOG("after merge with",reb[now][i],now,ans[now]);
    }

    LOG(now,ans[now]);
}

int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    pw2[0]=1;
    for (int i=1;i<max_n;i++){
        pw2[i]=1ll*pw2[i-1]*2%md;
    }

    cin>>n;
    vector<pii> aa(n);
    for (int i=0;i<n;i++){
        cin>>a[i];
        aa[i]=mp(a[i],i);
    }
    sort(all(aa));
    for (int i=0;i<n;i++){
        id_in_sorted[aa[i].sec]=i;
        value_of_sorted[i]=aa[i].fir;
    }
    for (int i=1;i<n;i++){
        int u,v;
        cin>>u>>v;
        u--;
        v--;
        reb[u].pb(v);
        reb[v].pb(u);
    }
    dfs0(0,-1);
    st.init();
    dfs1(0,-1);

    int answer=(ans[0]+st.get_sum_value_cnt(1,0,n-1,0,n-1))%md;
    LOG(st.get_sum_value_cnt(1,0,n-1,0,n-1));
    D{
        vector<pii> dp0;
        build_state_from_current_st(dp0);
        cerr<<"dp0 :: "<<"\n";
        for (auto i:dp0){
            cerr<<value_of_sorted[i.fir]<<" "<<i.sec<<"\n";
        }
    };
    cout<<answer<<"\n";
}

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 29928kb

input:

4
1 2 3 4
1 2
2 4
3 2

output:

44

result:

ok 1 number(s): "44"

Test #2:

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

input:

5
3 5 6 5 1
4 1
2 3
3 5
1 3

output:

154

result:

ok 1 number(s): "154"

Test #3:

score: 0
Accepted
time: 1236ms
memory: 68836kb

input:

278989
864394090 384799247 186936242 598547507 962916136 540758021 158527118 688236322 682301622 948660645 881768181 481432818 557201870 794956026 205262301 920739629 141926922 990361777 818811172 150579096 1032726 328924563 638044961 21740781 484574329 737977343 113738003 345289940 363021157 582495...

output:

434293031

result:

ok 1 number(s): "434293031"

Test #4:

score: 0
Accepted
time: 1301ms
memory: 69244kb

input:

287397
510502405 166707542 6543103 963754822 492178400 668790842 47929394 688150962 147460907 412966210 837275339 435773111 138898710 968087621 746522431 948665731 920170470 717898411 411586421 148909638 659985934 484520123 95176393 425866444 512660652 511428439 958021520 150660147 292591135 7320851...

output:

878336347

result:

ok 1 number(s): "878336347"

Test #5:

score: 0
Accepted
time: 1333ms
memory: 70028kb

input:

294100
74665954 206916315 273249696 386095808 903673441 622958780 983022845 464633239 86441267 729372644 689111719 644187811 334408092 398907453 597161881 558735093 189557855 477930470 242405983 95068877 16413783 608746253 761602925 18584327 934502624 975194906 309225637 343248456 653012994 23118382...

output:

712256085

result:

ok 1 number(s): "712256085"

Test #6:

score: 0
Accepted
time: 509ms
memory: 80648kb

input:

300000
606908308 171374011 595912591 940182632 431995844 726762958 256169397 447385236 984023043 528258150 35565776 27503639 983714715 313579533 8771592 967838717 206597371 821802152 910792624 238535529 923693575 376608809 519872194 981808980 991017798 212487460 780752406 913659452 464143746 1561438...

output:

847230777

result:

ok 1 number(s): "847230777"

Test #7:

score: 0
Accepted
time: 398ms
memory: 81160kb

input:

299999
782121216 968610368 25009662 478183585 274702257 773444727 687702634 507153995 474335571 202223165 211518854 313706139 936298699 536896304 702242243 590986189 912655919 596226813 737786290 917153157 990146015 268573292 418734072 3093396 94105534 834508206 491041607 939287910 88847044 81241220...

output:

41386982

result:

ok 1 number(s): "41386982"

Test #8:

score: 0
Accepted
time: 357ms
memory: 86444kb

input:

299999
471542727 799095206 509787451 592650193 362215487 588125595 604003616 881525850 755688267 236898505 532366496 67948692 232297132 929458994 431732351 274008699 164112665 690178118 571148044 408207186 453143828 959612302 878551481 584372849 894288979 552607930 610390281 409926978 15831307 61533...

output:

479741390

result:

ok 1 number(s): "479741390"

Test #9:

score: 0
Accepted
time: 415ms
memory: 79668kb

input:

299997
25665195 607081146 2312642 326822363 385524999 209349813 211617900 372403090 573561165 779096327 304738950 525680732 809703693 740379205 642786351 334319127 24588707 570042249 298675391 92525045 882182405 255387261 629708545 662677567 102298185 416640032 800837034 517838306 180083897 65300699...

output:

756252561

result:

ok 1 number(s): "756252561"

Test #10:

score: 0
Accepted
time: 457ms
memory: 78388kb

input:

299997
772601140 677850510 486293752 38381420 713140958 613588331 933478663 892594986 40965372 719820852 371692421 950213365 627891743 923082750 330248475 635152312 968671306 91954505 646645243 266673541 419892861 468231632 676898301 475369890 173394153 206234682 614218712 386714613 691986706 401543...

output:

909256779

result:

ok 1 number(s): "909256779"

Test #11:

score: 0
Accepted
time: 587ms
memory: 64972kb

input:

300000
489134030 28527536 706816680 898196399 298100900 573504624 817242924 202561288 93577170 123268581 618798194 660515267 184003900 797853604 19557769 572066362 795291782 626861802 147727524 877230625 221524908 456496916 339550982 405198767 983242265 794896971 192309413 938450729 890444371 624597...

output:

469866382

result:

ok 1 number(s): "469866382"

Test #12:

score: 0
Accepted
time: 1363ms
memory: 71624kb

input:

294048
940092908 294031597 203789774 965832063 294368312 530860757 29650208 19058325 908258800 103593701 42838422 924175435 729123020 53690401 645412066 388715095 613747643 989874415 525317950 992107991 110671090 174219963 334698216 629961584 722552204 684114784 157441276 972553266 734116607 6480471...

output:

869124246

result:

ok 1 number(s): "869124246"

Test #13:

score: 0
Accepted
time: 1360ms
memory: 71576kb

input:

294169
829764052 27629554 569458841 732262994 789440677 560991159 427506198 382533265 982599986 867072676 292427700 863455778 358352325 895409930 503249204 713052609 50315636 478399531 724940834 394303205 658023052 804043479 566808951 672917322 636437946 905749010 875678660 8659223 349546005 8117721...

output:

603293934

result:

ok 1 number(s): "603293934"

Test #14:

score: 0
Accepted
time: 1244ms
memory: 69344kb

input:

274214
936267909 35123462 801828019 44784239 618602008 669685848 531693333 374691550 641907521 460643008 895242266 178217314 865256487 210105123 791635414 102380409 605115311 319289854 173186400 301848535 470317014 68928307 385895216 80699081 351459001 505538791 208284669 800278979 952086428 4182918...

output:

344857557

result:

ok 1 number(s): "344857557"

Test #15:

score: 0
Accepted
time: 535ms
memory: 64940kb

input:

300000
300000 299999 299998 299997 299996 299995 299994 299993 299992 299991 299990 299989 299988 299987 299986 299985 299984 299983 299982 299981 299980 299979 299978 299977 299976 299975 299974 299973 299972 299971 299970 299969 299968 299967 299966 299965 299964 299963 299962 299961 299960 299959...

output:

8100800

result:

ok 1 number(s): "8100800"