Submission #476646

# Submission time Handle Problem Language Result Execution time Memory
476646 2021-09-27T23:40:22 Z Ozy Dynamic Diameter (CEOI19_diameter) C++17
24 / 100
213 ms 42436 KB
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define rep(i,a,b) for (int i = (a); i <= (b); i++)
#define repa(i,a,b) for (int i = (a); i >= (b); i--)
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "

#define MAX 100000
#define des first
#define val second
#define ini first
#define fin second

struct x{
    lli a;
    lli b;
    lli ab;
    lli ba;
    lli aba;
    lli lazy;
};

lli n,q,w,a,b,c,offset,last,nuevo,in,fi,act;
lli euleriano[MAX*3],prof[MAX*3],aristas[MAX+2][3];
pair<lli,lli> rango[MAX+2];
vector<pair<lli,lli> > hijos[MAX+2];
x st[MAX*5];

void combina(lli nodo) {

    lli izq = nodo*2;
    lli der = (nodo*2)+1;

    st[nodo].a = max(st[izq].a, st[der].a);
    st[nodo].b = max(st[izq].b, st[der].b);

    st[nodo].ab = max({st[izq].ab, st[der].ab, (st[izq].a + st[der].b)});
    st[nodo].ba = max({st[izq].ba, st[der].ba, (st[izq].b + st[der].a)});

    st[nodo].aba = max({st[izq].aba, st[der].aba, (st[izq].ab + st[der].a), (st[izq].a + st[der].ba)});
}

void DFS(lli pos, lli padre, lli p) {

    prof[act] = p;
    euleriano[act] = pos;
    act++;

    for (auto h : hijos[pos]) {
        if (h.des == padre) continue;

        DFS(h.des,pos,p+h.val);

        prof[act] = p;
        euleriano[act] = pos;
        act++;
    }
}

void aplica(lli pos) {
    if (st[pos].lazy != 0) {

        st[pos].a += st[pos].lazy;
        st[pos].b -= 2*st[pos].lazy;
        st[pos].ab -= st[pos].lazy;
        st[pos].ba -= st[pos].lazy;

        if (pos <= offset) {
            st[pos*2].lazy += st[pos].lazy;
            st[(pos*2)+1].lazy += st[pos].lazy;
        }

        st[pos].lazy = 0;
    }
}

void actualiza(lli pos, lli ini, lli fin, lli l, lli r, lli sum) {

    aplica(pos);

    if (ini > r || fin < l) return;
    else if (ini >= l && fin <= r) {
        st[pos].lazy = sum;
        aplica(pos);
    }
    else {

        lli mitad = (ini+fin)/2;
        actualiza(pos*2, ini, mitad, l, r, sum);
        actualiza((pos*2)+1, mitad+1, fin, l, r, sum);

        combina(pos);

    }

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> q >> w;
    rep(i,1,n-1) {

        cin >> a >> b >> c;
        aristas[i][0] = a;
        aristas[i][1] = b;
        aristas[i][2] = c;

        hijos[a].push_back({b,c});
        hijos[b].push_back({a,c});
    }

    //llenar euleriano y profundidad
    act = 1;
    DFS(1,0,0);
    act--;
    //largo de ST es 1 - act
    rep(i,1,act) {
        rango[euleriano[i]].fin = i;
        if (rango[euleriano[i]].ini == 0) rango[euleriano[i]].ini = i;
    }

    offset = 1;
    while (offset < act) offset *= 2;
    offset--;

    rep(i,1,act) {
        st[offset+i].a = prof[i];
        st[offset+i].b = -(2*prof[i]);
        st[offset+i].ab = -prof[i];
        st[offset+i].ba = -prof[i];
        st[offset+i].aba = 0;
    }

    repa(i,offset,1) combina(i);

    last = 0;
    rep(i,1,q){
        cin >> a >> b;
        a = (a+last)%(n-1);
        a++;
        c = (b+last)%w;

        nuevo = c - aristas[a][2];
        aristas[a][2] = c;

        b = aristas[a][1];
        a = aristas[a][0];

        in = max(rango[a].ini, rango[b].ini);
        fi = min(rango[a].fin, rango[b].fin);

        actualiza(1,1,offset+1,in,fi,nuevo);

        last = st[1].aba;
        cout << last << "\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2704 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2680 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2676 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2704 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2680 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2676 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2680 KB Output is correct
19 Correct 6 ms 3020 KB Output is correct
20 Correct 5 ms 3020 KB Output is correct
21 Correct 6 ms 3020 KB Output is correct
22 Correct 7 ms 3148 KB Output is correct
23 Correct 8 ms 4684 KB Output is correct
24 Correct 10 ms 4692 KB Output is correct
25 Correct 9 ms 4684 KB Output is correct
26 Correct 12 ms 5124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 9 ms 2892 KB Output is correct
5 Correct 39 ms 3788 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2764 KB Output is correct
8 Correct 2 ms 2764 KB Output is correct
9 Correct 3 ms 2816 KB Output is correct
10 Correct 11 ms 3104 KB Output is correct
11 Correct 51 ms 4112 KB Output is correct
12 Correct 4 ms 4556 KB Output is correct
13 Correct 4 ms 4608 KB Output is correct
14 Correct 6 ms 4556 KB Output is correct
15 Correct 15 ms 4872 KB Output is correct
16 Correct 72 ms 5984 KB Output is correct
17 Incorrect 45 ms 37244 KB Output isn't correct
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3020 KB Output is correct
2 Correct 9 ms 3148 KB Output is correct
3 Correct 37 ms 3604 KB Output is correct
4 Correct 65 ms 4308 KB Output is correct
5 Correct 8 ms 6476 KB Output is correct
6 Correct 15 ms 6604 KB Output is correct
7 Correct 48 ms 7284 KB Output is correct
8 Correct 92 ms 8184 KB Output is correct
9 Correct 28 ms 20520 KB Output is correct
10 Correct 39 ms 20676 KB Output is correct
11 Correct 76 ms 21368 KB Output is correct
12 Correct 126 ms 22132 KB Output is correct
13 Incorrect 53 ms 38484 KB Output isn't correct
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 213 ms 42436 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2704 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2680 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2676 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2680 KB Output is correct
19 Correct 6 ms 3020 KB Output is correct
20 Correct 5 ms 3020 KB Output is correct
21 Correct 6 ms 3020 KB Output is correct
22 Correct 7 ms 3148 KB Output is correct
23 Correct 8 ms 4684 KB Output is correct
24 Correct 10 ms 4692 KB Output is correct
25 Correct 9 ms 4684 KB Output is correct
26 Correct 12 ms 5124 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2636 KB Output is correct
29 Correct 2 ms 2636 KB Output is correct
30 Correct 9 ms 2892 KB Output is correct
31 Correct 39 ms 3788 KB Output is correct
32 Correct 2 ms 2636 KB Output is correct
33 Correct 2 ms 2764 KB Output is correct
34 Correct 2 ms 2764 KB Output is correct
35 Correct 3 ms 2816 KB Output is correct
36 Correct 11 ms 3104 KB Output is correct
37 Correct 51 ms 4112 KB Output is correct
38 Correct 4 ms 4556 KB Output is correct
39 Correct 4 ms 4608 KB Output is correct
40 Correct 6 ms 4556 KB Output is correct
41 Correct 15 ms 4872 KB Output is correct
42 Correct 72 ms 5984 KB Output is correct
43 Incorrect 45 ms 37244 KB Output isn't correct
44 Halted 0 ms 0 KB -