結果

問題 No.808 Kaiten Sushi?
ユーザー 沙耶花沙耶花
提出日時 2020-12-08 14:29:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 802 ms / 2,000 ms
コード長 4,988 bytes
コンパイル時間 2,535 ms
コンパイル使用メモリ 215,132 KB
実行使用メモリ 18,944 KB
最終ジャッジ日時 2023-10-17 17:14:31
合計ジャッジ時間 17,501 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 6 ms
4,348 KB
testcase_08 AC 6 ms
4,348 KB
testcase_09 AC 12 ms
4,348 KB
testcase_10 AC 8 ms
4,348 KB
testcase_11 AC 6 ms
4,348 KB
testcase_12 AC 8 ms
4,348 KB
testcase_13 AC 6 ms
4,348 KB
testcase_14 AC 4 ms
4,348 KB
testcase_15 AC 11 ms
4,348 KB
testcase_16 AC 8 ms
4,348 KB
testcase_17 AC 5 ms
4,348 KB
testcase_18 AC 4 ms
4,348 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 3 ms
4,348 KB
testcase_23 AC 177 ms
10,608 KB
testcase_24 AC 139 ms
7,828 KB
testcase_25 AC 164 ms
10,456 KB
testcase_26 AC 155 ms
10,336 KB
testcase_27 AC 504 ms
17,848 KB
testcase_28 AC 128 ms
7,476 KB
testcase_29 AC 472 ms
17,584 KB
testcase_30 AC 288 ms
11,392 KB
testcase_31 AC 534 ms
18,088 KB
testcase_32 AC 607 ms
18,808 KB
testcase_33 AC 283 ms
11,360 KB
testcase_34 AC 336 ms
11,848 KB
testcase_35 AC 415 ms
17,104 KB
testcase_36 AC 283 ms
11,304 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 2 ms
4,348 KB
testcase_39 AC 778 ms
18,384 KB
testcase_40 AC 138 ms
7,284 KB
testcase_41 AC 127 ms
7,444 KB
testcase_42 AC 528 ms
17,408 KB
testcase_43 AC 179 ms
10,272 KB
testcase_44 AC 416 ms
11,848 KB
testcase_45 AC 177 ms
10,272 KB
testcase_46 AC 96 ms
7,052 KB
testcase_47 AC 796 ms
18,944 KB
testcase_48 AC 796 ms
18,944 KB
testcase_49 AC 773 ms
18,944 KB
testcase_50 AC 800 ms
18,944 KB
testcase_51 AC 802 ms
18,944 KB
testcase_52 AC 549 ms
17,136 KB
testcase_53 AC 779 ms
18,808 KB
testcase_54 AC 2 ms
4,348 KB
testcase_55 AC 2 ms
4,348 KB
testcase_56 AC 2 ms
4,348 KB
testcase_57 AC 245 ms
10,368 KB
testcase_58 AC 396 ms
11,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <algorithm>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <vector>

namespace atcoder {

template <class S, S (*op)(S, S), S (*e)()> struct segtree {
  public:
    segtree() : segtree(0) {}
    segtree(int n) : segtree(std::vector<S>(n, e())) {}
    segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;

        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    template <bool (*f)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) {
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*f)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

}  // namespace atcoder

using namespace atcoder;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000000000000

array<int,2> op(array<int,2> a,array<int,2> b){
	rep(i,2)a[i] = max(a[i],b[i]);
	return a;
}

array<int,2> e(){
	return array<int,2>{-1,-1};
}

int main(){

	int N;
	cin>>N;
	
	long long L;
	cin>>L;
	
	vector<pair<long long,int>> V(2*N);
	rep(i,N){
		scanf("%lld",&V[i].first);
		V[i].second = 0;
	}
	
	rep(i,N){
		scanf("%lld",&V[i+N].first);
		V[i+N].second = 1;
	}
	
	sort(V.begin(),V.end());
	
	vector<array<int,2>> y(4*N,e());
	rep(i,2*N){
		y[i][V[i].second] = i;
		y[i+2*N][V[i].second] = 2*N+i;
	}
	
	segtree<array<int,2>,op,e> seg(y);
	
	vector<int> ans;
	int now = 0;
	
	while(ans.size()!=2*N){
		int temp = 0;
		if(ans.size()>0)temp = ans.back();
		
		int ok = 4*N,ng = temp;
		while(ok-ng>1){
			int mid = (ok+ng)/2;
			array<int,2> ret = seg.prod(temp,mid);
			if(ret[now]==-1)ng = mid;
			else ok = mid;
		}
		int x;
		{
			array<int,2> ret = seg.prod(temp,ok);
			x = ret[now^1];
		}
		ok = temp,ng = 4*N;
		while(ng-ok>1){
			int mid = (ok+ng)/2;
			array<int,2> ret = seg.prod(temp,mid);
			if(ret[now^1]<=x)ok = mid;
			else ng = mid;
		}
		
		array<int,2> ret = seg.prod(temp,ok);
		x = ret[now]%(2*N);
		ans.push_back(x);
		seg.set(x,e());
		seg.set(x+2*N,e());
		
		now ^= 1;
	}
	
	
	long long Ans = 0LL;
	
	rep(i,2*N){
		if(i==0)Ans += V[ans[i]].first;
		else{
			long long temp = V[ans[i]].first - V[ans[i-1]].first;
			if(temp<0)temp += L;
			Ans += temp;
		}
	}
	
	cout<<Ans<<endl;
		
	
	return 0;
}
0