結果
問題 | No.59 鉄道の旅 |
ユーザー |
![]() |
提出日時 | 2018-02-13 17:02:54 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 49 ms / 5,000 ms |
コード長 | 2,553 bytes |
コンパイル時間 | 1,476 ms |
コンパイル使用メモリ | 166,972 KB |
実行使用メモリ | 11,720 KB |
最終ジャッジ日時 | 2024-11-29 18:02:59 |
合計ジャッジ時間 | 2,968 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 12 |
ソースコード
#define __USE_MINGW_ANSI_STDIO 0#include <bits/stdc++.h>using namespace std;using ll = long long;#define int llusing VI = vector<int>;using VVI = vector<VI>;using PII = pair<int, int>;#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)#define REP(i, n) FOR(i, 0, n)#define ALL(x) x.begin(), x.end()#define PB push_backconst ll LLINF = (1LL<<60);const int INF = (1LL<<30);const int MOD = 1000000007;template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }template<class S,class T>ostream &operator <<(ostream& out,const pair<S,T>& a){out<<'('<<a.first<<','<<a.second<<')';return out;}template<class T>ostream &operator <<(ostream& out,const vector<T>& a){out<<'[';REP(i, a.size()) {out<<a[i];if(i!=a.size()-1)out<<',';}out<<']';return out;}int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};// Binary Indexed Tree// 0-indexedtemplate <typename T>class BIT {private:vector<T> bit;// 単位元int neutral = 0;// 更新クエリ, 区間クエリfunction<T(T,T)> f = [](const T l, const T r) { return l+r; };function<T(T,T)> g = [](const T l, const T r) { return l+r; };public:// 初期化BIT(int n = 1e5, int neu = 0,function<T(T,T)> _f = [](const T l, const T r) { return l+r; },function<T(T,T)> _g = [](const T l, const T r) { return l+r; }){init(n, neu, _f, _g);}void init(int n = 1e5, int neu = 0,function<T(T,T)> _f = [](const T l, const T r) { return l+r; },function<T(T,T)> _g = [](const T l, const T r) { return l+r; }){neutral = neu; f = _f; g = _g;bit.assign(n+5, neutral);}// iに対する点更新クエリvoid update(int i, T w) {for(int x = i+1; x < bit.size(); x += x&-x) bit[x] = f(bit[x], w);}// [0,i)に対する区間クエリT query(int i) {T ret = neutral;for(int x = i+1; x > 0; x -= x & -x) ret = g(ret, bit[x]);return ret;}};int w[100010];signed main() {int n, k;cin >> n >> k;REP(i, n) cin >> w[i];BIT<int> bit(1e6+5);REP(i, n) {if(w[i] > 0) {if(bit.query(1e6+4) - bit.query(w[i]-1) < k) {bit.update(w[i], 1);}} else {w[i] = -w[i];if(bit.query(w[i]) - bit.query(w[i]-1) >= 1) {bit.update(w[i], -1);}}}cout << bit.query(1e6+4) << endl;return 0;}