結果

問題 No.59 鉄道の旅
ユーザー yaoshimax
提出日時 2015-02-22 20:11:22
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 59 ms / 5,000 ms
コード長 1,268 bytes
コンパイル時間 765 ms
コンパイル使用メモリ 87,144 KB
実行使用メモリ 81,536 KB
最終ジャッジ日時 2024-12-24 20:30:34
合計ジャッジ時間 2,073 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 12
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:56:9: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   56 |    scanf("%d%d",&N,&K);
      |    ~~~~~^~~~~~~~~~~~~~
main.cpp:61:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   61 |       scanf("%d",&w);
      |       ~~~~~^~~~~~~~~

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>

using namespace std;

class BIT{
  /*binary indexed tree*/
 private:
  vector<long long> data;
  int size;
 public:
  BIT(int n): data(n+1,0),size(n){
    return;
  }
  long long sum(int l, int r ){
    // return sum in [l+1,r] or (l,r]
    return sum_(r)-sum_(l);
  }
  long long sum_(int i){
    // return sum (0, i]
    long long ans = 0;
    while( 0 < i ){
      ans += data[i];
      i -= i&-i;   // 11010110 -> 1010100
    }
    return ans;
  }
  void add(int i, int x){
    // add x to i
    while( i <= size ){
      data[i] += x;
      i+= i&-i;
    }
    return;
  }
};
int main(){
   int N,K;
   scanf("%d%d",&N,&K);
   BIT b(10000001);

   for( int i = 0 ; i < N; i++ ){
      int w;
      scanf("%d",&w);
      if(w>0 && b.sum(w-1,1000000) < K){
         b.add(w,1);
      }
      if(w<0 && b.sum(-w-1,-w) >0){
         b.add(-w,-1);
      }
   }
      printf("%d\n",(int)b.sum_(1000000));
   return 0;
}
0