結果

問題 No.1649 Manhattan Square
ユーザー 蜜蜂蜜蜂
提出日時 2021-07-28 09:42:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,240 bytes
コンパイル時間 4,852 ms
コンパイル使用メモリ 245,840 KB
実行使用メモリ 58,524 KB
最終ジャッジ日時 2024-04-14 15:54:37
合計ジャッジ時間 30,637 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

//g++ 3.cpp -std=c++14 -O2 -I .
#include <bits/stdc++.h>
using namespace std;

#include <atcoder/all>
using namespace atcoder;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vld = vector<ld>;
using vvld = vector<vld>;

#define fi first
#define se second
#define pb push_back
#define pq_big(T) priority_queue<T,vector<T>,less<T>>
#define pq_small(T) priority_queue<T,vector<T>,greater<T>>
#define all(a) a.begin(),a.end()
#define rep(i,start,end) for(ll i=start;i<(ll)(end);i++)
#define per(i,start,end) for(ll i=start;i>=(ll)(end);i--)

using mint = modint998244353;

struct S{
  //y=i範囲の点の数,xの合計,yの合計,xyの合計
  int quant;
  mint sum_x;
  mint sum_y;
  mint sum_pro;
};

S op(S a,S b){
  return {a.quant+b.quant,a.sum_x+b.sum_x,a.sum_y+b.sum_y,a.sum_pro+b.sum_pro};
}

S e(){
  return {0,0,0,0};
}

//i = 0 to N-1 j = i+1 to N (a[i]-a[j])^2
mint sum_differ(vll &a){
  int sz=a.size();

  mint res=0,sum=0;
  rep(i,0,sz){
    sum+=a[i];
    res+=a[i]*a[i]*2*sz;
  }
  sum*=sum;
  res-=2*sum;
  res/=2;
  return res;
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin>>n;

  vll x(n),y(n);
  vll val_x,val_y;
  rep(i,0,n){
    cin>>x[i]>>y[i];
    val_x.pb(x[i]);
    val_y.pb(y[i]);
  }

  sort(all(val_x));
  sort(all(val_y));
  unique(all(val_x));
  unique(all(val_y));

  map<ll,int> inv_x;
  map<ll,int> inv_y;

  rep(i,0,val_x.size()){
    inv_x[val_x[i]]=i;
  }
  rep(i,0,val_y.size()){
    inv_y[val_y[i]]=i;
  }

  //座圧後の点
  vector<pair<int,int>> point(n);
  rep(i,0,n){
    point[i].fi=inv_x[x[i]];
    point[i].se=inv_y[y[i]];
  }
  sort(all(point));


  //x_coordinate[i] := 座圧後にx座標がiとなる点の番号
  vvi x_coordinate(n+100);
  rep(i,0,n){
    x_coordinate[point[i].fi].pb(i);
  }

  mint ans=0;

  //左から平面走査
  //seg[i] := 座圧後にy座標がiとなる点についてのいろいろ
  segtree<S,op,e> seg(n+100);
  rep(i,0,n+100){
    if(x_coordinate[i].size()==0)continue;

    //寄与の計算と更新
    for(int num:x_coordinate[i]){
      int x=point[num].fi,y=point[num].se;
      mint ori_x=val_x[x],ori_y=val_y[y];
      S range=seg.prod(0,y);

      ans+=range.quant*ori_x*ori_y;
      ans-=ori_x*range.sum_y;
      ans-=ori_y*range.sum_x;
      ans+=range.sum_pro;

      S now=seg.get(y);
      now.quant++;
      now.sum_x+=ori_x;
      now.sum_y+=ori_y;
      now.sum_pro+=ori_x*ori_y;
      seg.set(y,now);
    }
  }

  //初期化
  rep(i,0,n+100){
    seg.set(i,e());
  }

  //右から平面走査
  per(i,n+99,0){
    if(x_coordinate[i].size()==0)continue;

    //寄与の計算と更新
    for(int num:x_coordinate[i]){
      int x=point[num].fi,y=point[num].se;
      mint ori_x=val_x[x],ori_y=val_y[y];
      S range=seg.prod(0,y);

      ans-=range.quant*ori_x*ori_y;
      ans+=ori_x*range.sum_y;
      ans+=ori_y*range.sum_x;
      ans-=range.sum_pro;

      S now=seg.get(y);
      now.quant++;
      now.sum_x+=ori_x;
      now.sum_y+=ori_y;
      now.sum_pro+=ori_x*ori_y;
      seg.set(y,now);
    }
  }

  ans*=2;
  ans+=sum_differ(x);
  ans+=sum_differ(y);

  cout<<ans.val()<<"\n";
}
0