Submission #1062381

#TimeUsernameProblemLanguageResultExecution timeMemory
1062381CodeLakVNPilot (NOI19_pilot)C++17
100 / 100
332 ms98132 KiB
#include <bits/stdc++.h>

#define no "NO"
#define yes "YES"
#define F first
#define S second
#define vec vector
#define task "PILOT"
#define ll long long
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())

using namespace std;

template<typename U, typename V> bool maximize(U &a, V b) {
    if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool minimize(U &a, V b) {
    if (a > b) { a = b; return 1; } return 0;
}

const int N = (int)1e6 + 9;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;

int n, numQuery;
int a[N], query[N];

namespace sub3 {
    bool valid() {
        return max(n, numQuery) <= 200;
    }

    void solve() {
        FOR(i, 1, numQuery) {
            int ans = 0;
            FOR(s, 1, n) {
                if (a[s] > query[i]) continue;
                FOR(e, s, n) {
                    if (a[e] <= query[i]) ans++;
                    else break;
                }
            }
            cout << ans << "\n";
        }
    }
}

namespace sub4 {
    bool valid() {
        return 1LL * n * numQuery <= (ll)1e8;
    }

    void solve() {
        FOR(i, 1, numQuery) {
            int ans = 0;
            FOR(s, 1, n) {
                if (a[s] > query[i]) continue;
                int e = s;
                while (e <=n && a[e] <= query[i]) e++;
                e--;
                int cnt = (e - s + 1);
                ans += cnt * (cnt + 1) / 2;
                s = e;
            }
            cout << ans << "\n";
        }
    }
}

namespace subFull {
    vector<int> pos[N];
    bool vis[N];
    ll preLen[2 * N], sufLen[2 * N];
    ll ans[N];

    void solve() {
        int maxVal = *max_element(query + 1, query + numQuery + 1);
        FOR(i, 1, n) pos[a[i]].push_back(i);

        memset(vis, 0, sizeof(vis));

        FOR(i, 1, maxVal) {
            ans[i] = ans[i - 1];
            for (int x : pos[i]) {
                ans[i]++;
                if (vis[x - 1]) ans[i] += preLen[x - 1], sufLen[x - 1 - preLen[x - 1] + 1]++;
                if (vis[x + 1]) ans[i] += sufLen[x + 1], preLen[x + 1 + sufLen[x + 1] - 1]++;
                if (vis[x - 1] && vis[x + 1]) {
                    ans[i] += 1LL * preLen[x - 1] * sufLen[x + 1];
                    sufLen[x - 1 - preLen[x - 1] + 1] += sufLen[x + 1];
                    preLen[x + 1 + sufLen[x + 1] - 1] += preLen[x - 1];
                }
                preLen[x] = preLen[x - 1] + 1;
                sufLen[x] = sufLen[x + 1] + 1;
                vis[x] = 1;
            }
        }

        FOR(i, 1, numQuery) {
            cout << ans[query[i]] << "\n";
        }
    }
}

void main_code() {
    cin >> n >> numQuery;
    FOR(i, 1, n) cin >> a[i];
    FOR(i, 1, numQuery) cin >> query[i];

    subFull::solve();
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    const bool MULTITEST = 0;
    int num_test = 1; if (MULTITEST) cin >> num_test;
    while (num_test--) { main_code(); cout << "\n"; }
}

/* Lak lu theo dieu nhac */

Compilation message (stderr)

pilot.cpp: In function 'int main()':
pilot.cpp:123:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  123 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
pilot.cpp:124:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  124 |         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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...