結果

問題 No.924 紲星
ユーザー ningenMeningenMe
提出日時 2019-11-09 01:51:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,573 bytes
コンパイル時間 2,185 ms
コンパイル使用メモリ 190,888 KB
実行使用メモリ 25,372 KB
最終ジャッジ日時 2024-09-15 03:48:43
合計ジャッジ時間 9,780 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 RE -
testcase_02 RE -
testcase_03 WA -
testcase_04 RE -
testcase_05 RE -
testcase_06 WA -
testcase_07 RE -
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;

#define REP(i,n) for(long long i = 0; i < (n); i++)
#define FOR(i, m, n) for(long long i = (m);i < (n); ++i)
#define ALL(obj) (obj).begin(),(obj).end()
#define SPEED cin.tie(0);ios::sync_with_stdio(false);

template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQR = priority_queue<T,vector<T>,greater<T>>;

constexpr long long MOD = (long long)1e9 + 7;
constexpr long long MOD2 = 998244353;
constexpr long long HIGHINF = (long long)1e18;
constexpr long long LOWINF = (long long)1e15;
constexpr long double PI = 3.1415926535897932384626433;

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, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
template <template <class tmp>  class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; 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);}
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;}

template<class T> class Median{
	multiset<T, greater<>> stl;
	multiset<T> str;
    T init;

    // 要素の均衡を保つ関数
    inline void balance() {
        while(stl.size() < str.size()) {
            stl.insert(*str.begin());
            str.erase(str.begin());
        }
        while(stl.size() > str.size()+1) {
            str.insert(*stl.begin());
            stl.erase(stl.begin());
        }

        if(!str.size()) return;

        for(T tmpl = *stl.begin(),tmpr = *str.begin(); tmpl>tmpr; tmpl = *stl.begin(), tmpr = *str.begin()) {
            stl.erase(stl.begin());
            str.erase(str.begin());
            stl.insert(tmpr);
            str.insert(tmpl);
        }
    }
public:
    Median(T init):init(init){
        //do nothing
    }

    void insert(T val){
        stl.insert(val);
        balance();
    }

    int find(T val){
        return (stl.find(val)!=stl.end() || str.find(val)!=str.end());
    }

    void eraseItr(T val){
        auto itr = stl.find(val);
        if(itr != stl.end()) {
            stl.erase(itr);
            balance();
            return;
        }
        itr = str.find(val);
        if(itr != str.end()) {
            str.erase(itr);
            balance();
            return;
        }
    }

    void eraseVal(T val){
        stl.erase(val);
        str.erase(val);
        balance();
    }

    T get(){
        return (stl.empty()?init:*stl.begin());
    }

    size_t size(){
        return stl.size()+str.size();
    }
};

template<class T> class Mo{
    unordered_map<long long,int> mp;
    long long N;
    int bucket;
    vector<pair<int,int>> range;
	vector<int> idx;
public:

    Mo(long long N, const vector<pair<int,int>>& range) : N(N),range(range),idx(range.size()),bucket(sqrt(N)) {
        iota(idx.begin(),idx.end(),0);
        sort(idx.begin(),idx.end(),[&](int a, int b){
            auto  al = range[a].first/bucket;
            auto  ar = range[a].second;
            auto  bl = range[b].first/bucket;
            auto  br = range[b].second;
            return (al != bl) ? (al < bl) : ((al%2)?(ar > br):(ar < br));
        });
    }
    
    //参照でvectorを渡したりすると良い
    void solve(const vector<T>& A, Median<T>& med){
        int l = 0, r = 0;
        for(int& i:idx){
            auto& xl = range[i].first;
            auto& xr = range[i].second;
            
            //左端を広げる
            while(xl < l){
                l--;
                extend(l,A,med);
            }
            //右端を広げる
            while(r < xr){
                r++;
                extend(r,A,med);
            }
            //左端を狭める
            while(l < xl){
                shrink(l,A,med);
                l++;
            }
            //右端を狭める
            while(xr < r){
                shrink(r,A,med);
                r--;
            }
            mp[xl*N+xr] = get(med);
        }
    }

    T operator [] (pair<int,int> p) {
        return mp[p.first*N+p.second];
    }

private:
    
    //伸びるとき
    void extend(int k,const vector<T>& A, Median<T>& med){
        med.insert(A[k]);
    }

    //縮むとき
    void shrink(int k,const vector<T>& A, Median<T>& med){
        med.eraseItr(A[k]);
    }

    //クエリ結果
    T get(Median<T>& med){
        return med.get();
    }
};

int main() {
    SPEED
	int N,Q; cin >> N >> Q;
    vector<ll> A(N);
	for(int i = 0; i < N; ++i) cin >> A[i];
	vector<pair<int,int>> range(Q);
    for(int i = 0; i < N; ++i) {
        int l,r; cin >> l >> r;
        l--,r--;
        range[i] = {l,r};
    }
	
	//Moで中央値列挙 O(N*sqrt(N)*(logN)^2)
    Mo<ll> mo(N,range);
    Median<ll> med(0);
    med.insert(A[0]);
    mo.solve(A,med);

    for(int i = 0; i < Q; ++i){
        cout << mo[range[i]] << endl;
    }

    return 0;
}
0