結果

問題 No.1649 Manhattan Square
ユーザー SSRSSSRS
提出日時 2021-08-13 22:03:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,202 bytes
コンパイル時間 2,107 ms
コンパイル使用メモリ 181,440 KB
実行使用メモリ 17,564 KB
最終ジャッジ日時 2024-04-14 17:58:17
合計ジャッジ時間 18,287 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 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 AC 275 ms
17,408 KB
testcase_43 AC 2 ms
6,944 KB
testcase_44 AC 2 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;
    BIT1.add(pos, 1);
    BITx.add(pos, x[i]);
    BITy.add(pos, y[i]);
    BITxy.add(pos, x[i] * y[i]);
  }
  ans2 *= 2;
  ans += ans2;
  ans %= MOD;
  cout << ans << endl;
}
0