結果

問題 No.255 Splarrraaay スプラーレェーーイ
ユーザー uwiuwi
提出日時 2015-07-25 16:42:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,461 ms / 10,000 ms
コード長 8,039 bytes
コンパイル時間 4,338 ms
コンパイル使用メモリ 81,768 KB
実行使用メモリ 83,564 KB
最終ジャッジ日時 2024-10-12 19:29:56
合計ジャッジ時間 17,330 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,396 ms
81,580 KB
testcase_01 AC 1,461 ms
81,672 KB
testcase_02 AC 1,197 ms
81,484 KB
testcase_03 AC 1,150 ms
83,564 KB
testcase_04 AC 51 ms
50,348 KB
testcase_05 AC 1,182 ms
81,636 KB
testcase_06 AC 1,220 ms
81,608 KB
testcase_07 AC 1,211 ms
81,264 KB
testcase_08 AC 1,228 ms
81,224 KB
testcase_09 AC 1,172 ms
81,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package contest;

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

public class Q637_2 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	static long mod = 1000000000000000009L;
	
	void solve()
	{
		long n = nl();
		int Q = ni();
		int[] ts = new int[Q];
		int[] ls = new int[Q];
		int[] rs = new int[Q];
		
		long[] xi = new long[Q*2];
		for(int i = 0;i < Q;i++){
			ts[i] = ni();
			xi[i*2] = nl()<<19|i*2;
			xi[i*2+1] = nl()+1<<19|i*2+1;
		}
		Arrays.sort(xi);
		long[] imap = new long[Q*2];
		int p = 0;
		for(int i = 0;i < Q*2;i++){
			if(i == 0 || xi[i]>>>19 != xi[i-1]>>>19){
				imap[p++] = xi[i]>>>19;
			}
			int ind = (int)(xi[i]&(1L<<19)-1);
			if((ind&1) == 0){
				ls[ind/2] = p-1;
			}else{
				rs[ind/2] = p-1;
			}
		}
		
		long[] score = new long[6];
		long[] best = new long[Q];
		int[] argbest = new int[Q];
		Arrays.fill(best, -1L);
		Arrays.fill(argbest, -1);
		SegmentTreeOverwriteRSQ1D st = new SegmentTreeOverwriteRSQ1D(imap);
		for(int k = 1;k <= 5;k++){
			st.update(0, imap.length);
			for(int i = 0;i < Q;i++){
				int t = ts[i];
				int L = ls[i];
				int R = rs[i];
				if(t == 0){
					long s = st.sum(L, R);
					if(s > best[i]){
						best[i] = s; argbest[i] = k;
					}else if(s == best[i] && argbest[i] != -1){
						argbest[i] = -1;
					}
				}else if(t == k){
					st.add(L, R);
				}else{
					st.update(L, R);
				}
			}
			score[k] = st.sum[1];
		}
		for(int i = 0;i < Q;i++){
			if(ts[i] == 0){
				if(argbest[i] != -1){
					score[argbest[i]] += best[i];
					score[argbest[i]] %= mod;
				}
			}
		}
		for(int i = 1;i <= 5;i++){
			if(i > 1)out.print(" ");
			out.print(score[i]%mod);
		}
		out.println();
	}
	
	public static class SegmentTreeOverwriteRSQ1D {
		public int M, H, N;
		public long[] sum;
		public int[] plus;
		public boolean[] zerocover;
//		public long[] cover;
		private long I = Long.MAX_VALUE;
		public long[] lens;
		public long[] co;
		
		public SegmentTreeOverwriteRSQ1D(long[] co)
		{
			this.co = co;
			N = co.length;
			M = Integer.highestOneBit(Math.max(N-1, 1))<<2;
			H = M>>>1;
			zerocover = new boolean[H];
			plus = new int[H];
			sum = new long[M];
			Arrays.fill(sum, 0L);
			lens = new long[M];
			for(int i = 0;i < N-1;i++){
				lens[i+H] = co[i+1]-co[i];
			}
			for(int i = H-1;i >= 1;i--){
				lens[i] = lens[2*i] + lens[2*i+1];
				propagate(i);
			}
		}
		
		private void propagate(int i)
		{
			if(!zerocover[i]){
				sum[i] = sum[2*i] + sum[2*i+1] + plus[i] * lens[i];
			}else{
				sum[i] = plus[i] * lens[i];
			}
		}
		
		public void update(int l, int r){ update(l, r, 0, H, 1); }
		
		private void update(int l, int r, int cl, int cr, int cur)
		{
			if(l <= cl && cr <= r){
				if(cr == cl+1){
					sum[cur] = 0L;
				}else{
					zerocover[cur] = true;
					plus[cur] = 0;
					propagate(cur);
				}
			}else{
				int mid = cl+cr>>>1;
				boolean bp = false;
				if(zerocover[cur]){
					if(2*cur >= H){
						sum[2*cur] = sum[2*cur+1] = 0L;
					}else{
						zerocover[2*cur] = zerocover[2*cur+1] = true;
						plus[2*cur] = plus[2*cur+1] = 0;
						bp = true;
					}
					zerocover[cur] = false;
				}
				{ // back-propagate plus&slope
					if(2*cur >= H){
						sum[2*cur] += plus[cur] * lens[2*cur];
						sum[2*cur+1] += plus[cur] * lens[2*cur+1];
					}else{
						plus[2*cur] += plus[cur];
						plus[2*cur+1] += plus[cur];
						bp = true;
					}
					plus[cur] = 0;
				}
				if(cl < r && l < mid){
					update(l, r, cl, mid, 2*cur);
				}else if(bp){
					propagate(2*cur);
				}
				if(mid < r && l < cr){
					update(l, r, mid, cr, 2*cur+1);
				}else if(bp){
					propagate(2*cur+1);
				}
				propagate(cur);
			}
		}
		
		public void add(int l, int r){ add(l, r, 0, H, 1); }
		
		private void add(int l, int r, int cl, int cr, int cur)
		{
			if(l <= cl && cr <= r){
				if(cr == cl+1){
					sum[cur] += lens[cur];
				}else{
					plus[cur] += 1;
					propagate(cur);
				}
			}else{
				int mid = cl+cr>>>1;
				boolean bp = false;
				if(zerocover[cur]){
					if(2*cur >= H){
						sum[2*cur] = sum[2*cur+1] = 0L;
					}else{
						zerocover[2*cur] = zerocover[2*cur+1] = true;
						plus[2*cur] = plus[2*cur+1] = 0;
						bp = true;
					}
					zerocover[cur] = false;
				}
				if(cl < r && l < mid){
					add(l, r, cl, mid, 2*cur);
				}else if(bp){
					propagate(2*cur);
				}
				if(mid < r && l < cr){
					add(l, r, mid, cr, 2*cur+1);
				}else if(bp){
					propagate(2*cur+1);
				}
				propagate(cur);
			}
		}
		
		public long sum(int l, int r){ return sum(l, r, 0, H, 1); }
		
		private long sum(int l, int r, int cl, int cr, int cur)
		{
			if(l <= cl && cr <= r){
				return sum[cur];
			}else{
				long inter = co[Math.min(cr, r)] - co[Math.max(cl, l)];
				if(zerocover[cur]){
					long ret = plus[cur] * inter;
					return ret;
				}
				int mid = cl+cr>>>1;
				long ret = 0;
				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);
				}
				ret += plus[cur] * inter;
				return ret;
			}
		}
		
//			public int[] toArray() { return toArray(1, 0, H, new int[H]); }
//			
//			private int[] toArray(int cur, int l, int r, int[] ret)
//			{
//				if(r-l == 1){
//					ret[cur-H] = sum[cur];
//				}else if(cover[cur] != I){
//					Arrays.fill(ret, l, r, cover[cur]);
//				}else{
//					toArray(2*cur, l, l+r>>>1, ret);
//					toArray(2*cur+1, l+r>>>1, r, ret);
//				}
//				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 Q637_2().run(); }
	
	private byte[] inbuf = new byte[1024];
	private 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