結果

問題 No.1365 [Cherry 1st Tune] Whose Fault?
ユーザー chocoruskchocorusk
提出日時 2021-01-22 23:28:49
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 600 ms / 2,000 ms
コード長 2,166 bytes
コンパイル時間 1,435 ms
コンパイル使用メモリ 132,644 KB
実行使用メモリ 6,504 KB
最終ジャッジ日時 2023-08-28 11:43:13
合計ジャッジ時間 21,155 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 52 ms
4,380 KB
testcase_14 AC 35 ms
4,376 KB
testcase_15 AC 9 ms
4,376 KB
testcase_16 AC 7 ms
4,376 KB
testcase_17 AC 30 ms
4,380 KB
testcase_18 AC 17 ms
4,376 KB
testcase_19 AC 51 ms
4,380 KB
testcase_20 AC 44 ms
4,380 KB
testcase_21 AC 47 ms
4,380 KB
testcase_22 AC 38 ms
4,376 KB
testcase_23 AC 111 ms
4,712 KB
testcase_24 AC 240 ms
5,168 KB
testcase_25 AC 480 ms
5,416 KB
testcase_26 AC 327 ms
5,332 KB
testcase_27 AC 402 ms
4,380 KB
testcase_28 AC 190 ms
4,376 KB
testcase_29 AC 137 ms
4,384 KB
testcase_30 AC 600 ms
5,680 KB
testcase_31 AC 522 ms
6,140 KB
testcase_32 AC 290 ms
5,704 KB
testcase_33 AC 481 ms
4,688 KB
testcase_34 AC 377 ms
4,804 KB
testcase_35 AC 214 ms
5,392 KB
testcase_36 AC 435 ms
6,504 KB
testcase_37 AC 94 ms
4,376 KB
testcase_38 AC 122 ms
5,652 KB
testcase_39 AC 562 ms
5,480 KB
testcase_40 AC 569 ms
5,412 KB
testcase_41 AC 421 ms
5,680 KB
testcase_42 AC 417 ms
4,456 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 253 ms
6,404 KB
testcase_45 AC 362 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <stack>
#include <array>
#include <list>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
struct unionfind{
    vector<int> as, bs;
    vector<double> ps;
    vector<int> par, sz;
    unionfind() {}
    unionfind(int n):par(n), sz(n, 1), as(n), bs(n), ps(n){
        for(int i=0; i<n; i++) par[i]=i;
    }
    int find(int x){
        if(par[x]==x) return x;
        return par[x]=find(par[x]);
    }
    bool unite(int x, int y){
        x=find(x); y=find(y);
        if(x==y) return false;
        if(sz[x]>sz[y]) swap(x, y);
        par[x]=y;
        as[y]+=as[x];
        bs[y]+=bs[x];
        if(ps[y]<0 || ps[x]<0) ps[y]=-1;
        else ps[y]+=ps[x];
        sz[y]+=sz[x];
        return true;
    }
    bool same(int x, int y){
        return find(x)==find(y);
    }
    int size(int x){
        return sz[find(x)];
    }
    double calc(int x){
        x=find(x);
        if(ps[x]<0) return 0;
        return (double)(as[x]-bs[x])*(as[x]-bs[x])/ps[x];
    }
};
int main()
{
    int n;
    cin>>n;
    int a[100010], b[100010], p[100010];
    for(int i=0; i<n; i++) cin>>a[i];
    for(int i=0; i<n; i++) cin>>b[i];
    for(int i=0; i<n; i++) cin>>p[i];
    int q; cin>>q;
    unionfind uf(n);
    for(int i=0; i<n; i++){
        uf.as[i]=a[i];
        uf.bs[i]=b[i];
        uf.ps[i]=-1;
        if(p[i]>0) uf.ps[i]=1.0/p[i];
    }
    double ans=0;
    for(int i=0; i<n; i++){
        ans+=(double)p[i]*(a[i]-b[i])*(a[i]-b[i]);
    }
    while(q--){
        int x, y; cin>>x>>y; x--; y--;
        double ans1=ans;
        ans1-=uf.calc(x);
        ans1-=uf.calc(y);
        if(uf.unite(x, y)){
            ans1+=uf.calc(x);
            ans=ans1;
        }
        printf("%.9lf\n", ans);
    }
    return 0;
}
0