結果

問題 No.1054 Union add query
ユーザー snow39snow39
提出日時 2020-05-15 22:22:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 800 ms / 2,000 ms
コード長 2,788 bytes
コンパイル時間 1,116 ms
コンパイル使用メモリ 102,092 KB
実行使用メモリ 150,136 KB
最終ジャッジ日時 2023-10-19 15:01:23
合計ジャッジ時間 6,187 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 481 ms
35,264 KB
testcase_04 AC 800 ms
146,712 KB
testcase_05 AC 315 ms
20,052 KB
testcase_06 AC 333 ms
63,544 KB
testcase_07 AC 247 ms
63,544 KB
testcase_08 AC 292 ms
63,544 KB
testcase_09 AC 604 ms
145,812 KB
testcase_10 AC 309 ms
150,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

class DisjointSet{
public:
  vector<int> rank,p;
  vector<int> sz;
 
  DisjointSet(){}
  DisjointSet(int size){
    rank.resize(size,0);
    p.resize(size,0);
    sz.resize(size,0);
    for(int i=0;i<size;i++) makeSet(i);
  }
 
  void makeSet(int x){
    p[x]=x;
    rank[x]=0;
    sz[x]=1;
  }
 
  bool same(int x,int y){
    return findSet(x)==findSet(y);
  }
 
  void unite(int x,int y){
    if(same(x,y)) return;
    link(findSet(x),findSet(y));
  }
 
  void link(int x,int y){
    if(rank[x]>rank[y]){
      p[y]=x;
      sz[x]+=sz[y];
    }else{
      p[x]=y;
      sz[y]+=sz[x];
      if(rank[x]==rank[y]){
        rank[y]++;
      }
    }
  }
 
  int findSet(int x){
    if(x!=p[x]){
      p[x]=findSet(p[x]);
    }
    return p[x];
  }
 
  int findSize(int x){
    return sz[findSet(x)];
  }
};

int main(){
  int N,Q;
  scanf("%d %d",&N,&Q);
  
  vector<vector<int>> addTimes(N),sum(N);
  rep(i,N){
    addTimes[i].push_back(-1);
    sum[i].push_back(0);
  }
  DisjointSet us(N);
  vector<vector<int>> changeTimes(N),leader(N);
  rep(i,N){
    leader[i].push_back(i);
    changeTimes[i].push_back(0);
  }
  vector<vector<int>> state(N);
  rep(i,N) state[i].push_back(i);

  for(int q=1;q<=Q;q++){
    int T,A,B;
    scanf("%d %d %d",&T,&A,&B);

    if(T==1){
      A--;B--;
      if(us.same(A,B)) continue;

      int preA=us.findSet(A);
      int preB=us.findSet(B);
      us.unite(A,B);
      int target=us.findSet(A);
      if(preA!=target) swap(preA,preB);
      for(auto n:state[preB]){
        changeTimes[n].push_back(q);
        leader[n].push_back(target);
        state[target].push_back(n);
      }
      state[preB].clear();

    }else if(T==2){
      A--;
      int target=us.findSet(A);
      addTimes[target].push_back(q);
      int s=sum[target].back();
      sum[target].push_back(s+B);

    }else if(T==3){
      A--;

      int ed=q;
      int ans=0;
      for(int i=leader[A].size()-1;i>=0;i--){
        int st=changeTimes[A][i];
        int target=leader[A][i];
        int r=lower_bound(addTimes[target].begin(),addTimes[target].end(),ed)-addTimes[target].begin();
        int l=lower_bound(addTimes[target].begin(),addTimes[target].end(),st)-addTimes[target].begin();
        r--;l--;
        ans+=sum[target][r]-sum[target][l];
        ed=st;
      }
      printf("%d\n",ans);
    }
  }

  return 0;
}
0