Submission #104069

# Submission time Handle Problem Language Result Execution time Memory
104069 2019-04-03T22:05:21 Z bogdan10bos Designated Cities (JOI19_designated_cities) C++14
100 / 100
921 ms 55412 KB
/// <3 azilE
#include <bits/stdc++.h>

using namespace std;

//#define FILE_IO

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> p3i;

const int NMAX = 2e5;
const int LGMAX = 18;
const ll oo = 1LL << 60;

int N;
ll sol[NMAX + 5], dwn[NMAX + 5];
vector<p3i> edg[NMAX + 5];

void swapNodes(int x, int y)
{
    if(x == y)  return;
    swap(edg[x], edg[y]);
    for(int i = 1; i <= N; i++)
        for(auto &edge: edg[i])
        {
            if(edge.first == x) edge.first = y;
            else if(edge.first == y)    edge.first = x;
        }
}

void DFS(int nod, int fth = 0)
{
    dwn[nod] = 0;
    for(auto &edge: edg[nod])
        if(edge.first != fth)
        {
            DFS(edge.first, nod);
            dwn[nod] += dwn[edge.first] + edge.second.first;
        }
}

void DFS2(int nod, int fth = 0, ll cost = 0)
{
    ll mycost = cost;
    ll sumdwn = dwn[nod];
    sol[1] = min(sol[1], mycost + sumdwn);
    for(auto &edge: edg[nod])
        if(edge.first != fth)
            DFS2(edge.first, nod, mycost + sumdwn - dwn[edge.first] - edge.second.first + edge.second.second);
}

namespace Ezial
{
    ll dp[NMAX + 5];
    int who[NMAX + 5];
    ll sol2;
    int newroot;
    pii gods;

    ll value[NMAX + 5];
    int f[NMAX + 5], done[NMAX + 5];

    int I, ord[NMAX + 5], itv[NMAX + 5][2];

    class SegmentTree
    {
    public:
        int N, sti, dri, idd;
        ll val, ans;
        int id[4 * NMAX + 5];
        ll aint[4 * NMAX + 5], add[4 * NMAX + 5];

        void B(int nod, int st, int dr)
        {
            aint[nod] = add[nod] = 0;
            if(st == dr)
            {
                id[nod] = ord[st];
                return;
            }

            int mij = st + (dr - st) / 2;
            B(nod * 2, st, mij);
            B(nod * 2 + 1, mij + 1, dr);

            id[nod] = id[nod * 2];
        }

        void build(int _N) { N = _N; B(1, 1, N); }

        void lazy(int nod)
        {
            aint[nod * 2] += add[nod];
            add[nod * 2] += add[nod];
            aint[nod * 2 + 1] += add[nod];
            add[nod * 2 + 1] += add[nod];
            add[nod] = 0;
        }

        void U(int nod, int st, int dr)
        {
            if(sti <= st && dr <= dri)
            {
                aint[nod] += val;
                add[nod] += val;
                return;
            }

            int mij = st + (dr - st) / 2;
            lazy(nod);

            if(sti <= mij)  U(nod * 2, st, mij);
            if(mij < dri)   U(nod * 2 + 1, mij + 1, dr);

            if(aint[nod * 2] > aint[nod * 2 + 1])
            {
                aint[nod] = aint[nod * 2];
                id[nod] = id[nod * 2];
            }
            else
            {
                aint[nod] = aint[nod * 2 + 1];
                id[nod] = id[nod * 2 + 1];
            }
        }

        void update(int st, int dr, ll _val)
        {
            sti = st, dri = dr, val = _val;
            U(1, 1, N);
        }

        int query()
        {
            return id[1];
        }
    }aint;

    void DFS2(int nod, int fth = 0, ll cost = 0)
    {
        if((int)edg[nod].size() == 1)
        {
            who[nod] = nod;
            dp[nod] = 0;
            return;
        }

        ll min1 = oo, min2 = oo;
        int who1 = -1, who2 = -1;
        for(auto &edge: edg[nod])
            if(edge.first != fth)
            {
                DFS2(edge.first, nod, cost + dwn[nod] - dwn[edge.first] - edge.second.first + edge.second.second);

                ll nowcost = -dwn[edge.first] - edge.second.first + dp[edge.first];
                if(nowcost < min1)
                {
                    min2 = min1, who2 = who1;
                    min1 = nowcost, who1 = who[edge.first];
                }
                else if(nowcost < min2)
                    min2 = nowcost, who2 = who[edge.first];
            }

        dp[nod] = dwn[nod] + min1, who[nod] = who1;

        if(who2 != -1)
        {
            ll nowcost = cost + dwn[nod] + min1 + min2;
            if(nowcost < sol2)
            {
                sol2 = nowcost;
                newroot = nod;
                gods = {who1, who2};
            }
        }
    }

    void DFS3(int nod, int fth = 0)
    {
        f[nod] = fth;
        itv[nod][0] = ++I;
        ord[I] = nod;
        for(auto &edge: edg[nod])
            if(edge.first != fth)
            {
                DFS3(edge.first, nod);
                value[edge.first] = edge.second.first;
            }
        itv[nod][1] = I;
    }

    void goup(int nod)
    {
        done[nod] = 1;
        if(nod == 1)     return;
        goup(f[nod]);
    }

    void solve()
    {
        int root = 1;
        for(int i = 1; i <= N; i++)
            if((int)edg[i].size() > 1)
            {
                root = i;
                break;
            }
        swapNodes(1, root);

        DFS(1);
        sol2 = oo;
        DFS2(1);

        swapNodes(1, newroot);
        sol[2] = sol2;

        DFS3(1);

        goup(gods.first);
        goup(gods.second);

        aint.build(N);
        for(int i = 1; i <= N; i++)
            if(!done[i])    aint.update(itv[i][0], itv[i][1], value[i]);

        int cnt = 2;
        ll ans = sol2;
        while(1)
        {
            int nod = aint.query();
            if(done[nod])   return;

            while(!done[nod])
            {
                ans -= value[nod];
                aint.update(itv[nod][0], itv[nod][1], -value[nod]);
                done[nod] = 1;
                nod = f[nod];
            }

            cnt++;
            sol[cnt] = ans;
        }
    }
}

int main()
{
    #ifdef FILE_IO
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
    #endif

    scanf("%d", &N);
    for(int i = 1; i < N; i++)
    {
        int x, y, a, b;
        scanf("%d%d%d%d", &x, &y, &a, &b);
        edg[x].push_back({y, {a, b}});
        edg[y].push_back({x, {b, a}});
    }

    DFS(1);
    sol[1] = oo;
    DFS2(1);

    if(N > 2)   Ezial::solve();

    int Q;
    scanf("%d", &Q);
    for(int q = 1; q <= Q; q++)
    {
        int id;
        scanf("%d", &id);
        printf("%lld\n", sol[id]);
    }

    return 0;
}

Compilation message

designated_cities.cpp: In function 'int main()':
designated_cities.cpp:256:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &N);
     ~~~~~^~~~~~~~~~
designated_cities.cpp:260:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d%d", &x, &y, &a, &b);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:272:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &Q);
     ~~~~~^~~~~~~~~~
designated_cities.cpp:276:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &id);
         ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4960 KB Output is correct
2 Correct 6 ms 5116 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 6 ms 5120 KB Output is correct
5 Correct 6 ms 5120 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 6 ms 5120 KB Output is correct
10 Correct 6 ms 5120 KB Output is correct
11 Correct 6 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 757 ms 33656 KB Output is correct
3 Correct 557 ms 46328 KB Output is correct
4 Correct 639 ms 33656 KB Output is correct
5 Correct 780 ms 33584 KB Output is correct
6 Correct 752 ms 35768 KB Output is correct
7 Correct 607 ms 33828 KB Output is correct
8 Correct 601 ms 47096 KB Output is correct
9 Correct 548 ms 34852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 737 ms 33656 KB Output is correct
3 Correct 563 ms 48776 KB Output is correct
4 Correct 836 ms 33656 KB Output is correct
5 Correct 785 ms 33712 KB Output is correct
6 Correct 703 ms 36088 KB Output is correct
7 Correct 628 ms 34572 KB Output is correct
8 Correct 631 ms 42616 KB Output is correct
9 Correct 558 ms 34984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4960 KB Output is correct
2 Correct 6 ms 5116 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 6 ms 5120 KB Output is correct
5 Correct 6 ms 5120 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 6 ms 5120 KB Output is correct
10 Correct 6 ms 5120 KB Output is correct
11 Correct 6 ms 5120 KB Output is correct
12 Correct 6 ms 5120 KB Output is correct
13 Correct 9 ms 5496 KB Output is correct
14 Correct 8 ms 5624 KB Output is correct
15 Correct 9 ms 5496 KB Output is correct
16 Correct 11 ms 5500 KB Output is correct
17 Correct 10 ms 5504 KB Output is correct
18 Correct 9 ms 5504 KB Output is correct
19 Correct 10 ms 5580 KB Output is correct
20 Correct 10 ms 5504 KB Output is correct
21 Correct 10 ms 5500 KB Output is correct
22 Correct 9 ms 5376 KB Output is correct
23 Correct 9 ms 5376 KB Output is correct
24 Correct 9 ms 5504 KB Output is correct
25 Correct 9 ms 5632 KB Output is correct
26 Correct 11 ms 5504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 757 ms 33656 KB Output is correct
3 Correct 557 ms 46328 KB Output is correct
4 Correct 639 ms 33656 KB Output is correct
5 Correct 780 ms 33584 KB Output is correct
6 Correct 752 ms 35768 KB Output is correct
7 Correct 607 ms 33828 KB Output is correct
8 Correct 601 ms 47096 KB Output is correct
9 Correct 548 ms 34852 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 737 ms 33656 KB Output is correct
12 Correct 563 ms 48776 KB Output is correct
13 Correct 836 ms 33656 KB Output is correct
14 Correct 785 ms 33712 KB Output is correct
15 Correct 703 ms 36088 KB Output is correct
16 Correct 628 ms 34572 KB Output is correct
17 Correct 631 ms 42616 KB Output is correct
18 Correct 558 ms 34984 KB Output is correct
19 Correct 6 ms 4992 KB Output is correct
20 Correct 726 ms 33728 KB Output is correct
21 Correct 640 ms 55412 KB Output is correct
22 Correct 644 ms 38648 KB Output is correct
23 Correct 770 ms 40104 KB Output is correct
24 Correct 764 ms 38904 KB Output is correct
25 Correct 813 ms 40068 KB Output is correct
26 Correct 804 ms 38956 KB Output is correct
27 Correct 785 ms 40104 KB Output is correct
28 Correct 657 ms 41824 KB Output is correct
29 Correct 754 ms 40084 KB Output is correct
30 Correct 785 ms 39452 KB Output is correct
31 Correct 656 ms 40620 KB Output is correct
32 Correct 632 ms 49816 KB Output is correct
33 Correct 483 ms 41484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4960 KB Output is correct
2 Correct 6 ms 5116 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 6 ms 5120 KB Output is correct
5 Correct 6 ms 5120 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 6 ms 5120 KB Output is correct
10 Correct 6 ms 5120 KB Output is correct
11 Correct 6 ms 5120 KB Output is correct
12 Correct 6 ms 4992 KB Output is correct
13 Correct 757 ms 33656 KB Output is correct
14 Correct 557 ms 46328 KB Output is correct
15 Correct 639 ms 33656 KB Output is correct
16 Correct 780 ms 33584 KB Output is correct
17 Correct 752 ms 35768 KB Output is correct
18 Correct 607 ms 33828 KB Output is correct
19 Correct 601 ms 47096 KB Output is correct
20 Correct 548 ms 34852 KB Output is correct
21 Correct 7 ms 5120 KB Output is correct
22 Correct 737 ms 33656 KB Output is correct
23 Correct 563 ms 48776 KB Output is correct
24 Correct 836 ms 33656 KB Output is correct
25 Correct 785 ms 33712 KB Output is correct
26 Correct 703 ms 36088 KB Output is correct
27 Correct 628 ms 34572 KB Output is correct
28 Correct 631 ms 42616 KB Output is correct
29 Correct 558 ms 34984 KB Output is correct
30 Correct 6 ms 5120 KB Output is correct
31 Correct 9 ms 5496 KB Output is correct
32 Correct 8 ms 5624 KB Output is correct
33 Correct 9 ms 5496 KB Output is correct
34 Correct 11 ms 5500 KB Output is correct
35 Correct 10 ms 5504 KB Output is correct
36 Correct 9 ms 5504 KB Output is correct
37 Correct 10 ms 5580 KB Output is correct
38 Correct 10 ms 5504 KB Output is correct
39 Correct 10 ms 5500 KB Output is correct
40 Correct 9 ms 5376 KB Output is correct
41 Correct 9 ms 5376 KB Output is correct
42 Correct 9 ms 5504 KB Output is correct
43 Correct 9 ms 5632 KB Output is correct
44 Correct 11 ms 5504 KB Output is correct
45 Correct 6 ms 4992 KB Output is correct
46 Correct 726 ms 33728 KB Output is correct
47 Correct 640 ms 55412 KB Output is correct
48 Correct 644 ms 38648 KB Output is correct
49 Correct 770 ms 40104 KB Output is correct
50 Correct 764 ms 38904 KB Output is correct
51 Correct 813 ms 40068 KB Output is correct
52 Correct 804 ms 38956 KB Output is correct
53 Correct 785 ms 40104 KB Output is correct
54 Correct 657 ms 41824 KB Output is correct
55 Correct 754 ms 40084 KB Output is correct
56 Correct 785 ms 39452 KB Output is correct
57 Correct 656 ms 40620 KB Output is correct
58 Correct 632 ms 49816 KB Output is correct
59 Correct 483 ms 41484 KB Output is correct
60 Correct 6 ms 5120 KB Output is correct
61 Correct 761 ms 42656 KB Output is correct
62 Correct 598 ms 54540 KB Output is correct
63 Correct 684 ms 41464 KB Output is correct
64 Correct 793 ms 42872 KB Output is correct
65 Correct 816 ms 41336 KB Output is correct
66 Correct 742 ms 42592 KB Output is correct
67 Correct 742 ms 41464 KB Output is correct
68 Correct 773 ms 42904 KB Output is correct
69 Correct 829 ms 44536 KB Output is correct
70 Correct 921 ms 42364 KB Output is correct
71 Correct 884 ms 42028 KB Output is correct
72 Correct 720 ms 43604 KB Output is correct
73 Correct 728 ms 50632 KB Output is correct
74 Correct 516 ms 45732 KB Output is correct