結果

問題 No.919 You Are A Project Manager
ユーザー ningenMeningenMe
提出日時 2021-01-04 04:18:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 58 ms / 3,000 ms
コード長 9,086 bytes
コンパイル時間 3,233 ms
コンパイル使用メモリ 230,348 KB
実行使用メモリ 5,672 KB
最終ジャッジ日時 2024-10-13 23:14:31
合計ジャッジ時間 5,594 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
5,508 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 4 ms
5,248 KB
testcase_05 AC 4 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 23 ms
5,248 KB
testcase_08 AC 7 ms
5,248 KB
testcase_09 AC 5 ms
5,248 KB
testcase_10 AC 8 ms
5,248 KB
testcase_11 AC 7 ms
5,248 KB
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 9 ms
5,248 KB
testcase_14 AC 3 ms
5,248 KB
testcase_15 AC 4 ms
5,248 KB
testcase_16 AC 13 ms
5,248 KB
testcase_17 AC 13 ms
5,636 KB
testcase_18 AC 14 ms
5,508 KB
testcase_19 AC 14 ms
5,516 KB
testcase_20 AC 52 ms
5,656 KB
testcase_21 AC 14 ms
5,512 KB
testcase_22 AC 25 ms
5,248 KB
testcase_23 AC 23 ms
5,248 KB
testcase_24 AC 25 ms
5,248 KB
testcase_25 AC 23 ms
5,248 KB
testcase_26 AC 50 ms
5,652 KB
testcase_27 AC 43 ms
5,508 KB
testcase_28 AC 57 ms
5,536 KB
testcase_29 AC 14 ms
5,636 KB
testcase_30 AC 14 ms
5,636 KB
testcase_31 AC 13 ms
5,640 KB
testcase_32 AC 13 ms
5,512 KB
testcase_33 AC 26 ms
5,248 KB
testcase_34 AC 26 ms
5,248 KB
testcase_35 AC 56 ms
5,668 KB
testcase_36 AC 55 ms
5,540 KB
testcase_37 AC 58 ms
5,672 KB
testcase_38 AC 58 ms
5,544 KB
testcase_39 AC 2 ms
5,248 KB
testcase_40 AC 4 ms
5,248 KB
testcase_41 AC 2 ms
5,248 KB
testcase_42 AC 2 ms
5,248 KB
testcase_43 AC 3 ms
5,248 KB
testcase_44 AC 5 ms
5,248 KB
testcase_45 AC 2 ms
5,248 KB
testcase_46 AC 5 ms
5,248 KB
testcase_47 AC 24 ms
5,248 KB
testcase_48 AC 23 ms
5,248 KB
testcase_49 AC 25 ms
5,248 KB
testcase_50 AC 24 ms
5,248 KB
testcase_51 AC 22 ms
5,248 KB
testcase_52 AC 16 ms
5,248 KB
testcase_53 AC 23 ms
5,248 KB
testcase_54 AC 4 ms
5,248 KB
testcase_55 AC 2 ms
5,248 KB
testcase_56 AC 1 ms
5,248 KB
testcase_57 AC 2 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using int128  = __int128_t;
using int64   = long long;
using int32   = int;
using uint128 = __uint128_t;
using uint64  = unsigned long long;
using uint32  = unsigned int;

#define ALL(obj) (obj).begin(),(obj).end()
template<class T> using priority_queue_reverse = priority_queue<T,vector<T>,greater<T>>;

constexpr int64 MOD = 1'000'000'000LL + 7; //'
constexpr int64 MOD2 = 998244353;
constexpr int64 HIGHINF = 1'000'000'000'000'000'000LL;
constexpr int64 LOWINF = 1'000'000'000'000'000LL; //'
constexpr long double PI = 3.1415926535897932384626433L;

template <class T> vector<T> multivector(size_t N,T init){return vector<T>(N,init);}
template <class... T> auto multivector(size_t N,T... t){return vector<decltype(multivector(t...))>(N,multivector(t...));}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const deque<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}
template <class T> void chmax(T& a, const T b){a=max(a,b);}
template <class T> void chmin(T& a, const T b){a=min(a,b);}
vector<string> split(const string &str, const char delemiter) {vector<string> res;stringstream ss(str);string buffer; while( getline(ss, buffer, delemiter) ) res.push_back(buffer); return res;}
inline constexpr int msb(int x) {return x?31-__builtin_clz(x):-1;}
inline constexpr int64 ceil_div(const int64 a,const int64 b) {return (a+(b-1))/b;}// return ceil(a/b)
void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;}

/*
 * @title BitVector
 * @docs md/segment/BitVector.md
 */
class BitVector{
    inline static constexpr size_t BIT_BLOCK_SIZE = 5;
    inline static constexpr size_t BIT_BLOCK_NUM  = 1<<BIT_BLOCK_SIZE;
    inline static constexpr uint32 popcount(uint32 x) {return __builtin_popcount(x);}
    inline static constexpr uint32 popcount(uint64 x) {return __builtin_popcountll(x);}

    vector<uint32> vec, acc;
    bool is_builded = false;
public:
    BitVector(uint32 N) {
        uint32 tmp = (N + BIT_BLOCK_NUM-1) / BIT_BLOCK_NUM;
        vec.assign(tmp, 0);
        acc.assign(tmp, 0);
    }
    void build() {
        for (size_t i = 0,sum = 0; i < acc.size(); ++i) acc[i] = (sum += popcount(vec[i]) );
        is_builded = true;
    }
    //[0,r) count of bit
    uint32 rank(uint32 r, const bool bit) const {
        assert(is_builded);
        uint32 bit_r = r >> BIT_BLOCK_SIZE;
        uint32 sum = (bit_r ? acc[bit_r - 1] : 0) + popcount(vec[bit_r] & ((1U << (r & (BIT_BLOCK_NUM-1))) - 1));
        return bit ? sum : r - sum;
    }
    //[l,l+1) = bit
    void update(uint32 l, const bool bit) {
        uint32 bit_l = l >> BIT_BLOCK_SIZE;
        if (bit) vec[bit_l] |=   1U << (l & (BIT_BLOCK_NUM-1));
        else     vec[bit_l] &= ~(1U << (l & (BIT_BLOCK_NUM-1)));
    }
    //[l,l+1)
    bool operator[](uint32 l) const { 
        assert(is_builded);
        return ((vec[l >> BIT_BLOCK_SIZE] >> (l & (BIT_BLOCK_NUM-1))) & 1); 
    }
};

/*
 * @title WaveletMatrix
 * @docs md/segment/WaveletMatrix.md
 */
template<class T> class WaveletMatrix{
	size_t length;
    size_t depth;
	vector<BitVector> multi_bit_vector;
	vector<uint32> sum_bit_off;
    vector<T> vec;
    vector<uint32> zarts(const vector<T>& ar) {
        vector<uint32> ord(ar.size()),compressed(ar.size());
        iota(ord.begin(),ord.end(),0);
        sort(ord.begin(),ord.end(),[&](size_t l, size_t r){return ar[l]<ar[r];});
        uint32 cnt = 0;
        compressed.front() = 0;
        T pre = ar[ord.front()];
        for(size_t i=1;i<ord.size();++i) {
            size_t j=ord[i];
            if(pre < ar[j]) ++cnt;
            compressed[j] = cnt;
            pre = ar[j];
        }
        return compressed;
    }
public:
	WaveletMatrix(const vector<T> &arg_vec): vec(arg_vec) {
        length = vec.size();
        auto compressed = zarts(vec);
        sort(vec.begin(),vec.end());
        vec.erase(unique(vec.begin(),vec.end()),vec.end());
        uint32 maxi = *max_element(compressed.begin(),compressed.end()) + 1;
        for(depth=0; (1<<depth) <= maxi; ++depth );

		multi_bit_vector.assign(depth, BitVector(length));
		sum_bit_off.assign(depth, 0UL);
		vector<uint32> prev = compressed, next = prev;
		for(uint32 j = 0; j < depth; ++j,swap(prev,next)) {
			uint32 bit = 1UL << (depth - j - 1);
			for(uint32 i = 0; i < length; ++i) sum_bit_off[j] += !(prev[i] & bit);

			uint32 idx_bit_off = 0, idx_bit_on = sum_bit_off[j];
			for(uint32 i = 0; i < length; ++i) {
				if (prev[i] & bit) multi_bit_vector[j].update(i,1), next[idx_bit_on++] = prev[i];
				else next[idx_bit_off++] = prev[i];
            }
			multi_bit_vector[j].build();
		}
	}

    //[l,l+1) element
	T operator[](uint32 l) const {
		uint32 val = 0;
		for (uint32 j = 0; j < depth; ++j) {
			const bool bit = multi_bit_vector[j][l];
			val = ((val << 1) | bit);
			l = multi_bit_vector[j].rank(l, bit);
			if(bit) l += sum_bit_off[j];
		}
		return vec[val];
	}

    //[l,r) range count of more and less than c (c'<c, c'=c, c<c')
	// tuple<uint32, uint32, uint32> rank_all(T c, uint32 l, uint32 r) const
	// {
	// 	if (c > vec.back()) return make_tuple(r - l, 0U, 0U);
	// 	array<uint32,2> more_and_less = {0,0};
	// 	for (uint32 j = 0; j < depth; ++j) {
	// 		const bool bit = (c >> (depth - j - 1)) & 1;
	// 		more_and_less[bit] += r - l;
	// 		l = multi_bit_vector[j].rank(l, bit);
	// 		r = multi_bit_vector[j].rank(r, bit);
	// 		if (bit) l += sum_bit_off[j], r += sum_bit_off[j];
	// 		more_and_less[bit] -= r - l;
	// 	}
	// 	return make_tuple(more_and_less[1], r - l, more_and_less[0]);
	// }

    //[l,r) range k (0-indexed) th smallest number
	T quantile(uint32 l, uint32 r, uint32 k) const {
		uint32 val = 0;
		for (uint32 j = 0; j < depth; ++j) {
			uint32 cnt_bit_off = multi_bit_vector[j].rank(r, 0) - multi_bit_vector[j].rank(l, 0);
			const bool bit = (k >= cnt_bit_off);
			val = ((val << 1) | bit);
			l = multi_bit_vector[j].rank(l, bit);
			r = multi_bit_vector[j].rank(r, bit);
			if (bit) l += sum_bit_off[j], r += sum_bit_off[j], k -= cnt_bit_off;
		}
		return vec[val];
	}
};

int main() {
    cin.tie(0);ios::sync_with_stdio(false);
	int N; cin >> N;
    vector<int64> A(N);
	for(int i = 0; i < N; ++i) cin >> A[i];
    WaveletMatrix<int64> wm(A);

	//クエリ区間を列挙、調和級数なのでO(N*logN)
	vector<pair<int,int>> range;
	for(int j = 1; j <= N; ++j) {
		int M = N/j;
		for(int i = 0;     i+j <= N; i+=j) range.push_back({i,i+j});
		for(int i = N-M*j; i+j <= N; i+=j) range.push_back({i,i+j});
	}

	long long ans = 0;
	int cnt = 0;
	//区間長決め打ち全探索O(N*logN)
	for(long long n = 1; n <= N; ++n) {
		int M = N/n;
		vector<long long> lSum(M,0),rSum(M,0);
		vector<pair<int, int>> l_range(M),r_range(M);
		//区間取得 O(M)
		for(int i = 0; i < M; ++i) {
			l_range[i] = range[cnt + i];
			lSum[i]   = n*wm.quantile(l_range[i].first,l_range[i].second,(l_range[i].second-l_range[i].first-1)/2) ;
			r_range[i] = range[cnt + i + M];
			rSum[i]   = n*wm.quantile(r_range[i].first,r_range[i].second,(r_range[i].second-r_range[i].first-1)/2);
		}
		//累積和 O(M)
		for(int i = 1;    i < M; ++i) lSum[i]  += lSum[i-1];
		for(int i = M-2; 0 <= i; --i) rSum[i]  += rSum[i+1];
		//累積max O(M)
		for(int i = 1;    i < M; ++i) chmax(lSum[i],lSum[i-1]);
		for(int i = M-2; 0 <= i; --i) chmax(rSum[i],rSum[i+1]);

		chmax(ans,lSum[M-1]);
		chmax(ans,rSum[0]);

		//尺取りしながら左右決め打ち全探索 O(M)
		int j = 0;
		for(int i = 0; i < M; ++i) {
			while(j < M && l_range[i].second-1 >= r_range[j].first) j++;
			if(j<M && l_range[i].second-1 < r_range[j].first) {
				chmax(ans,lSum[i]+rSum[j]);
			}
		}
		cnt += 2*M;
	}
	cout << ans << endl;

    return 0;
}
0