/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Wrong Answer 1ms 540.0 KiB
#3 Wrong Answer 1ms 540.0 KiB

Code

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
//#include <queue>
//#include <numeric>
#include <cassert>

using namespace std;

#ifdef LOCAL_DEBUG
#include <local_debug.h>
#define DEBUG(...) DBG2::cprint(#__VA_ARGS__, __LINE__, __VA_ARGS__)
#else
#define DEBUG(...)
#endif

#define SZ(a) int((a).size())
#define REP(i,n) for(int i=0,_n=(n);i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)

using llong = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using II = pair<int,int>;

class SegmentTree {
   int N;
   vector<int> T;

   void build() {
      for (int i = N - 1; i > 0; --i)
         T[i] = T[2*i] + T[2*i+1];
   }
public:
   SegmentTree(int _N) {
      for (N = 1; N < _N; N *= 2);
      T.assign(N*2, 0);
      build();
   }

   void update(int p, int value) {
      int i = p + N;
      T[i] += value;
      for (i /= 2; i > 0; i /= 2)
         T[i] = T[2*i] + T[2*i+1];
   }

   int query(int L, int R) const {
      int resL = 0, resR = 0;
      for (int l = L + N, r = R + N; l <= r; l /= 2, r /= 2) {
         if (1 == (l & 1)) resL = resL + T[l++];
         if (0 == (r & 1)) resR = T[r--] + resR;
      }
      return resL + resR;
   }
};

int solve(int N, VI A) {
   for (int& a : A) a++;

   vector<bool> special(N, false);

   {
      SegmentTree bit(N + 1);
      for (int i = 0; i < N; i++) {
         int a = A[i];
         int cnt = bit.query(0, a);
         if (cnt >= a)
            special[i] = true;
         bit.update(a, 1);
      }
   }

   {
      SegmentTree bit(N + 1);
      for (int i = N-1; i >= 0; i--) {
         int a = A[i];
         int cnt = bit.query(a, N);
         if (cnt >= a)
            special[i] = true;
         bit.update(a, 1);
      }
   }

   int res = count(special.begin(), special.end(), true);
   return res;
}

int main(int argc, char* argv[]) {
   ios_base::sync_with_stdio(false); 
   cin.tie(nullptr);

   int N;
   cin >> N;
   VI A(N);
   REP(i, N)
      cin >> A[i];
   int res = solve(N, A);
   cout << res << '\n';

   return 0;
}

Information

Submit By
Type
Submission
Problem
P1184 The Curious Kid and the Number Game
Contest
Brain Booster #9
Language
C++17 (G++ 13.2.0)
Submit At
2025-04-06 16:05:40
Judged At
2025-04-06 16:05:40
Judged By
Score
1
Total Time
1ms
Peak Memory
540.0 KiB