Submission #1337829

#TimeUsernameProblemLanguageResultExecution timeMemory
1337829adscodingMountains (NOI20_mountains)C++20
100 / 100
206 ms8648 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#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 FORLL(i, a, b) for (ll i = a, _b = b; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a, _b = b; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)

template<typename T>
void __print_one(const char *&s, const T &x)
{
    while (*s == ' ') ++s;
    const char *p = s;
    int bal = 0;
    while (*s)
    {
        if (*s == '(') ++bal;
        else if (*s == ')') --bal;
        else if (*s == ',' && bal == 0) break;
        ++s;
    }
    cerr.write(p, s - p) << " = " << x;
    if (*s == ',')
    {
        ++s;
        cerr << "  ,  ";
    }
}

template<typename... Args>
void debug(const char *s, Args... args)
{
    cerr << "[  ";
    int dummy[] = { 0 , ( __print_one(s, args) , 0 )... };
    (void)dummy;
    cerr << "  ]\n\n";
}

template<class X>
bool maximize(X &a, X b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

template<class X>
bool minimize(X &a, X b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}

// --------------------------------------------------------------------------------------------

const int maxn = 3e5 + 3;
int n;
ll H[maxn], P[maxn];
vector<ll> ids;

// --------------------------------------------------------------------------------------------

struct FenTree
{
    vector<int> fw;
    void init(int n)
    {
        fw.clear();
        fw.assign(n + 3, 0);
    }
    void upd(int pos, int val)
    {
        for (; pos < int(fw.size()); pos += pos & -pos)
            fw[pos] += val;
    }
    int get(int pos)
    {
        int res = 0;
        for (; pos; pos &= pos - 1)
            res += fw[pos];
        return res;
    }
};

int lower(ll x) {return lower_bound(all(ids), x) - ids.begin() + 1;}

void solve()
{
    cin >> n;
    FOR(i, 1, n)
    {
        cin >> H[i];
        ids.push_back(H[i]);
    }
    uni(ids);

    FenTree fw;
    fw.init(n);

    FOR(i, 1, n)
    {
        P[i] = fw.get(lower(H[i]) - 1);
        fw.upd(lower(H[i]), 1);
    }

    ll res = 0;
    fw.init(n);
    FORD(i, n, 1)
    {
        res += P[i] * fw.get(lower(H[i]) - 1);
        fw.upd(lower(H[i]), 1);
    }

    cout << res;
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    #define TASK "TEST"
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        freopen(TASK".OUT", "w", stdout);
    }
    solve();
    return 0;
}

Compilation message (stderr)

Mountains.cpp: In function 'int main()':
Mountains.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Mountains.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |         freopen(TASK".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...