結果

問題 No.1649 Manhattan Square
ユーザー yakkiyakki
提出日時 2021-08-21 08:36:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 278 ms / 3,000 ms
コード長 3,579 bytes
コンパイル時間 1,383 ms
コンパイル使用メモリ 135,024 KB
実行使用メモリ 17,364 KB
最終ジャッジ日時 2024-10-14 17:29:24
合計ジャッジ時間 12,966 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 5 ms
5,248 KB
testcase_03 AC 5 ms
5,248 KB
testcase_04 AC 5 ms
5,248 KB
testcase_05 AC 5 ms
5,248 KB
testcase_06 AC 5 ms
5,248 KB
testcase_07 AC 254 ms
17,212 KB
testcase_08 AC 256 ms
17,192 KB
testcase_09 AC 261 ms
17,200 KB
testcase_10 AC 256 ms
17,208 KB
testcase_11 AC 264 ms
17,212 KB
testcase_12 AC 251 ms
17,108 KB
testcase_13 AC 258 ms
17,236 KB
testcase_14 AC 255 ms
17,240 KB
testcase_15 AC 255 ms
17,236 KB
testcase_16 AC 248 ms
17,360 KB
testcase_17 AC 246 ms
17,112 KB
testcase_18 AC 247 ms
17,236 KB
testcase_19 AC 251 ms
17,364 KB
testcase_20 AC 248 ms
17,236 KB
testcase_21 AC 253 ms
17,236 KB
testcase_22 AC 268 ms
17,236 KB
testcase_23 AC 257 ms
17,236 KB
testcase_24 AC 258 ms
17,232 KB
testcase_25 AC 256 ms
17,236 KB
testcase_26 AC 267 ms
17,240 KB
testcase_27 AC 257 ms
17,236 KB
testcase_28 AC 257 ms
17,240 KB
testcase_29 AC 263 ms
17,236 KB
testcase_30 AC 265 ms
17,364 KB
testcase_31 AC 259 ms
17,236 KB
testcase_32 AC 260 ms
17,236 KB
testcase_33 AC 278 ms
17,236 KB
testcase_34 AC 267 ms
17,232 KB
testcase_35 AC 265 ms
17,240 KB
testcase_36 AC 252 ms
17,236 KB
testcase_37 AC 257 ms
17,360 KB
testcase_38 AC 258 ms
17,236 KB
testcase_39 AC 254 ms
17,364 KB
testcase_40 AC 255 ms
17,240 KB
testcase_41 AC 252 ms
17,264 KB
testcase_42 AC 223 ms
17,236 KB
testcase_43 AC 1 ms
6,820 KB
testcase_44 AC 1 ms
6,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<bitset>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<functional>
#include<cstdio>
#include<cstdlib>
#include<numeric>
#include<ctime>
//#include<atcoder/all>
using namespace std;
//using namespace atcoder;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
//#define MOD 1000000007
#define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592
#define PI acos(-1.0)

const double EPS = 1e-10;

using ll = long long;
using Pi = pair<int,int>;
using Pl = pair<ll,ll>;
//using mint = modint998244353;

int dh[] = {-1,1,0,0};
int dw[] = {0,0,1,-1};

template<typename T>
vector<T> compress(vector<T> &v){
    vector<T> res = v;
    sort(res.begin(),res.end());
    res.erase(unique(res.begin(),res.end()),res.end());
    for(auto &u : v){u = lower_bound(res.begin(),res.end(),u)-res.begin();}
    return res;
}

template<typename T>
struct BinaryIndexedTree{
	vector<T> bit;
	BinaryIndexedTree(int sz){
		bit.resize(sz+1);
	}
	void add(int k, T x){ //k番目にxを加える
		for(int i = k; i < (int)bit.size(); i |= i+1) bit[i] += x;	
	}
	T sum(int k){ //[0,k)の区間の総和を求める
		T res = 0;      
		for(int i = k-1; i >= 0; i = (i&(i+1))-1){
			res += bit[i];
		}
		return res;
	}
};



int main(){
    int n; cin >> n;
    vector<ll> x(n),y(n);
    vector<Pl> xy(n);
    rep(i,n){
        cin >> x[i] >> y[i];
        xy[i].first = x[i];
        xy[i].second = y[i];
    }

    auto f = [&](auto v){
        ll s1 = 0,s2 = 0,s3 = 0;
        rep(i,n-1){
            s1 += v[i]*v[i]%MOD*(n-i-1)%MOD;
            s1 %= MOD;
        }
        vector<ll> sum(n+1);
        rep(i,n) sum[i+1] = (sum[i]+v[i]*v[i]%MOD)%MOD;
        rep(i,n-1){
            s3 += (sum[n]-sum[i+1]+MOD)%MOD;
            s3 %= MOD;
        }
        vector<ll> sum2(n+1);
        rep(i,n) sum2[i+1] = (sum2[i]+v[i])%MOD;
        rep(i,n-1){
            s2 += v[i]*(sum2[n]-sum2[i+1]+MOD)%MOD;
            s2 %= MOD;
        }
        s2 *= 2;
        s2 %= MOD;
        ll res = (s1+s3-s2+MOD)%MOD;
        return res;
    };

    ll ans = f(x)+f(y);

    sort(xy.begin(),xy.end());
    vector<ll> comp;
    rep(i,n) comp.push_back(xy[i].second);
    compress(comp);

    BinaryIndexedTree<ll> bit1(n),bit2(n),bit3(n),bit4(n); //1,x,y,x*yの和

    ll ans2 = 0;
    rep(i,n){
        ll l = xy[i].first;
        ll r = xy[i].second;
        ll a1 = bit1.sum(comp[i]+1)%MOD;
        ll a2 = bit2.sum(comp[i]+1)%MOD;
        ll a3 = bit3.sum(comp[i]+1)%MOD;
        ll a4 = bit4.sum(comp[i]+1)%MOD;
        ll b1 = (bit1.sum(n)-bit1.sum(comp[i]+1))%MOD;
        ll b2 = (bit2.sum(n)-bit2.sum(comp[i]+1))%MOD;
        ll b3 = (bit3.sum(n)-bit3.sum(comp[i]+1))%MOD;
        ll b4 = (bit4.sum(n)-bit4.sum(comp[i]+1))%MOD;
        ans2 += l*r%MOD*a1%MOD;
        ans2 %= MOD;
        ans2 -= r*a2%MOD;
        ans2 -= l*a3%MOD;
        ans2 %= MOD;
        if(ans2 < 0) ans2 += MOD;
        ans2 += a4%MOD;
        ans2 %= MOD;

        ans2 += l*b3%MOD;
        ans2 %= MOD;
        ans2 += r*b2%MOD;
        ans2 %= MOD;
        ans2 -= l*r%MOD*b1%MOD;
        ans2 -= b4%MOD;
        ans2 %= MOD;
        if(ans2 < 0) ans2 += MOD;
        bit1.add(comp[i],1);
        bit2.add(comp[i],l);
        bit3.add(comp[i],r);
        bit4.add(comp[i],l*r%MOD);

    }
    ans2 *= 2;
    ans += ans2;
    ans %= MOD;
    cout << ans << endl;
}

0