結果
| 問題 |
No.1054 Union add query
|
| コンテスト | |
| ユーザー |
fgwiebfaoish
|
| 提出日時 | 2020-05-16 03:10:54 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 1,208 ms / 2,000 ms |
| コード長 | 4,791 bytes |
| コンパイル時間 | 878 ms |
| コンパイル使用メモリ | 113,784 KB |
| 実行使用メモリ | 101,480 KB |
| 最終ジャッジ日時 | 2024-09-19 19:06:28 |
| 合計ジャッジ時間 | 9,009 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 8 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
const int mod=(int)1e9+7;
const double eps=1e-11;
static void Main(){
Sc sc=new Sc();
var s=sc.Ia;
var h=new int[s[1]][];
var f1=new int[s[1]];
var f2=new int[s[1]];
Uf uf=new Uf(s[0]+1);
for(int i = 0;i<s[1];i++) {
h[i]=sc.Ia;
if(h[i][0]==1){
uf.Union(h[i][1],h[i][2]);
}
else if(h[i][0]==2){
int r=uf.Root(h[i][1]);
f1[i]=r;
f2[i]=uf.l[r].n;
}
}
var g=new int[s[0]+1];
for(int i = 2;uf.all!=2;i++) {uf.Union(1,i);}
Bit2 bit=new Bit2(s[0]+1);
int k=0;
var e=uf.f[uf.Root(1)];
while(e!=null){
g[e.n]=k++;
e=e.ne;
}
StringBuilder sb=new StringBuilder();
for(int i = 0;i<s[1];i++) {
if(h[i][0]==2){
bit.Ud(g[f1[i]],g[f2[i]],h[i][2]);
}
else if(h[i][0]==3){
sb.Append(bit.Get(g[h[i][1]],g[h[i][1]])+"\n");
}
}
Console.Write(sb);
}
}
public class Bit2{
private int n;
private Bit bit1,bit2;
public Bit2(int n){
this.n=n;
bit1=new Bit(n);
bit2=new Bit(n);
}
public void Ud(int l,int r,long p){
bit1.Ud(l,-p*(l-1));
bit2.Ud(l,p);
bit1.Ud(r+1,p*r);
bit2.Ud(r+1,-p);
}
public long Get(int l,int r){return bit2.Get(r)*r+bit1.Get(r)-bit2.Get(l-1)*(l-1)-bit1.Get(l-1);}
public class Bit{
private int n;
private long[] bit;
public Bit(int n){
this.n=n;
bit=new long[n+1];
}
public void Ud(int a,long p){a++;while(a<=n){bit[a]+=p;a+=a&-a;}}
public long Get(int n){
n++;
long a=0;
while(n>0){a+=bit[n];n-=n&-n;}
return a;
}
public void Clear(){bit=new long[n+1];}
}
}
public class Uf{
public class Nd{
public int n;
public Nd ne;
public Nd(int n){this.n=n;}
}
private int[] arr,hi;
public Nd[] f,l;
public int all;
public Uf(int n){
arr=new int[n];
hi=new int[n];
f=new Nd[n];
l=new Nd[n];
all=n;
for(int i=0;i<n;i++){
arr[i]=-1;
f[i]=l[i]=new Nd(i);
}
}
public bool Union(int a,int b){
a=Root(a);
b=Root(b);
if(a!=b){
if(hi[a]<hi[b]){a^=b;b^=a;a^=b;}
arr[a]+=arr[b];
hi[a]=Max(hi[a],hi[b]+1);
arr[b]=a;
all--;
l[a].ne=f[b];
l[a]=l[b];
return true;
}
return false;
}
public bool Same(int a,int b){return Root(a)==Root(b);}
public int Size(int a){return -arr[Root(a)];}
public int Root(int a){return arr[a]<0?a:arr[a]=Root(arr[a]);}
public void Scan(int a,int m,Action<int> fu){
var e=f[a];
for(int j = 0;j<m;j++) {
fu(e.n);
e=e.ne;
}
}
}
public class Sc{
public int I{get{return int.Parse(Console.ReadLine());}}
public long L{get{return long.Parse(Console.ReadLine());}}
public double D{get{return double.Parse(Console.ReadLine());}}
public string S{get{return Console.ReadLine();}}
public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
public string[] Sa{get{return Console.ReadLine().Split();}}
public object[] Oa{get{return Console.ReadLine().Split();}}
public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
public int[] Ia3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),int.Parse);}
public int[] Ia3(bool a,int b,bool c,int d){return Array.ConvertAll((Console.ReadLine()+" 0 0").Split(),int.Parse);}
public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
public long[] La3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),long.Parse);}
public long[] La3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),long.Parse);}
public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
public double[] Da3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),double.Parse);}
public double[] Da3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" 0 0":"")).Split(),double.Parse);}
public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}
fgwiebfaoish