Submission #413785

#TimeUsernameProblemLanguageResultExecution timeMemory
413785nicolaalexandraMagic Tree (CEOI19_magictree)C++14
36 / 100
2070 ms25532 KiB
#include <bits/stdc++.h>
#define DIM 100010
using namespace std;

int v[DIM],cost[DIM],d[DIM],a[DIM],w[DIM];
long long dp[DIM][1010];
vector <int> L[DIM];
set <pair<int,long long> > s[DIM];
set <int> aux;
int n,m,k,i,j,x,y;

void dfs (int nod, int tata){

    int ok = 0;
    for (auto vecin : L[nod])
        if (vecin != tata){
            ok = 1;
            dfs (vecin,nod);
        }

    if (!ok){
        if (v[nod]){
            s[nod].insert(make_pair(v[nod],cost[nod]));
            //for (int i=v[nod];i<=k;i++)
              //  dp[nod][i] = cost[nod];
        }

    } else {

        /// toti timpii ditincti
        aux.clear();
        for (auto vecin : L[nod]){
            if (vecin == tata)
                continue;
            for (auto it : s[vecin]){
                s[nod].insert(make_pair(it.first,0LL));
                aux.insert(it.first);
            }
        }

        for (auto timp : aux){

            for (auto vecin : L[nod]){
                if (vecin == tata || s[vecin].empty())
                    continue;

                set <pair<int,long long> > :: iterator it2 = s[vecin].lower_bound(make_pair(timp,0));
                if (it2 == s[vecin].end())
                    it2--;
                if (it2 != s[vecin].begin() && it2->first > timp)
                    it2--;
                if (it2->first <= timp){


                    set <pair<int,long long> > :: iterator it3 = s[nod].lower_bound(make_pair(timp,0));
                    long long val = it3->second + it2->second;

                    s[nod].erase(it3);
                    s[nod].insert(make_pair(timp,val));
                }
            }

        }

        if (v[nod]){
            /// dp[nod][d]

            if (aux.empty()){
                s[nod].insert(make_pair(v[nod],cost[nod]));
            } else {

                set <pair<int,long long> > :: iterator it = s[nod].lower_bound(make_pair(v[nod],0));
                if (it == s[nod].end())
                    it--;
                if (it != s[nod].begin() && it->first > v[nod])
                    it--;

                long long val = cost[nod];
                if (it->first <= v[nod])
                    val += it->second;

                if (it->first != v[nod])
                    s[nod].insert(make_pair(v[nod],val));


                for (auto it : aux)
                    if (it >= v[nod]){
                        set <pair<int,long long> > :: iterator it2 = s[nod].lower_bound(make_pair(it,0));
                        int nr = max (it2->second,val);

                        if (nr > it2->second){
                            s[nod].erase(it2);
                            s[nod].insert(make_pair(it,nr));
                        }
                    }
            }
        }

        for (auto vecin : L[nod])
            if (vecin != tata)
                s[vecin].clear();

    }

}

int cautare_binara (int v[], int n, int val){
    int st = 1, dr = n;
    while (st <= dr){
        int mid = (st+dr)>>1;
        if (v[mid] == val)
            return mid;
        if (v[mid] < val)
            st = mid+1;
        else dr = mid-1;
    }
}

int main (){

    //ifstream cin ("date.in");
    //ofstream cout ("date.out");

    cin>>n>>m>>k;

    int ok2 = 1; /// cazul cu lant
    for (i=2;i<=n;i++){
        cin>>x;
        L[x].push_back(i);
        L[i].push_back(x);

        if (x != i-1)
            ok2 = 0;
    }

    int ok = 1; long long sum = 0;
    for (i=1;i<=m;i++){
        cin>>x;
        cin>>v[x]>>cost[x];

        if (L[x].size() > 1)
            ok = 0;
        sum += cost[x];

        if (cost[x] != 1)
            ok2 = 0;
    }

    if (ok){
        cout<<sum;
        return 0;
    }

    if (ok2){
        int el = 0;
        for (i=1;i<=n;i++)
            if (v[i])
                a[++el] = v[i];
        reverse (a+1,a+el+1);
        int sz = 0;
        for (i=1;i<=el;i++){
            int st = 1, dr = sz;
            while (st <= dr){
                int mid = (st+dr)>>1;
                if (a[d[mid]] <= a[i])
                    st = mid+1;
                else dr = mid-1;
            }
            if (st == sz+1)
                sz++;
            d[st] = i;
        }

        cout<<sz;


        return 0;
    }

    /// normalizez v urii
    int el = 0;
    for (i=1;i<=n;i++)
        if (v[i])
            w[++el] = v[i];

    sort (w+1,w+el+1);

    int el2 = 1;
    for (i=2;i<=m;i++)
        if (w[i] != w[i-1])
            w[++el2] = w[i];
    el = el2;

    for (i=1;i<=n;i++){
        if (!v[i])
            continue;
        int val = v[i];
        v[i] = cautare_binara (w,el,val);
    }

    k = el;


    dfs (1,0);


    long long sol = 0;
    for (auto it : s[1])
        sol = max (sol,it.second);

    cout<<sol;



    return 0;
}

Compilation message (stderr)

magictree.cpp: In function 'int cautare_binara(int*, int, int)':
magictree.cpp:117:1: warning: control reaches end of non-void function [-Wreturn-type]
  117 | }
      | ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...