結果

問題 No.880 Yet Another Segment Tree Problem
ユーザー uwiuwi
提出日時 2020-09-06 11:29:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,173 ms / 5,000 ms
コード長 7,866 bytes
コンパイル時間 3,786 ms
コンパイル使用メモリ 89,536 KB
実行使用メモリ 77,056 KB
最終ジャッジ日時 2023-10-24 02:48:27
合計ジャッジ時間 25,007 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
53,424 KB
testcase_01 AC 88 ms
53,336 KB
testcase_02 AC 92 ms
55,092 KB
testcase_03 AC 90 ms
54,920 KB
testcase_04 AC 88 ms
54,984 KB
testcase_05 AC 81 ms
54,008 KB
testcase_06 AC 75 ms
53,444 KB
testcase_07 AC 86 ms
54,756 KB
testcase_08 AC 89 ms
55,016 KB
testcase_09 AC 93 ms
54,872 KB
testcase_10 AC 88 ms
54,976 KB
testcase_11 AC 1,104 ms
76,520 KB
testcase_12 AC 1,105 ms
76,264 KB
testcase_13 AC 818 ms
73,816 KB
testcase_14 AC 952 ms
77,056 KB
testcase_15 AC 1,122 ms
76,292 KB
testcase_16 AC 1,162 ms
76,296 KB
testcase_17 AC 1,044 ms
76,488 KB
testcase_18 AC 1,020 ms
76,244 KB
testcase_19 AC 480 ms
70,392 KB
testcase_20 AC 487 ms
69,756 KB
testcase_21 AC 467 ms
69,388 KB
testcase_22 AC 456 ms
69,320 KB
testcase_23 AC 498 ms
69,340 KB
testcase_24 AC 436 ms
72,000 KB
testcase_25 AC 437 ms
71,176 KB
testcase_26 AC 442 ms
71,200 KB
testcase_27 AC 452 ms
71,152 KB
testcase_28 AC 436 ms
71,112 KB
testcase_29 AC 956 ms
75,076 KB
testcase_30 AC 1,151 ms
76,388 KB
testcase_31 AC 1,173 ms
76,848 KB
testcase_32 AC 288 ms
64,844 KB
testcase_33 AC 320 ms
65,236 KB
testcase_34 AC 326 ms
65,388 KB
testcase_35 AC 312 ms
65,236 KB
testcase_36 AC 323 ms
65,292 KB
testcase_37 AC 318 ms
65,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package hackerearth.easy2009;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

class D3 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int n = ni(), m = ni();
		int[] a = na(n);
		SegmentTreeRGcdizeRUpdateRMaxQRSQ st = new SegmentTreeRGcdizeRUpdateRMaxQRSQ(a);
		for(int z = 0;z < m;z++){
			int t = ni();
			if(t == 1){
				int l = ni()-1;
				int r = ni()-1;
				int x = ni();
				st.update(l, r+1, x);
			}else if(t == 2){
				int l = ni()-1;
				int r = ni()-1;
				int x = ni();
				st.gcdize(l, r+1, x);
			}else if(t == 3){
				int l = ni()-1;
				int r = ni()-1;
				out.println(st.max(l, r+1));
			}else{
				int l = ni()-1;
				int r = ni()-1;
				out.println(st.sum(l, r+1));
			}
			/*
			tr(st.lcms);
			tr(st.covers);
			tr(st.sums);
			*/
		}
	}
	
	public static class SegmentTreeRGcdizeRUpdateRMaxQRSQ {
		public int M, H, N;
		public int[] maxs;
		public long[] sums;
		public long[] lcms;
		public int[] covers;
		private long I = Long.MAX_VALUE / 8;
		
		public SegmentTreeRGcdizeRUpdateRMaxQRSQ(int[] a)
		{
			N = a.length;
			M = Integer.highestOneBit(Math.max(N-1, 1))<<2;
			H = M>>>1;
			
			maxs = new int[M];
			sums = new long[M];
			lcms = new long[M];
			covers = new int[M];
			for(int i = 0;i < N;i++){
				sums[H+i] = lcms[H+i] = maxs[H+i] = covers[H+i] = a[i];
			}
			for(int i = H-1;i >= 1;i--)propagate(i);
		}
		
		private void propagate(int cur)
		{
			if(cur >= H){
				lcms[cur] = sums[cur] = maxs[cur] = covers[cur];
			}else{
				int L = 2*cur, R = 2*cur+1;
				if(covers[cur] == 0){
					lcms[cur] = lcm(lcms[L], lcms[R]);
					maxs[cur] = Math.max(maxs[L], maxs[R]);
					sums[cur] = sums[L] + sums[R];
				}else{
					int len = H/Integer.highestOneBit(cur);
					lcms[cur] = covers[cur];
					maxs[cur] = covers[cur];
					sums[cur] = (long)covers[cur] * len;
				}
			}
		}
		
		private long lcm(long a, long b)
		{
			if(a >= I || b >= I)return I;
			if(a == 0)return b;
			if(b == 0)return a;
			long ad = a / gcd(a, b);
			if((double)ad * b >= I)return I;
			return ad*b;
		}
		
		/*
		public static long gcd(long a, long b) {
			while (b > 0) {
				long c = a;
				a = b;
				b = c % b;
			}
			return a;
		}
		
		public static int gcd(int a, int b) {
			while (b > 0) {
				int c = a;
				a = b;
				b = c % b;
			}
			return a;
		}
		*/
		
		
		public static int gcd(int a, int b) {
			if(a == 0)return b;
			if(b == 0)return a;
			
			int ashift = Integer.numberOfTrailingZeros(a);
			int bshift = Integer.numberOfTrailingZeros(b);
			a >>>= ashift;
			b >>>= bshift;
			while(b != a){
				if(a > b){
					int t = a; a = b; b = t;
				}
				b -= a;
				b >>>= Integer.numberOfTrailingZeros(b);
			}
			
			return a<<Math.min(ashift, bshift);
		}
		
		public static long gcd(long a, long b) {
			if(a == 0)return b;
			if(b == 0)return a;
			
			int ashift = Long.numberOfTrailingZeros(a);
			int bshift = Long.numberOfTrailingZeros(b);
			a >>>= ashift;
			b >>>= bshift;
			while(b != a){
				if(a > b){
					long t = a; a = b; b = t;
				}
				b -= a;
				b >>>= Long.numberOfTrailingZeros(b);
			}
			
			return a<<Math.min(ashift, bshift);
		}
		
		public void gcdize(int l, int r, int v) {
			gcdize(l, r, v, 0, H, 1);
		}
		
		void push(int cur)
		{
			if(cur < H && covers[cur] != 0){
				// push
				for(int i = 2*cur;i <= 2*cur+1;i++){
					covers[i] = covers[cur];
					propagate(i);
				}
				covers[cur] = 0;
			}
		}
		
		boolean allsame(int cur)
		{
			return (long)maxs[cur] * (H/Integer.highestOneBit(cur)) == sums[cur];
		}
		
		protected void gcdize(int l, int r, int v, int cl, int cr, int cur)
		{
			if(v % lcms[cur] == 0){
				// not need to dig further
				return;
			}
			if(l <= cl && cr <= r){
				if(allsame(cur)){
					// all nodes have same value
					update(l, r, gcd(v, maxs[cur]), cl, cr, cur);
					return;
				}
			}
			push(cur);
			
			int mid = cl+cr>>>1;
			if(cl < r && l < mid)gcdize(l, r, v, cl, mid, 2*cur);
			if(mid < r && l < cr)gcdize(l, r, v, mid, cr, 2*cur+1);
			propagate(cur);
		}
		
		public void update(int l, int r, int v) {
			update(l, r, v, 0, H, 1);
		}
		
		protected void update(int l, int r, int v, int cl, int cr, int cur)
		{
			if(l <= cl && cr <= r){
				covers[cur] = v;
				propagate(cur);
				return;
			}
			
			push(cur);
			int mid = cl+cr>>>1;
			if(cl < r && l < mid)update(l, r, v, cl, mid, 2*cur);
			if(mid < r && l < cr)update(l, r, v, mid, cr, 2*cur+1);
			propagate(cur);
		}
		
		public long sum(int l, int r) {
			return sum(l, r, 0, H, 1);
		}
		
		protected long sum(int l, int r, int cl, int cr, int cur)
		{
			if(l <= cl && cr <= r){
				return sums[cur];
			}
			
			push(cur);
			
			long ret = 0;
			int mid = cl+cr>>>1;
			if(cl < r && l < mid)ret += sum(l, r, cl, mid, 2*cur);
			if(mid < r && l < cr)ret += sum(l, r, mid, cr, 2*cur+1);
			return ret;
		}
		
		public int max(int l, int r) {
			return max(l, r, 0, H, 1);
		}
		
		protected int max(int l, int r, int cl, int cr, int cur)
		{
			if(l <= cl && cr <= r){
				return maxs[cur];
			}
			
			push(cur);
			
			int ret = 0;
			int mid = cl+cr>>>1;
			if(cl < r && l < mid)ret = Math.max(ret, max(l, r, cl, mid, 2*cur));
			if(mid < r && l < cr)ret = Math.max(ret, max(l, r, mid, cr, 2*cur+1));
			return ret;
		}
	}
	
	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
	}
	
	public static void main(String[] args) throws Exception { new D3().run(); }
	
	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;
	
	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}
0