結果

問題 No.1919 Many Monster Battles
ユーザー RubikunRubikun
提出日時 2022-04-29 23:50:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 6,758 bytes
コンパイル時間 3,016 ms
コンパイル使用メモリ 233,360 KB
実行使用メモリ 6,632 KB
最終ジャッジ日時 2023-09-11 15:33:09
合計ジャッジ時間 12,764 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=200005,INF=1<<30;

//2d point update rectangle sum

// https://kopricky.github.io/code/SegmentTrees/rangetree_pointupdate.html

template<typename T> class segtree {
private:
    int n, sz;
    vector<T> node;
public:
    void init(const vector<T>& v){
        sz = (int)v.size();
        n = 1;
        while(n < sz){
            n *= 2;
        }
        node.assign(2*n, 0);
        for(int i = 0; i < sz; i++){
            node[i+n] = v[i];
        }
        for(int i=n-1; i>=1; i--){
            node[i] = node[2*i] + node[2*i+1];
        }
    }
    void update(int k, const T a)
    {
        node[k+=n] += a;
        while(k>>=1){
            node[k] = node[2*k] + node[2*k+1];
        }
    }
    T query(int a, int b)
    {
        T res1 = 0, res2 = 0;
        a += n, b += n;
        while(a != b){
            if(a % 2) res1 = res1 + node[a++];
            if(b % 2) res2 = res2 + node[--b];
            a >>= 1, b >>= 1;
        }
        return res1 + res2;
    }
    void print(){
        for(int i = 0; i < sz; i++){
            cout << query(i, i+1) << " ";
        }
        cout << endl;
    }
};

//座標の型, 値の型
template<typename CandidateType, typename ValueType> class RangeTree
{
public:
    static_assert(std::is_integral<CandidateType>::value, "Integral required.");
private:
    using CT = CandidateType;
    using VT = ValueType;
    using pcc = pair<CT, CT>;
    using pci = pair<CT, int>;
    int n, sz;
    vector<segtree<VT> > seg;
    // y座標, x座標
    vector<vector<pcc> > yx;
    // y座標, x座標
    vector<pcc> sorted;
    void update_(int id, const CT x, const CT y, const VT val) {
        id += n-1;
        const int yid = lower_bound(all(yx[id]), pcc(y, x)) - yx[id].begin();
        seg[id].update(yid, val);
        while(id > 0){
            id = (id - 1) / 2;
            const int yid = lower_bound(all(yx[id]), pcc(y, x)) - yx[id].begin();
            seg[id].update(yid, val);
        }
    }
    VT query(const int lxid, const int rxid, const CT ly, const CT ry, const int k, const int l, const int r) {
        if(r <= lxid || rxid <= l) return 0;
        if(lxid <= l && r <= rxid){
            const int lyid = lower_bound(all(yx[k]), pcc(ly, numeric_limits<CT>::min())) - yx[k].begin();
            const int ryid = upper_bound(all(yx[k]), pcc(ry, numeric_limits<CT>::min())) - yx[k].begin();
            return (lyid >= ryid) ? 0 : seg[k].query(lyid, ryid);
        }else{
            return query(lxid, rxid, ly, ry, 2*k+1, l, (l+r)/2) + query(lxid, rxid, ly, ry, 2*k+2, (l+r)/2, r);
        }
    }
public:
    // 座標, 点の値
    RangeTree(const vector<pcc>& cand, const vector<VT>& val) : n(1), sz((int)cand.size()), sorted(sz){
        while(n < sz) n *= 2;
        for(int i = 0; i < sz; ++i){
            sorted[i] = {cand[i].first, i};
        }
        sort(all(sorted), [&](const pcc& a, const pcc& b){
            return (a.first == b.first) ? (cand[a.second].second < cand[b.second].second) : (a.first < b.first);
        });
        yx.resize(2*n-1), seg.resize(2*n-1);
        for(int i = 0; i < sz; ++i){
            yx[i+n-1] = {{sorted[i].second, sorted[i].first}};
            vector<VT> arg = {val[sorted[i].second]};
            seg[i+n-1].init(arg);
            sorted[i].second = cand[sorted[i].second].second;
        }
        for(int i = n-2; i >= 0; --i){
            yx[i].resize((int)yx[2*i+1].size() + (int)yx[2*i+2].size());
            if(yx[i].empty()) continue;
            merge(all(yx[2*i+1]), all(yx[2*i+2]), yx[i].begin(), [&](const pcc& a, const pcc& b){
                return (cand[a.first].second == cand[b.first].second)
                ? (a.second < b.second) : (cand[a.first].second < cand[b.first].second);
            });
            vector<VT> arg((int)yx[i].size());
            for(int j = 0; j < (int)yx[i].size(); ++j){
                arg[j] = val[yx[i][j].first];
            }
            seg[i].init(arg);
        }
        for(int i = 0; i < 2*n-1; ++i){
            for(pcc& e : yx[i]){
                e.first = cand[e.first].second;
            }
        }
    }
    // 点 (x,y) の更新を行う
    void update(const CT x, const CT y, const VT val){
        const int id = lower_bound(all(sorted), pcc(x, y)) - sorted.begin();
        return update_(id, x, y, val);
    }
    // [lx,rx) × [ly,ry) の長方形領域のクエリに答える
    VT query(const CT lx, const CT ly, const CT rx, const CT ry){
        const int lxid = lower_bound(all(sorted), pcc(lx, numeric_limits<CT>::min())) - sorted.begin();
        const int rxid = upper_bound(all(sorted), pcc(rx, numeric_limits<CT>::min())) - sorted.begin();
        return (lxid >= rxid) ? 0 : query(lxid, rxid, ly, ry, 0, 0, n);
    }
};


int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N;cin>>N;
    vector<pair<ll,ll>> S(N);
    for(int i=0;i<N;i++) cin>>S[i].fi;
    for(int i=0;i<N;i++) cin>>S[i].se;
    
    {
        sort(all(S));
        
        vector<pair<ll,ll>> aa;
        vector<ll> bb;
        RangeTree<ll,ll> RT(aa,bb);
        RangeTree<ll,ll> cn(aa,bb);
        
        ll ans=0;
        
        map<pair<ll,ll>,ll> MA1,MA2;
        
        for(int i=0;i<N;i++){
            ll x=S[i].fi+S[i].se,y=S[i].fi-S[i].se;
            auto a=RT.query(-INF,x,-INF,y);
            auto b=cn.query(-INF,x,-INF,y);
            ans+=S[i].fi*b-a;
            MA1[mp(x,y)]+=S[i].fi;
            MA2[mp(x,y)]++;
            RT.update(x,y,MA1[mp(x,y)]);
            cn.update(x,y,MA2[mp(x,y)]);
        }
        
        cout<<ans<<" ";
    }
    
    for(int i=0;i<N;i++) swap(S[i].fi,S[i].se);
    
    {
        sort(all(S));
        
        vector<pair<ll,ll>> aa;
        vector<ll> bb;
        RangeTree<ll,ll> RT(aa,bb);
        RangeTree<ll,ll> cn(aa,bb);
        
        ll ans=0;
        
        map<pair<ll,ll>,ll> MA1,MA2;
        
        for(int i=0;i<N;i++){
            ll x=S[i].fi+S[i].se,y=S[i].fi-S[i].se;
            auto a=RT.query(-INF,x,-INF,y);
            auto b=cn.query(-INF,x,-INF,y);
            ans+=S[i].fi*b-a;
            MA1[mp(x,y)]+=S[i].fi;
            MA2[mp(x,y)]++;
            RT.update(x,y,MA1[mp(x,y)]);
            cn.update(x,y,MA2[mp(x,y)]);
        }
        
        cout<<ans<<endl;
    }
}
0