結果
| 問題 | No.2710 How many more? | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2024-04-27 21:13:04 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 234 ms / 2,000 ms | 
| コード長 | 750 bytes | 
| コンパイル時間 | 1,615 ms | 
| コンパイル使用メモリ | 175,296 KB | 
| 実行使用メモリ | 6,820 KB | 
| 最終ジャッジ日時 | 2024-11-16 02:55:16 | 
| 合計ジャッジ時間 | 6,183 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 17 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using pii = pair<int, int>;
int main() {
  int N, Q;
  cin >> N >> Q;
  vector<pii> A(N);
  rep(i, N) {
    int Ai;
    cin >> Ai;
    A[i] = {Ai, i};
  }
  sort(A.begin(), A.end());
  vector<int> C(N, 0);
  rep(i, N-1) {
    if (A[i+1].first == A[i].first) {
      C[i] = C[i+1] = 1;
    }
  }
  vector<int> B(N);
  rep(i, N) {
    B[A[i].second] = i;
  }
  rep(i, Q) {
    int x, y;
    cin >> x >> y;
    x--; y--;
    int dif = B[x] - B[y] - 1;
    if (B[x]+1 >= 0 && C[B[x]] > 0 && C[B[x]-1] > 0)
      dif--;
    if (B[y]-1 < N && C[B[y]] > 0 && C[B[y]+1] > 0)
      dif--;
    if (dif < 0) dif = 0;
    cout << dif << endl;
  }
  return 0;
}
            
            
            
        