Submission #532656

# Submission time Handle Problem Language Result Execution time Memory
532656 2022-03-03T14:50:21 Z mat50013 Dynamic Diameter (CEOI19_diameter) C++11
24 / 100
5000 ms 150872 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
const int NMAX(100005);
vector<pair<int, ll> > G[NMAX];
vector<int> frati[NMAX];
map<pair<int, int>, vector<pair<int, int> > > mp;
bool dead[NMAX], mark[NMAX], viz[NMAX];
int pas, dim, sz[NMAX], tip[NMAX], cnt, baza, mov, id[NMAX];
ll dist[NMAX], maxx[NMAX];

struct SEGTREE
{
    vector<ll> Arb, lazy;
    vector<int> in, out;
    int cati;
    inline void init(int n)
    {
        cati = n;
        int put = 1;
        while(put <= n)
            put <<= 1;
        put <<= 1;
        Arb.resize(put), in.resize(put), out.resize(put), lazy.resize(put << 1);
    }
    inline void build(int nod, int st, int dr)
    {
        if(st == dr)
        {
            Arb[nod] = dist[st];
            return;
        }
        int mij = (st + dr) / 2;
        build(2 * nod, st, mij);
        build(2 * nod + 1, mij + 1, dr);
        Arb[nod] = max(Arb[2 * nod], Arb[2 * nod + 1]);
    }
    inline void push(int nod){
        if(lazy[nod]){
            Arb[nod] += lazy[nod];
            lazy[2 * nod] += lazy[nod];
            lazy[2 * nod + 1] += lazy[nod];
            lazy[nod] = 0;
        }
    }
    inline void update(int nod, int st, int dr, int a, int b, int val){
        if(a <= st && dr <= b){
            lazy[nod] += val;
            push(nod);
            return;
        }
        push(nod);
        int mij = (st + dr) / 2;
        if(a <= mij)
            update(2 * nod, st, mij, a, b, val);
        if(b > mij)
            update(2 * nod + 1, mij + 1, dr, a, b, val);
        push(2 * nod), push(2 * nod + 1);
        Arb[nod] = max(Arb[2 * nod], Arb[2 * nod + 1]);
    }
    inline int query(int nod, int st, int dr, int a, int b){
        push(nod);
        if(a <= st && dr <= b)
            return Arb[nod];
        int mij = (st + dr) / 2;
        int rez1 = 0, rez2 = 0;
        if(a <= mij)
            rez1 = query(2 * nod, st, mij, a, b);
        if(b > mij)
            rez2 = query(2 * nod + 1, mij + 1, dr, a, b);
        return max(rez1, rez2);
    }
} interv[NMAX];

inline int getC(int nod)
{
    viz[nod] = 1;
    sz[nod] = 1;
    int maxx = 0;
    for(auto it: G[nod])
        if(!viz[it.first] && mark[it.first])
        {
            int c = getC(it.first);
            if(c != -1)
                return c;
            sz[nod] += sz[it.first];
            maxx = max(maxx, sz[it.first]);
        }
    maxx = max(maxx, dim - sz[nod]);
    if(maxx <= dim / 2)
        return nod;
    return -1;
}

inline void DFS(int nod)
{
    viz[nod] = 1;
    tip[nod] = cnt;
    for(auto it: G[nod])
        if(!viz[it.first] && mark[it.first])
            DFS(it.first);
}

inline void buildTree(int nod, int tata = -1)
{
    interv[baza].in[id[nod]] = ++mov;
    for(auto it: G[nod])
        if(tata != it.first && mark[it.first])
        {
            mp[ {min(it.first, nod), max(it.first, nod)}].push_back({id[it.first], baza});
            dist[mov + 1] = dist[interv[baza].in[id[nod]]] + it.second;
            buildTree(it.first, nod);
        }
    interv[baza].out[id[nod]] = mov;
}

inline void centroidDecomp(vector<int> nodes)
{
    if(nodes.size() == 1)
        return;
    int mr = 0;
    for(auto it: nodes)
        mark[it] = 1, sz[it] = tip[it] = 0, viz[it] = 0, id[it] = ++mr;

    dim = nodes.size();
    int cen = getC(nodes[0]);
    baza = cen;
    mov = dist[1] = 0;
    interv[baza].init(nodes.size());
    buildTree(cen);
    interv[baza].build(1, 1, nodes.size());

    for(auto it: nodes)
        viz[it] = 0;
    viz[cen] = 1;
    cnt = 0;
    for(auto it: G[cen])
        if(!viz[it.first] && mark[it.first])
        {
            frati[cen].push_back(id[it.first]);
            ++cnt;
            DFS(it.first);
        }
    vector<vector<int> > nex(cnt + 1);
    for(auto it: nodes)
        nex[tip[it]].push_back(it);

    for(auto it: nodes)
        mark[it] = 0, sz[it] = tip[it] = 0, viz[it] = 0;
    int nn = cnt;
    for(int i = 1; i <= nn; ++i)
        centroidDecomp(nex[i]);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    int n, q;
    ll w;
    cin >> n >> q >> w;

    vector<tuple<int, int, int> > Muchie;
    for(int i = 1; i < n; ++i)
    {
        int a, b;
        ll c;
        cin >> a >> b >> c;
        Muchie.push_back(make_tuple(min(a, b), max(a, b), c));
        G[a].push_back({b, c});
        G[b].push_back({a, c});
    }

    vector<int> noduri;
    for(int i = 1; i <= n; ++i)
        noduri.push_back(i);
    centroidDecomp(noduri);

    auto calc = [](int i)
    {
        vector<int> vec;
        for(auto it: frati[i])
        {
            int val = interv[i].query(1, 1, interv[i].cati, interv[i].in[it], interv[i].out[it]);
            vec.push_back(val);
        }
        sort(vec.begin(), vec.end(), greater<int>());
        int maxxx = 0;
        if(vec.size() > 1)
            maxxx = vec[0] + vec[1];
        else maxxx = vec[0];
        return maxxx;
    };

    for(int i = 1; i <= n; ++i)
        if(frati[i].size())
            maxx[i] = calc(i);

    ll last = 0;
    for(int i = 1; i <= q; ++i)
    {
        int d;
        ll e;
        cin >> d >> e;
        d = (d + last) % (n - 1);
        e = (e + last) % w;

        int x, y, c;
        tie(x, y, c) = Muchie[d];
        for(auto it: mp[ {x, y}]){
            interv[it.second].update(1, 1, interv[it.second].cati, interv[it.second].in[it.first], interv[it.second].out[it.first], e - c);
            maxx[it.second] = calc(it.second);
        }
        Muchie[d] = make_tuple(x, y, e);
        ll rz = 0;
        for(int j = 1; j <= n; ++j)
            rz = max(rz, maxx[j]);
        cout << rz << '\n';
        last = rz;
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 15180 KB Output is correct
2 Correct 8 ms 15196 KB Output is correct
3 Correct 8 ms 15180 KB Output is correct
4 Correct 8 ms 15160 KB Output is correct
5 Correct 8 ms 15180 KB Output is correct
6 Correct 8 ms 15124 KB Output is correct
7 Correct 8 ms 15180 KB Output is correct
8 Correct 8 ms 15180 KB Output is correct
9 Correct 8 ms 15180 KB Output is correct
10 Correct 8 ms 15180 KB Output is correct
11 Correct 7 ms 15196 KB Output is correct
12 Correct 8 ms 15180 KB Output is correct
13 Correct 8 ms 15212 KB Output is correct
14 Correct 10 ms 15164 KB Output is correct
15 Correct 8 ms 15280 KB Output is correct
16 Correct 9 ms 15188 KB Output is correct
17 Correct 9 ms 15180 KB Output is correct
18 Correct 9 ms 15232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 15180 KB Output is correct
2 Correct 8 ms 15196 KB Output is correct
3 Correct 8 ms 15180 KB Output is correct
4 Correct 8 ms 15160 KB Output is correct
5 Correct 8 ms 15180 KB Output is correct
6 Correct 8 ms 15124 KB Output is correct
7 Correct 8 ms 15180 KB Output is correct
8 Correct 8 ms 15180 KB Output is correct
9 Correct 8 ms 15180 KB Output is correct
10 Correct 8 ms 15180 KB Output is correct
11 Correct 7 ms 15196 KB Output is correct
12 Correct 8 ms 15180 KB Output is correct
13 Correct 8 ms 15212 KB Output is correct
14 Correct 10 ms 15164 KB Output is correct
15 Correct 8 ms 15280 KB Output is correct
16 Correct 9 ms 15188 KB Output is correct
17 Correct 9 ms 15180 KB Output is correct
18 Correct 9 ms 15232 KB Output is correct
19 Correct 34 ms 15964 KB Output is correct
20 Correct 35 ms 16076 KB Output is correct
21 Correct 35 ms 16192 KB Output is correct
22 Correct 30 ms 16204 KB Output is correct
23 Correct 77 ms 19944 KB Output is correct
24 Correct 85 ms 21228 KB Output is correct
25 Correct 88 ms 22132 KB Output is correct
26 Correct 81 ms 23532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 15180 KB Output is correct
2 Correct 8 ms 15180 KB Output is correct
3 Correct 10 ms 15200 KB Output is correct
4 Correct 32 ms 15428 KB Output is correct
5 Correct 126 ms 16236 KB Output is correct
6 Correct 8 ms 15128 KB Output is correct
7 Correct 9 ms 15320 KB Output is correct
8 Correct 17 ms 15308 KB Output is correct
9 Correct 95 ms 15392 KB Output is correct
10 Correct 896 ms 15640 KB Output is correct
11 Correct 4405 ms 16712 KB Output is correct
12 Correct 24 ms 17100 KB Output is correct
13 Correct 140 ms 17100 KB Output is correct
14 Correct 1215 ms 17136 KB Output is correct
15 Execution timed out 5035 ms 17320 KB Time limit exceeded
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 16076 KB Output is correct
2 Correct 43 ms 16196 KB Output is correct
3 Correct 181 ms 16696 KB Output is correct
4 Correct 346 ms 17480 KB Output is correct
5 Correct 52 ms 27044 KB Output is correct
6 Correct 201 ms 27256 KB Output is correct
7 Correct 770 ms 27952 KB Output is correct
8 Correct 1501 ms 28636 KB Output is correct
9 Correct 238 ms 81352 KB Output is correct
10 Correct 756 ms 81656 KB Output is correct
11 Correct 3022 ms 82276 KB Output is correct
12 Execution timed out 5045 ms 83048 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5073 ms 150872 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 15180 KB Output is correct
2 Correct 8 ms 15196 KB Output is correct
3 Correct 8 ms 15180 KB Output is correct
4 Correct 8 ms 15160 KB Output is correct
5 Correct 8 ms 15180 KB Output is correct
6 Correct 8 ms 15124 KB Output is correct
7 Correct 8 ms 15180 KB Output is correct
8 Correct 8 ms 15180 KB Output is correct
9 Correct 8 ms 15180 KB Output is correct
10 Correct 8 ms 15180 KB Output is correct
11 Correct 7 ms 15196 KB Output is correct
12 Correct 8 ms 15180 KB Output is correct
13 Correct 8 ms 15212 KB Output is correct
14 Correct 10 ms 15164 KB Output is correct
15 Correct 8 ms 15280 KB Output is correct
16 Correct 9 ms 15188 KB Output is correct
17 Correct 9 ms 15180 KB Output is correct
18 Correct 9 ms 15232 KB Output is correct
19 Correct 34 ms 15964 KB Output is correct
20 Correct 35 ms 16076 KB Output is correct
21 Correct 35 ms 16192 KB Output is correct
22 Correct 30 ms 16204 KB Output is correct
23 Correct 77 ms 19944 KB Output is correct
24 Correct 85 ms 21228 KB Output is correct
25 Correct 88 ms 22132 KB Output is correct
26 Correct 81 ms 23532 KB Output is correct
27 Correct 8 ms 15180 KB Output is correct
28 Correct 8 ms 15180 KB Output is correct
29 Correct 10 ms 15200 KB Output is correct
30 Correct 32 ms 15428 KB Output is correct
31 Correct 126 ms 16236 KB Output is correct
32 Correct 8 ms 15128 KB Output is correct
33 Correct 9 ms 15320 KB Output is correct
34 Correct 17 ms 15308 KB Output is correct
35 Correct 95 ms 15392 KB Output is correct
36 Correct 896 ms 15640 KB Output is correct
37 Correct 4405 ms 16712 KB Output is correct
38 Correct 24 ms 17100 KB Output is correct
39 Correct 140 ms 17100 KB Output is correct
40 Correct 1215 ms 17136 KB Output is correct
41 Execution timed out 5035 ms 17320 KB Time limit exceeded
42 Halted 0 ms 0 KB -