結果
| 問題 |
No.1649 Manhattan Square
|
| コンテスト | |
| ユーザー |
Nachia
|
| 提出日時 | 2024-04-18 02:51:24 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 407 ms / 3,000 ms |
| コード長 | 1,925 bytes |
| コンパイル時間 | 1,269 ms |
| コンパイル使用メモリ | 90,612 KB |
| 最終ジャッジ日時 | 2025-02-21 02:58:05 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 43 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <atcoder/modint>
#include <atcoder/fenwicktree>
using namespace std;
using i64 = long long;
#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() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
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;
}
Nachia