結果

問題 No.1170 Never Want to Walk
ユーザー fgwiebfaoishfgwiebfaoish
提出日時 2020-08-14 22:38:23
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 385 ms / 2,000 ms
コード長 9,171 bytes
コンパイル時間 3,459 ms
コンパイル使用メモリ 109,324 KB
実行使用メモリ 54,588 KB
最終ジャッジ日時 2023-07-31 23:11:01
合計ジャッジ時間 10,966 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
20,972 KB
testcase_01 AC 57 ms
20,932 KB
testcase_02 AC 58 ms
20,792 KB
testcase_03 AC 57 ms
20,840 KB
testcase_04 AC 56 ms
18,744 KB
testcase_05 AC 57 ms
18,824 KB
testcase_06 AC 59 ms
20,920 KB
testcase_07 AC 59 ms
20,968 KB
testcase_08 AC 58 ms
20,852 KB
testcase_09 AC 57 ms
22,820 KB
testcase_10 AC 57 ms
20,972 KB
testcase_11 AC 56 ms
20,860 KB
testcase_12 AC 64 ms
20,872 KB
testcase_13 AC 66 ms
18,888 KB
testcase_14 AC 66 ms
20,836 KB
testcase_15 AC 66 ms
20,952 KB
testcase_16 AC 65 ms
21,136 KB
testcase_17 AC 66 ms
22,920 KB
testcase_18 AC 64 ms
19,092 KB
testcase_19 AC 65 ms
20,940 KB
testcase_20 AC 66 ms
18,832 KB
testcase_21 AC 66 ms
20,976 KB
testcase_22 AC 67 ms
20,824 KB
testcase_23 AC 65 ms
21,068 KB
testcase_24 AC 64 ms
20,888 KB
testcase_25 AC 65 ms
18,984 KB
testcase_26 AC 67 ms
20,880 KB
testcase_27 AC 378 ms
50,404 KB
testcase_28 AC 385 ms
54,588 KB
testcase_29 AC 376 ms
48,540 KB
testcase_30 AC 382 ms
50,732 KB
testcase_31 AC 382 ms
50,372 KB
testcase_32 AC 340 ms
50,372 KB
testcase_33 AC 358 ms
50,512 KB
testcase_34 AC 351 ms
50,756 KB
testcase_35 AC 345 ms
50,540 KB
testcase_36 AC 339 ms
50,432 KB
testcase_37 AC 342 ms
48,520 KB
testcase_38 AC 361 ms
49,008 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

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;
using nint=System.Int32;
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 a=sc.Ia;
		var avl=new Avl();
		for(int i = 0;i<s[0];i++) {
			avl.Ud(a[i],i);
		}
		Uf uf=new Uf(s[0]);
		for(int i = 0;i<s[0];i++) {
			var e=avl.Lb(a[i]+s[1]);
			var g=avl.Ub(a[i]+s[2]);
			if(e!=null&&g!=null&&e.n<=g.n){
				uf.Union(i,(int)e.d);
				uf.Union(i,(int)g.d);
				int p=e.n;
				var f=avl.Lb(p+1);
				while(f!=null&&f.n<g.n){
					uf.Union(i,(int)f.d);
					p=f.n;
					avl.Dl(p);
					f=avl.Lb(p+1);
				}
			}
		}
		StringBuilder sb=new StringBuilder();
		for(int i = 0;i<s[0];i++) {
			sb.Append(uf.Size(i)+"\n");
		}
		Console.Write(sb);
	}
}
public class Uf{
	private int[] arr,hi;
	public int all;
	public Uf(int n){
		arr=new int[n];
		hi=new int[n];
		all=n;
		for(int i=0;i<n;i++){arr[i]=-1;}
	}
	public void Union(int a,int b){
		a=Root(a);
		b=Root(b);
		if(a!=b){
			if(hi[a]<hi[b]){(a,b)=(b,a);}
			arr[a]+=arr[b];
			hi[a]=Max(hi[a],hi[b]+1);
			arr[b]=a;
			all--;
		}
	}
	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 class Avl{
	public class Nd{
		public int h=1,c=1;
		public readonly nint n;
		public long d,ds;
		public Nd l,r;
		public Nd(nint n,long d,Nd du){this.n=n;this.d=d;ds=d;l=du;r=du;}
		public override string ToString()=>"(n:"+n.ToString()+" d:"+d.ToString()+")";
	}
	public Nd root,du;
	public int cnt=0;
	public Avl(){
		du=new Nd(nint.MinValue,0,du);du.h=0;du.c=0;
		root=du;
	}
	public void Ud(nint n,long d){
		if(root==du){root=new Nd(n,d,du);cnt++;}
		else if(root.n==n){root.d+=d;root.ds+=d;}
		else if(Fu(root,n,d)){
			if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);}
			cnt++;
		}
	}
	private bool Fu(Nd t,nint n,long d){
		t.ds+=d;
		bool bo=false;
		if(t.n>n){
			if(t.l!=du){
				if(t.l.n==n){t.l.d+=d;t.l.ds+=d;}
				else if(bo=Fu(t.l,n,d)){
					t.c++;
					if(Abs(t.l.l.h-t.l.r.h)>1){t.l=Rotate(t.l);}
					t.h=Max(t.l.h,t.r.h)+1;
				}
			}
			else{t.l=new Nd(n,d,du);bo=true;t.c++;t.h=Max(t.l.h,t.r.h)+1;}
		}
		else{
			if(t.r!=du){
				if(t.r.n==n){t.r.d+=d;t.r.ds+=d;}
				else if(bo=Fu(t.r,n,d)){
					t.c++;
					if(Abs(t.r.l.h-t.r.r.h)>1){t.r=Rotate(t.r);}
					t.h=Max(t.l.h,t.r.h)+1;
				}
			}
			else{t.r=new Nd(n,d,du);bo=true;t.c++;t.h=Max(t.l.h,t.r.h)+1;}
		}
		return bo;
	}
	public void Ud2(nint n,long d){
		if(root==du){root=new Nd(n,d,du);}
		else{
			Fu2(root,n,d);
			if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);}
		}
		cnt++;
	}
	private void Fu2(Nd t,nint n,long d){
		t.ds+=d;
		t.c++;
		if(t.n>n){
			if(t.l!=du){
				Fu2(t.l,n,d);
				if(Abs(t.l.l.h-t.l.r.h)>1){t.l=Rotate(t.l);}
			}
			else{t.l=new Nd(n,d,du);}
		}
		else{
			if(t.r!=du){
				Fu2(t.r,n,d);
				if(Abs(t.r.l.h-t.r.r.h)>1){t.r=Rotate(t.r);}
			}
			else{t.r=new Nd(n,d,du);}
		}
		t.h=Max(t.l.h,t.r.h)+1;
	}
	private Nd Rotate(Nd t){
		Nd nd=du;
		if(t.l.h>t.r.h){
			if(t.l.l.h>t.l.r.h){
				nd=t.l;t.l=t.l.r;nd.r=t;
				Ft(t);
			}
			else{
				nd=t.l.r;
				t.l.r=nd.l;nd.l=t.l;
				Ft(t.l);
				t.l=nd.r;nd.r=t;
				Ft(t);
			}
		}
		else{
			if(t.r.l.h>t.r.r.h){
				nd=t.r.l;
				t.r.l=nd.r;nd.r=t.r;
				Ft(t.r);
				t.r=nd.l;nd.l=t;
				Ft(t);
			}
			else{
				nd=t.r;t.r=t.r.l;nd.l=t;
				Ft(t);
			}
		}
		Ft(nd);
		return nd;
	}
	private void Ft(Nd t){
		t.h=Max(t.l.h,t.r.h)+1;
		t.c=t.l.c+t.r.c+1;
		t.ds=t.l.ds+t.r.ds+t.d;
	}
	public bool Dl(nint n){
		if(cnt==0){return false;}
		Nd t=root;
		if(t.n==n){
			root=Fd2(root);
			if(cnt==0){return true;}
			else if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);}
			else{Ft(root);}
			return true;
		}
		bool bo=Fd1(root,n);
		if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);}
		else{Ft(root);}
		return bo;
	}
	private bool Fd1(Nd t,nint n){
		if(t.n>n){
			if(t.l==du){return false;}
			else if(t.l.n!=n){
				bool bo=Fd1(t.l,n);
				if(Abs(t.l.l.h-t.l.r.h)>1){t.l=Rotate(t.l);}
				else{Ft(t.l);}
				return bo;
			}
			else{
				t.l=Fd2(t.l);
				Ft(t);
				return true;
			}
		}
		else{
			if(t.r==du){return false;}
			else if(t.r.n!=n){
				bool bo=Fd1(t.r,n);
				if(Abs(t.r.l.h-t.r.r.h)>1){t.r=Rotate(t.r);}
				else{Ft(t.r);}
				return bo;
			}
			else{
				t.r=Fd2(t.r);
				Ft(t);
				return true;
			}
		}
	}
	private Nd Fd2(Nd dn){
		cnt--;
		if(dn.l==du){return dn.r;}
		if(dn.r==du){return dn.l;}
		Nd u=dn.l;
		if(u.r==du){
			u.r=dn.r;
			Ft(u);
			return u;
		}
		u=Fd3(u,dn);
		if(Abs(dn.l.l.h-dn.l.r.h)>1){u.l=Rotate(dn.l);}
		else{Ft(dn.l);}
		Ft(u);
		return u;
	}
	private Nd Fd3(Nd u,Nd dn){
		if(u.r.r!=du){
			Nd v=Fd3(u.r,dn);
			if(Abs(u.r.l.h-u.r.r.h)>1){u.r=Rotate(u.r);}
			else{Ft(u.r);}
			return v;
		}
		else{
			Nd v=u.r;u.r=u.r.l;v.l=dn.l;v.r=dn.r;
			return v;
		}
	}
	public Nd Hs(nint n){
		Nd t=root;
		if(cnt==0){return null;}
		if(t.n==n){return t;}
		Nd a=null;
		while(true){
			if(t.n>n){
				if(t.l==du){break;}
				else if(t.l.n!=n){t=t.l;}
				else{a=t.l;break;}
			}
			else{
				if(t.r==du){break;}
				else if(t.r.n!=n){t=t.r;}
				else{a=t.r;break;}
			}
		}
		return a;
	}
	public Nd Ra(int n){return cnt>=n&&cnt!=0?Fr(root,n):null;}
	private Nd Fr(Nd t,int n){
		if(t.l.c>n-1){return Fr(t.l,n);}
		if(t.l.c<n-1){return Fr(t.r,n-t.l.c-1);}
		return t;
	}
	public Nd Lb(nint n){return cnt!=0?Flb(root,n):null;}
	private Nd Flb(Nd t,nint n){
		if(t.n<n){
			if(t.r==du){return null;}
			return Flb(t.r,n);
		}
		if(t.n>n){
			if(t.l==du){return t;}
			Nd u=Flb(t.l,n);
			return u==null?t:u;
		}
		return t;
	}
	public Nd Ub(nint n){return cnt!=0?Fub(root,n):null;}
	private Nd Fub(Nd t,nint n){
		if(t.n>n){
			if(t.l==du){return null;}
			return Fub(t.l,n);
		}
		if(t.n<n){
			if(t.r==du){return t;}
			Nd u=Fub(t.r,n);
			return u==null?t:u;
		}
		return t;
	}
	public int Rg(nint a,nint b){return cnt!=0?Frg(root,a,b,false,false):0;}
	private int Frg(Nd t,nint a,nint b,bool l,bool r){
		if(t.h==0){return 0;}
		if(l&&r){return t.c;}
		if(t.n<a){return Frg(t.r,a,b,t.n>=a,r);}
		if(t.n>b){return Frg(t.l,a,b,l,t.n<=b);}
		return Frg(t.l,a,b,l,t.n<=b)+Frg(t.r,a,b,t.n>=a,r)+1;
	}
	public long Rd(nint a,nint b){return cnt!=0?Frd(root,a,b,false,false):0;}
	private long Frd(Nd t,nint a,nint b,bool l,bool r){
		if(t.h==0){return 0;}
		if(l&&r){return t.ds;}
		if(t.n<a){return Frd(t.r,a,b,t.n>=a,r);}
		if(t.n>b){return Frd(t.l,a,b,l,t.n<=b);}
		return Frd(t.l,a,b,l,t.n<=b)+Frd(t.r,a,b,t.n>=a,r)+t.d;
	}
	public void Ad(int n,int d){
		Nd t=root;
		if(cnt==0){return;}
		if(t.n==n){t.d+=d;t.ds+=d;return;}
		while(true){
			t.ds+=d;
			if(t.n>n){
				if(t.l==du){break;}
				else if(t.l.n!=n){t=t.l;}
				else{t.l.d+=d;t.l.ds+=d;break;}
			}
			else{
				if(t.r==du){break;}
				else if(t.r.n!=n){t=t.r;}
				else{t.r.d+=d;t.r.ds+=d;break;}
			}
		}
	}
	public void En(Action<Nd> f){if(cnt>0){Fen(root,f);}}
	private void Fen(Nd t,Action<Nd> f){
		f(t);
		if(t.l!=du){Fen(t.l,f);}
		if(t.r!=du){Fen(t.r,f);}
	}
}
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(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),int.Parse);}
	public int[] Ia3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),long.Parse);}
	public long[] La3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).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;}
}
0