結果

問題 No.1649 Manhattan Square
ユーザー 👑 NachiaNachia
提出日時 2021-08-13 22:27:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 297 ms / 3,000 ms
コード長 1,855 bytes
コンパイル時間 875 ms
コンパイル使用メモリ 93,300 KB
実行使用メモリ 9,472 KB
最終ジャッジ日時 2024-04-27 03:54:56
合計ジャッジ時間 14,566 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 4 ms
6,940 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 AC 4 ms
6,940 KB
testcase_05 AC 4 ms
6,940 KB
testcase_06 AC 4 ms
6,940 KB
testcase_07 AC 271 ms
9,344 KB
testcase_08 AC 262 ms
9,344 KB
testcase_09 AC 257 ms
9,344 KB
testcase_10 AC 263 ms
9,344 KB
testcase_11 AC 263 ms
9,344 KB
testcase_12 AC 273 ms
9,472 KB
testcase_13 AC 274 ms
9,344 KB
testcase_14 AC 274 ms
9,344 KB
testcase_15 AC 274 ms
9,472 KB
testcase_16 AC 277 ms
9,344 KB
testcase_17 AC 278 ms
9,344 KB
testcase_18 AC 297 ms
9,344 KB
testcase_19 AC 275 ms
9,344 KB
testcase_20 AC 274 ms
9,344 KB
testcase_21 AC 266 ms
9,344 KB
testcase_22 AC 261 ms
9,344 KB
testcase_23 AC 261 ms
9,344 KB
testcase_24 AC 256 ms
9,344 KB
testcase_25 AC 255 ms
9,472 KB
testcase_26 AC 257 ms
9,344 KB
testcase_27 AC 268 ms
9,344 KB
testcase_28 AC 260 ms
9,344 KB
testcase_29 AC 261 ms
9,344 KB
testcase_30 AC 259 ms
9,344 KB
testcase_31 AC 259 ms
9,344 KB
testcase_32 AC 261 ms
9,344 KB
testcase_33 AC 263 ms
9,344 KB
testcase_34 AC 260 ms
9,344 KB
testcase_35 AC 262 ms
9,344 KB
testcase_36 AC 259 ms
9,344 KB
testcase_37 AC 260 ms
9,344 KB
testcase_38 AC 258 ms
9,344 KB
testcase_39 AC 256 ms
9,344 KB
testcase_40 AC 264 ms
9,344 KB
testcase_41 AC 260 ms
9,472 KB
testcase_42 AC 194 ms
9,372 KB
testcase_43 AC 2 ms
6,940 KB
testcase_44 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <atcoder/modint>
#include <atcoder/fenwicktree>
using namespace std;
using i64 = int64_t;
using u64 = uint64_t;
#define rep(i,n) for(int i=0; i<(n); i++)


const i64 MOD = 998244353;
using m32 = atcoder::static_modint<MOD>;


int lowerBoundIdx(const vector<int>& A, int v){
  return lower_bound(A.begin(),A.end(),v) - A.begin();
}


m32 solve(vector<pair<int,int>> P){
  int n = P.size();
  sort(P.begin(),P.end());
  vector<int> Y(n);
  rep(i,n) Y[i] = P[i].second;
  sort(Y.begin(),Y.end());
  atcoder::fenwick_tree<m32> G0(n);
  atcoder::fenwick_tree<m32> G1(n);
  atcoder::fenwick_tree<m32> G2(n);
  m32 ans = 0;
  for(auto p : P){
    int y = lowerBoundIdx(Y,p.second);
    m32 w = m32(p.first) + p.second;
    ans += G2.sum(0,y);
    ans -= G1.sum(0,y) * w * 2;
    ans += G0.sum(0,y) * w * w;
    G0.add(y, 1);
    G1.add(y, w);
    G2.add(y, w * w);
  }
  return ans;
}

m32 solve2(vector<pair<int,int>> P){
  int n = P.size();
  sort(P.begin(),P.end());
  int prex = -1;
  m32 G0 = 0;
  m32 G1 = 0;
  m32 G2 = 0;
  m32 ans = 0;
  rep(i,n){
    auto p = P[i];
    m32 w = p.second;
    if(p.first == prex){
      ans += G0 * w * w;
      ans -= 2 * G1 * w;
      ans += G2;
    }
    else{
      G0 = G1 = G2 = 0;
    }
    G0 += 1;
    G1 += w;
    G2 += w * w;
    prex = P[i].first;
  }
  return ans;
}


int main() {
  int N; cin >> N;
  vector<pair<int,int>> X(N);
  rep(i,N) cin >> X[i].first >> X[i].second;
  m32 ans = 0;
  rep(t,2){
    rep(i,N) X[i].first = 1000000000 - X[i].first;
    ans += solve(X);
  }
  ans -= solve2(X);
  rep(i,N) swap(X[i].first,X[i].second);
  ans += solve2(X);
  cout << ans.val() << "\n";
  return 0;
}


struct ios_do_not_sync{
  ios_do_not_sync(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
  }
} ios_do_not_sync_instance;


0