結果

問題 No.2710 How many more?
ユーザー kaiki yamada
提出日時 2024-11-01 21:55:02
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,592 bytes
コンパイル時間 3,949 ms
コンパイル使用メモリ 283,032 KB
実行使用メモリ 7,936 KB
最終ジャッジ日時 2024-11-01 21:55:08
合計ジャッジ時間 5,737 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 5 WA * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<P> VP;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define ALL(v) v.begin(), v.end()
#define OUT(n) cout << n << "\n"
// constexpr ll MOD=998244353;
// constexpr ll MOD=1000000007;
// constexpr ll INF=2e18;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    ll N, Q;
    cin >> N >> Q;

    VP A(N);
    REP(i, N) {
        cin >> A[i].first;
        A[i].second = i;
    }

    VP sorted_A = A;
    sort(ALL(sorted_A));

    VI C(N); // 一個前の人のindex
    ll prev = -1;
    REP(i, N) {
        if (i != 0) {
            if (sorted_A[i].first != sorted_A[i - 1].first) {
                prev = sorted_A[i - 1].second;
            }
        }
        C[sorted_A[i].second] = prev;
    }

    VI B(N); // i番目の人前にいる人の数
    ll cnt = 0;
    ll wait = 0;
    REP(i, N) {
        if (i != 0) {
            if (sorted_A[i].first != sorted_A[i - 1].first) {
                cnt += wait;
                wait = 0;
            }
        }
        B[sorted_A[i].second] = cnt;
        wait += 1;
    }

    REP(i, Q) {
        ll x, y;
        cin >> x >> y;
        ll k = 0;
        if (C[x - 1] != -1) k = B[C[x - 1]];
        ll howmany = k - B[y - 1];
        if(C[x - 1] != -1) {
            if(B[x - 1] - B[C[x - 1]] != 1) {
                howmany += B[x - 1] - B[C[x - 1]];
            }
        }
        if (howmany < 0) {
            howmany = 0;
        }
        OUT(howmany);
    }
}
0