Submission #1290234

#TimeUsernameProblemLanguageResultExecution timeMemory
1290234vuquangsangConstruction of Highway (JOI18_construction)C++20
16 / 100
2094 ms14668 KiB
#include <bits/stdc++.h>
using namespace std;

#define     el "\n"
#define     FOR(i,a,b) for(int i = (a), _b = (b); i <= _b; i++)
#define     FORD(i,a,b) for(int i = (a), _b = (b); i >= _b; i--)
#define     pb push_back
#define     fi first
#define     se second
#define     all(x) x.begin(),x.end()
#define     lg(x) __lg(x)
#define     alla(a,n) a+1,a+n+1
#define     ll long long


template <class T> bool maxi(T &x, T y) { if(x < y) { x = y ; return true ;} return false;}
template <class T> bool mini(T &x, T y) { if(x > y) { x = y ; return true ;} return false;}

const int N = 1e5 + 2;

int n, a[N];
vector<int> adj[N];
pair<int, int> E[N];

void compress()
{
    vector<int> V;
    FOR(i, 1, n) V.push_back(a[i]);
    sort(all(V));
    V.resize(unique(all(V)) - V.begin());
    FOR(i, 1, n) {
        a[i] = lower_bound(all(V), a[i]) - V.begin() + 1;
    }
}
void inp()
{
    cin >> n;
    FOR(i, 1, n) cin >> a[i];
    FOR(i, 1, n - 1) {
        int x, y; cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
        E[i] = {x, y};
    }
    compress();
}


namespace subtask_1
{

    int h[N], par[N];

    void dfs(int u, int p)
    {
        for(int v : adj[u]) if(v != p) {
            h[v] = h[u] + 1;
            par[v] = u;
            dfs(v, u);
        }
    }

    int calc(int x)
    {
        int ans = 0;
        int cur = x;
        while(cur > 0) {
            if(a[cur] > a[x]) ans++;
            cur = par[cur];
        }
        return ans;
    }
    void slv()
    {
        dfs(1, 1);
        FOR(i, 1, n - 1) {
            int x = E[i].first, y = E[i].second;

            int cur = x;
            int ans = 0;
            while(cur > 0) {
                ans += calc(cur);
                cur = par[cur];
            }
            cur = x;
            while(cur > 0) {
                a[cur] = a[y];
                cur = par[cur];
            }
            cout << ans << el;
        }
    }
}

namespace subtask_12
{
    int bit[N];

    void upd(int x, int v)
    {
        for(; x <= n; x += x & -x) bit[x] += v;
    }
    int get(int x)
    {
        int ans = 0;
        for(; x >= 1; x -= x & -x) ans += bit[x];
        return ans;
    }
    int h[N], par[N];

    void dfs(int u, int p)
    {
        for(int v : adj[u]) if(v != p) {
            h[v] = h[u] + 1;
            par[v] = u;
            dfs(v, u);
        }
    }
    void slv()
    {
        dfs(1, 1);
        FOR(i, 1, n - 1) {
            int x = E[i].first, y = E[i].second;
            memset(bit, 0, sizeof bit);

            int ans = 0;
            int cur = x;
            while(cur > 0) {
                ans += get(a[cur] - 1);
                upd(a[cur], 1);
                cur = par[cur];
            }
            cur = x;
            while(cur > 0) {
                a[cur] = a[y];
                cur = par[cur];
            }
            cout << ans << el;
        }
    }
}
//
//namespace subtask_3
//{
//
//    int sz[N], h[N], par[N];
//
//    void dfs(int u, int p)
//    {
//        sz[u] = 1;
//        for(int v : adj[u]) if(v != p) {
//            h[v] = h[u] + 1;
//            par[v] = u;
//            dfs(v, u);
//            sz[u] += sz[v];
//        }
//    }
//    int head[N], chainID[N], mtc = 1, time = 0, pos[N], euler[N];
//
//    void hld(int u, int p)
//    {
//        if(!head[mtc]) {
//            head[mtc] = u;
//        }
//        chainID[u] = mtc;
//
//        euler[++time] = u;
//        pos[u] = time;
//
//        int ma = 0;
//        for(int v : adj[u]) if(v != p) {
//            if(!ma || sz[v] > sz[ma]) ma = v;
//        }
//        if(ma) hld(ma, u);
//
//        for(int v : adj[u]) if(v != p && v != ma) {
//            mtc++;
//            hld(v, u);
//        }
//    }
//
//}
//    void slv()
//    {
//        dfs(1, 1);
//        hld(1, 1);
//
//        FOR(i, 1, n - 1) {
//            int x = E[i].first, y = E[i].second;
//
//
//        }
//    }
//}

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

    #define __Azul__ "JOI18_construction"
    if(fopen(__Azul__".inp", "r")) {
        freopen(__Azul__".inp", "r", stdin);
        freopen(__Azul__".out", "w", stdout);
    }

    bool qs = 0;

    int T = 1;
    if(qs) cin >> T;
    while(T--) {
        inp();
        subtask_12::slv();
    }

    cerr << "\nTime" << 0.001 * clock() << "s "; return 0;


}





Compilation message (stderr)

construction.cpp:196:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  196 | main()
      | ^~~~
construction.cpp: In function 'int main()':
construction.cpp:202:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  202 |         freopen(__Azul__".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:203:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  203 |         freopen(__Azul__".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...