QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#259649#6662. 기지 간소화actor0 49ms22320kbC++145.1kb2023-11-21 09:31:192023-11-21 09:31:20

Judging History

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

  • [2023-11-21 09:31:20]
  • 评测
  • 测评结果:0
  • 用时:49ms
  • 内存:22320kb
  • [2023-11-21 09:31:19]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define rep(i,l,r) for (int i = (l); i <= (r); i++)
#define per(i,r,l) for (int i = (r); i >= (l); i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define maxn(a, b) a = max(a, b)
#define minn(a, b) a = min(a, b)
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
const ll mod = 1e9+7;
ll rp(ll a,ll b) {ll res=1%mod;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
template<int MOD> struct mint {
    static const int mod = MOD;
    int v; explicit operator int() const { return v; }
    mint():v(0) {}
    mint(ll _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD);
        if (v < 0) v += MOD; }
    bool operator==(const mint& o) const {
        return v == o.v; }
    friend bool operator!=(const mint& a, const mint& b) { 
        return !(a == b); }
    friend bool operator<(const mint& a, const mint& b) { 
        return a.v < b.v; }
   
    mint& operator+=(const mint& o) { 
        if ((v += o.v) >= MOD) v -= MOD; 
        return *this; }
    mint& operator-=(const mint& o) { 
        if ((v -= o.v) < 0) v += MOD; 
        return *this; }
    mint& operator*=(const mint& o) { 
        v = int((ll)v*o.v%MOD); return *this; }
    mint& operator/=(const mint& o) { return (*this) *= inv(o); }
    friend mint pow(mint a, ll p) {
        mint ans = 1; assert(p >= 0);
        for (; p; p /= 2, a *= a) if (p&1) ans *= a;
        return ans; }
    friend mint inv(const mint& a) { assert(a.v != 0); 
        return pow(a,MOD-2); }
        
    mint operator-() const { return mint(-v); }
    mint& operator++() { return *this += 1; }
    mint& operator--() { return *this -= 1; }
    friend mint operator+(mint a, const mint& b) { return a += b; }
    friend mint operator-(mint a, const mint& b) { return a -= b; }
    friend mint operator*(mint a, const mint& b) { return a *= b; }
    friend mint operator/(mint a, const mint& b) { return a /= b; }
};
using mi = mint<mod>;
    
const int N = 250010;
int n;
mi ans, sum, tmp;
int sz[N], big[N], cnt[N], bigv[N];
set<PII> st;
vector<PII> e[N];

void calc(int u, int fa, int t) {

    auto add = [&](const int &v, const int &c) {
        sum += c * 1ll * v * (v + 1) / 2;
    };

    auto it = st.upper_bound(mp(u, 1e9));
    it--;
    if (u == it->fi) {
        auto le = it;
        if (le != st.begin()) {
            le--;
            add(it->se - it->fi + 1, -1);
            add(le->se - le->fi + 1, -1);
            st.erase(le);
            st.erase(it);
            st.insert(mp(le->fi, it->se));
            it = st.find(mp(le->fi, it->se));
            add(it->se - it->fi + 1, 1);
        }
    } else {
        add(it->se - it->fi + 1, -1);
        st.insert(mp(it->fi, u - 1));
        st.insert(mp(u, it->se));
        st.erase(it);
        add(u - it->fi, 1);
        add(it->se - u + 1, 1);
        it = st.find(mp(u, it->se));
    }

    if (u == it->se) {
        auto ri = it; ri++;
        if (ri != st.end()) {
            add(it->se - it->fi + 1, -1);
            add(ri->se - ri->fi + 1, -1);
            st.insert(mp(it->fi, ri->se));
            st.erase(ri);
            st.erase(it);
            it = st.find(mp(it->fi, ri->se));
            add(it->se - it->fi + 1, 1);
        }
    } else {
        add(it->se - it->fi + 1, -1);
        st.insert(mp(u + 1, it->se));
        st.insert(mp(it->fi, u));
        st.erase(it);
        add(it->se - u, 1);
        add(u - it->fi + 1, 1);
    }
    for (auto [v, w] : e[u]) {
        if (v != fa && v != t)
            calc(v, u, t);
    }
}

void dfs(int u, int fa, int val, bool keep) {
    for (auto [v, w] : e[u]) {
        if (v != fa && v != big[u]) 
            dfs(v, u, w, false);
    }
    sum = tmp;
    st.clear();
    st.insert({0, n - 1});
    if (~big[u]) dfs(big[u], u, bigv[u], true);
    calc(u, fa, big[u]);
    // printf("VAL: %d sum: %d\n", val, tmp - sum); for (auto [l, r] : st) printf("%d %d\n", l, r);
    ans += (tmp - sum) * val;
    if (!keep) {
        st.clear();
        st.insert({0, n - 1});
    }
}

void getsz(int u, int fa) {
    sz[u] = 1;
    big[u] = -1;
    for (auto [v, w] : e[u]) {
        if (v == fa) continue;
        getsz(v, u);
        sz[u] += sz[v];
        if (big[u] == -1 || sz[v] > sz[big[u]]) {
            big[u] = v;
            bigv[u] = w;
        }
    }
}

int maintenance_costs_sum(vector<int> U, vector<int> V, vector<int> W) {
    n = SZ(U) + 1;
    tmp = n * 1ll * (n + 1) / 2;
    rep(i,0,n-2) e[U[i]].eb(V[i], W[i]);
    getsz(0, 0);
    // rep(i,0,n-1) printf("%d\n", big[i]);
    st.insert({0, n - 1});
    dfs(0, 0, 0, false);
    return ans.v;
}

#if 0
int main() {
    cout << maintenance_costs_sum(VI{0, 2, 2, 0}, VI{2, 1, 4, 3}, VI{2, 3, 6, 5}) << endl;
    // cout << maintenance_costs_sum(VI{0}, VI{1}, VI{1}) << endl;
    return 0;
}
#endif 

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 10000kb

input:

2
1 0 1

output:

0

result:

wrong answer 1st lines differ - expected: '1', found: '0'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #36:

score: 0
Wrong Answer
time: 41ms
memory: 21160kb

input:

249494
137621 137622 1
198127 198128 1
117358 117359 1
155777 155778 1
112742 112743 1
235668 235669 1
20632 20622 1
41333 41334 1
170699 170692 1
130269 130268 1
137208 137207 1
37791 37767 1
130187 130178 1
89795 89817 1
91408 91359 1
243301 243574 1
22017 22018 1
116466 116467 1
160094 160095 1
4...

output:

0

result:

wrong answer 1st lines differ - expected: '714152529', found: '0'

Subtask #4:

score: 0
Wrong Answer

Test #48:

score: 0
Wrong Answer
time: 49ms
memory: 22320kb

input:

249876
133760 107716 545485826
57898 35556 542825636
159559 78814 588304799
9037 105623 735470824
242676 240955 258616989
58653 143501 194781066
36591 97835 699376285
95049 123298 35300031
91751 203920 865003045
53908 18382 376452723
211462 200538 638209824
48693 89388 132147210
238496 151742 322726...

output:

0

result:

wrong answer 1st lines differ - expected: '497361697', found: '0'

Subtask #5:

score: 0
Skipped

Dependency #1:

0%