結果

問題 No.1649 Manhattan Square
ユーザー SSRSSSRS
提出日時 2021-08-13 22:03:57
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 426 ms / 3,000 ms
コード長 2,225 bytes
コンパイル時間 2,073 ms
コンパイル使用メモリ 181,676 KB
実行使用メモリ 17,600 KB
最終ジャッジ日時 2024-04-14 18:00:52
合計ジャッジ時間 18,758 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 6 ms
6,940 KB
testcase_03 AC 6 ms
6,940 KB
testcase_04 AC 6 ms
6,944 KB
testcase_05 AC 6 ms
6,944 KB
testcase_06 AC 6 ms
6,940 KB
testcase_07 AC 411 ms
17,384 KB
testcase_08 AC 403 ms
17,268 KB
testcase_09 AC 413 ms
17,388 KB
testcase_10 AC 407 ms
17,428 KB
testcase_11 AC 416 ms
17,208 KB
testcase_12 AC 329 ms
12,924 KB
testcase_13 AC 376 ms
15,032 KB
testcase_14 AC 344 ms
13,644 KB
testcase_15 AC 372 ms
14,208 KB
testcase_16 AC 321 ms
12,416 KB
testcase_17 AC 324 ms
12,584 KB
testcase_18 AC 320 ms
12,204 KB
testcase_19 AC 357 ms
13,840 KB
testcase_20 AC 336 ms
13,056 KB
testcase_21 AC 365 ms
14,944 KB
testcase_22 AC 412 ms
17,600 KB
testcase_23 AC 420 ms
17,460 KB
testcase_24 AC 418 ms
17,280 KB
testcase_25 AC 416 ms
17,260 KB
testcase_26 AC 420 ms
17,248 KB
testcase_27 AC 412 ms
17,240 KB
testcase_28 AC 417 ms
17,536 KB
testcase_29 AC 415 ms
17,280 KB
testcase_30 AC 416 ms
17,404 KB
testcase_31 AC 410 ms
17,280 KB
testcase_32 AC 407 ms
17,404 KB
testcase_33 AC 413 ms
17,280 KB
testcase_34 AC 415 ms
17,280 KB
testcase_35 AC 418 ms
17,564 KB
testcase_36 AC 414 ms
17,280 KB
testcase_37 AC 415 ms
17,560 KB
testcase_38 AC 423 ms
17,240 KB
testcase_39 AC 426 ms
17,280 KB
testcase_40 AC 421 ms
17,252 KB
testcase_41 AC 415 ms
17,260 KB
testcase_42 AC 288 ms
17,408 KB
testcase_43 AC 2 ms
6,944 KB
testcase_44 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
template <typename T>
struct binary_indexed_tree{
	int N;
	vector<T> BIT;
	binary_indexed_tree(int N): N(N), BIT(N + 1, 0){
	}
	void add(int i, T x){
		i++;
		while (i <= N){
			BIT[i] += x;
			BIT[i] %= MOD;
			i += i & -i;
		}
	}
	T sum(int i){
		T ans = 0;
		while (i > 0){
			ans += BIT[i];
			ans %= MOD;
			i -= i & -i;
		}
		return ans;
	}
	T sum(int L, int R){
		return (sum(R) - sum(L) + MOD) % MOD;
	}
};
int main(){
  int N;
  cin >> N;
  vector<long long> x(N), y(N);
  for (int i = 0; i < N; i++){
    cin >> x[i] >> y[i];
  }
  vector<pair<long long, long long>> P(N);
  for (int i = 0; i < N; i++){
    P[i] = make_pair(x[i], y[i]);
  }
  sort(P.begin(), P.end());
  for (int i = 0; i < N; i++){
    x[i] = P[i].first;
    y[i] = P[i].second;
  }
  long long x1 = 0, x12 = 0, x2 = 0;
  for (int i = 0; i < N; i++){
    x12 += x[i] * x1;
    x12 %= MOD;
    x1 += x[i];
    x1 %= MOD;
    x2 += x[i] * x[i];
    x2 %= MOD;
  }
  long long y1 = 0, y12 = 0, y2 = 0;
  for (int i = 0; i < N; i++){
    y12 += y[i] * y1;
    y12 %= MOD;
    y1 += y[i];
    y1 %= MOD;
    y2 += y[i] * y[i];
    y2 %= MOD;
  }
  long long ans = 0;
  ans += x2 * (N - 1);
  ans -= x12 * 2 % MOD;
  ans += y2 * (N - 1);
  ans -= y12 * 2 % MOD;
  ans += MOD * 2;
  vector<long long> yc = y;
  sort(yc.begin(), yc.end());
  yc.erase(unique(yc.begin(), yc.end()), yc.end());
  int cnt = yc.size();
  binary_indexed_tree<long long> BIT1(cnt), BITx(cnt), BITy(cnt), BITxy(cnt);
  long long ans2 = 0;
  for (int i = 0; i < N; i++){
    int pos = lower_bound(yc.begin(), yc.end(), y[i]) - yc.begin();
    ans2 += x[i] * y[i] % MOD * BIT1.sum(0, pos) % MOD;
    ans2 -= x[i] * BITy.sum(0, pos) % MOD;
    ans2 -= y[i] * BITx.sum(0, pos) % MOD;
    ans2 += BITxy.sum(0, pos);
    ans2 -= x[i] * y[i] % MOD * BIT1.sum(pos, cnt) % MOD;
    ans2 += x[i] * BITy.sum(pos, cnt);
    ans2 += y[i] * BITx.sum(pos, cnt);
    ans2 -= BITxy.sum(pos, cnt);
    ans2 += MOD * 4;
    ans2 %= MOD;
    BIT1.add(pos, 1);
    BITx.add(pos, x[i]);
    BITy.add(pos, y[i]);
    BITxy.add(pos, x[i] * y[i] % MOD);
  }
  ans2 *= 2;
  ans += ans2;
  ans %= MOD;
  cout << ans << endl;
}
0