結果

問題 No.1649 Manhattan Square
ユーザー mugen_1337mugen_1337
提出日時 2021-08-13 23:03:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 9,553 bytes
コンパイル時間 3,200 ms
コンパイル使用メモリ 229,924 KB
実行使用メモリ 13,888 KB
最終ジャッジ日時 2024-04-14 20:02:54
合計ジャッジ時間 10,951 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
13,884 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
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;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;

template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

template<ll Mod>
struct ModInt{
    long long x;
    ModInt():x(0){}
    ModInt(long long y):x(y>=0?y%Mod:(Mod-(-y)%Mod)%Mod){}
    ModInt &operator+=(const ModInt &p){
        if((x+=p.x)>=Mod) x-=Mod;
        return *this;
    }
    ModInt &operator-=(const ModInt &p){
        if((x+=Mod-p.x)>=Mod)x-=Mod;
        return *this;
    }
    ModInt &operator*=(const ModInt &p){
        x=(int)(1ll*x*p.x%Mod);
        return *this;
    }
    ModInt &operator/=(const ModInt &p){
        (*this)*=p.inverse();
        return *this;
    }
    ModInt operator-()const{return ModInt(-x);}
    ModInt operator+(const ModInt &p)const{return ModInt(*this)+=p;}
    ModInt operator-(const ModInt &p)const{return ModInt(*this)-=p;}
    ModInt operator*(const ModInt &p)const{return ModInt(*this)*=p;}
    ModInt operator/(const ModInt &p)const{return ModInt(*this)/=p;}
    bool operator==(const ModInt &p)const{return x==p.x;}
    bool operator!=(const ModInt &p)const{return x!=p.x;}
    ModInt inverse()const{
        int a=x,b=Mod,u=1,v=0,t;
        while(b>0){
            t=a/b;
            swap(a-=t*b,b);swap(u-=t*v,v);
        }
        return ModInt(u);
    }
    ModInt pow(long long n)const{
        ModInt ret(1),mul(x);
        while(n>0){
            if(n&1) ret*=mul;
            mul*=mul;n>>=1;
        }
        return ret;
    }
    friend ostream &operator<<(ostream &os,const ModInt &p){return os<<p.x;}
    friend istream &operator>>(istream &is,ModInt &a){long long t;is>>t;a=ModInt<Mod>(t);return (is);}
    static int get_mod(){return Mod;}
};

using mint=ModInt<998244353>;


template<typename Monoid,typename OperatorMonoid>
struct ImplicitTreap{

    private:

    inline int xorshift(){
        static int x=122312555;
        static int y=336261662;
        static int z=558127122;
        static int w=917277772;
        int t;
        t=x^(x<<11);
        x=y;y=z;z=w;
        return w=(w^(w>>19))^(t^(t>>8));
    }

    using F=function<Monoid(Monoid,Monoid)>;
    using G=function<Monoid(Monoid,OperatorMonoid)>;
    using H=function<OperatorMonoid(OperatorMonoid,OperatorMonoid)>;

    struct Node{
        Node *l,*r;
        int cnt,priority;
        Monoid val,acc;
        OperatorMonoid lazy;
        bool rev;
        Node()=default;
        Node(const Monoid &val,const OperatorMonoid &lazy,int priority):
        l(nullptr),r(nullptr),cnt(1),priority(priority),val(val),acc(val),lazy(lazy),rev(false){}
    } *root=nullptr;

    const F f;
    const G g;
    const H h;
    const Monoid M1;
    const OperatorMonoid OM0;

    int count(const Node *t){return t?t->cnt:0;}
    Monoid acc(const Node *t){return t?t->acc:M1;}

    Node *update(Node *t){
        t->cnt=count(t->l)+count(t->r)+1;
        t->acc=f(f(acc(t->l),t->val),acc(t->r));
        return t;
    }
    Node *propagate(Node *t){
        if(t and t->rev){
            t->rev=false;
            swap(t->l,t->r);
            if(t->l) t->l->rev^=1;
            if(t->r) t->r->rev^=1;
        }
        if(t and t->lazy!=OM0){
            t->val=g(t->val,t->lazy);
            if(t->l){
                t->l->lazy=h(t->l->lazy,t->lazy);
                t->l->acc=g(t->l->acc,t->lazy);
            }
            if(t->r){
                t->r->lazy=h(t->r->lazy,t->lazy);
                t->r->acc=g(t->r->acc,t->lazy);
            }
            t->lazy=OM0;
        }
        return update(t);
    }

    Node *merge(Node *l,Node *r){
        if(!l or !r) return l?l:r;
        if(l->priority>r->priority){
            l=propagate(l);
            l->r=merge(l->r,r);
            return update(l);
        }else{
            r=propagate(r);
            r->l=merge(l,r->l);
            return update(r);
        }
    }
    pair<Node *,Node *> split(Node *t,int k){
        if(!t) return {nullptr,nullptr};
        t=propagate(t);
        if(k<=count(t->l)){
            auto s=split(t->l,k);
            t->l=s.second;
            return {s.first,update(t)};
        }else{
            auto s=split(t->r,k-count(t->l)-1);
            t->r=s.first;
            return {update(t),s.second};
        }
    }

    void insert(Node *&t,int k,const Monoid &x){
        auto s=split(t,k);
        t=merge(merge(s.first,new Node(x,OM0,xorshift())),s.second);
    }
    void erase(Node *&t,int k){
        auto s=split(t,k);
        t=merge(s.first,split(s.second,1).second);
    }

    Monoid query(Node *&t,int a,int b){
        if(a>b) return M1;
        auto x=split(t,a);
        auto y=split(x.second,b-a);
        auto ret=acc(y.first);
        t=merge(x.first,merge(y.first,y.second));
        return ret;
    }
    void update(Node *&t,int a,int b,const OperatorMonoid &o){
        if(a>b) return ;
        auto x=split(t,a);
        auto y=split(x.second,b-a);
        y.first->lazy=h(y.first->lazy,o);
        t=merge(x.first,merge(propagate(y.first),y.second));
    }
    void reverse(Node *&t,int a,int b){
        if(a>b) return ;
        auto x=split(t,a);
        auto y=split(x.second,b-a);
        y.first->rev^=1;
        t=merge(x.first,merge(y.first,y.second));
    }
    // [l,r)の先頭がmになるまで左シフト
    void rotate(Node *&t,int l,int m,int r){
        reverse(t,l,r);
        reverse(t,l,l+r-m);
        reverse(t,l+r-m,r);   
    }
    void dump(Node *t,typename vector<Monoid>::iterator &ite){
        if(!t) return ;
        t=propagate(t);
        dump(t->l,ite);
        *ite=t->val;
        dump(t->r,++ite);
    }

    public:

    ImplicitTreap(const F &f,const G &g,const H &h,const Monoid &M1,const OperatorMonoid &OM0):
        f(f),g(g),h(h),M1(M1),OM0(OM0){}

    int size(){return count(root);}
    bool empty(){return !root;}

    void build(const vector<Monoid> &v){
        if(v.empty()) return ;
        for(int i=(int)v.size()-1;i>=0;i--) insert(0,v[i]);
    }

    void insert(int k,const Monoid &x){insert(root,k,x);}
    void erase(int k){erase(root,k);}
    void erase(int l,int r){
        auto x=split(root,l);
        auto y=split(x.second,r-l);
        root=merge(x.first,y.second);
    }
    void reverse(int l,int r){reverse(root,l,r);}
    Monoid query(int l,int r){return query(root,l,r);}
    void update(int l,int r,const OperatorMonoid &x){update(root,l,r,x);}
    vector<Monoid> dump(){
        vector<Monoid> ret(size());
        auto ite=begin(ret);
        dump(root,ite);
        return ret;
    }

    Monoid operator[](int idx){return query(idx,idx+1);}
};

// range add range sum (sum, count)
using M=pair<ll,ll>;
using OM=ll;
const M M1=M(0,0);
const OM OM0=0;
M segf(M a,M b){
    return M(a.first+b.first,a.second+b.second);
}
M segg(M a,OM b){
    return M(a.first+a.second*b,a.second);
}
OM segh(OM a,OM b){
    return a+b;
}


signed main(){
    int N;cin>>N;

    vector<ll> x(N),y(N);
    rep(i,N) cin>>x[i]>>y[i];
    

    mint res=0;
    {
        mint sx=0;
        mint ss=0;
        rep(i,N){
            res+=mint(x[i])*x[i]*i+ss-sx*x[i]*2;
            ss+=mint(x[i])*x[i];
            sx+=x[i];
        }
    }
    {
        mint sy=0;
        mint ss=0;
        rep(i,N){
            res+=mint(y[i])*y[i]*i+ss-sy*y[i]*2;
            ss+=mint(y[i])*y[i];
            sy+=y[i];
        }
    }

    {
        vector<int> idx(N);
        iota(ALL(idx),0);
        sort(ALL(idx),[&](int i,int j){return x[i]>x[j];});

        vector<ll> nx(N),ny(N);
        rep(i,N) nx[i]=x[idx[i]], ny[i]=y[idx[i]];
        x=nx;
        y=ny;
    }

    ImplicitTreap<M,OM> lw(segf,segg,segh,M1,OM0),hi(segf,segg,segh,M1,OM0);
    vector<pair<ll,ll>> ysort;
    rep(i,N) ysort.emplace_back(y[i],1);

    sort(ALL(ysort));

    lw.build(ysort);
    hi.build({});

    rep(i,N){
        int l=0,r=lw.size()-1,j=lw.size();
        while(l<=r){
            // cout<<"sine "<<l<<" ~ "<<r<<", ("<<j<<endl;
            int m=(l+r)/2;
            // cout<<"kasu"<<lw[m]<<endl;
            if(lw[m].first==y[i]){
                j=m;
                break;
            }
            if(y[i]<=lw[m].first) j=m,r=m-1;
            else                  l=m+1;
        }

        mint add=mint(y[i])*j-lw.query(0,j).first+lw.query(j+1,lw.size()).first-mint(y[i])*(lw.size()-1-j);

        lw.erase(j);

        l=0,r=hi.size()-1;
        int k=hi.size();
        while(l<=r){
            int m=(l+r)/2;
            if(y[i]>=hi[m].first) k=m,l=m+1;
            else                  r=m-1;
        }

        mint sub=mint(y[i])*mint(k)-mint(hi.query(0,k).first)+mint(hi.query(k,hi.size()).first)-mint(y[i])*(hi.size()-k);


        res+=(add+sub)*x[i]*2;
        hi.insert(k,make_pair(y[i],1));
    }
    cout<<res<<endl;
    return 0;
}
0