結果

問題 No.749 クエリ全部盛り
ユーザー uwiuwi
提出日時 2018-10-19 22:58:54
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,774 ms / 3,000 ms
コード長 7,589 bytes
コンパイル時間 4,697 ms
コンパイル使用メモリ 85,692 KB
実行使用メモリ 153,148 KB
最終ジャッジ日時 2024-11-18 22:11:07
合計ジャッジ時間 19,252 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 171 ms
97,216 KB
testcase_01 AC 168 ms
97,180 KB
testcase_02 AC 173 ms
97,176 KB
testcase_03 AC 174 ms
97,188 KB
testcase_04 AC 167 ms
97,104 KB
testcase_05 AC 266 ms
102,508 KB
testcase_06 AC 267 ms
102,040 KB
testcase_07 AC 263 ms
102,260 KB
testcase_08 AC 271 ms
102,552 KB
testcase_09 AC 273 ms
102,252 KB
testcase_10 AC 384 ms
105,708 KB
testcase_11 AC 378 ms
105,656 KB
testcase_12 AC 381 ms
106,024 KB
testcase_13 AC 381 ms
105,796 KB
testcase_14 AC 377 ms
105,856 KB
testcase_15 AC 1,738 ms
152,768 KB
testcase_16 AC 1,772 ms
153,148 KB
testcase_17 AC 1,751 ms
152,876 KB
testcase_18 AC 1,772 ms
152,792 KB
testcase_19 AC 1,774 ms
152,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package contest181019;
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 F2 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	// 01123
	//  112
	
	void solve()
	{
		// 0:sum
		// 1:update
		// 2:add
		// 3:mul
		// 4:add fibonacci
		// 0 1 1 2 3 5 8
		// 0 0 0 0 1 1 2
		// 0 1 1 2 4 6 10
		
		
		int n = ni(), Q = ni();
		
		long[] F = new long[2000005];
		F[0] = 0;
		F[1] = 1;
		for(int i = 2;i < 2000005;i++) {
			F[i] = F[i-1] + F[i-2];
			F[i] %= mod;
		}
		
		long[] cumw = new long[2000000+1];
		for(int i = 0;i < 2000000+1;i++) {
			cumw[i] = i;
		}
		
		long[] cumwf = new long[2000000+1];
		for(int i = 0;i < 2000000;i++) {
			cumwf[i+1] = cumwf[i] + F[i];
			cumwf[i+1] %= mod;
		}
		
		SegmentTreeOverwriteRAddRMQL sts = new SegmentTreeOverwriteRAddRMQL(n, cumw);
		SegmentTreeOverwriteRAddRMQL stsf = new SegmentTreeOverwriteRAddRMQL(n, cumwf);
		
		
		for(int z = 0;z < Q;z++) {
			int type = ni(), l = ni(), r = ni(), K = ni();
			if(type == 0) {
				long ans = (sts.sum(l, r+1) + stsf.sum(l, r+1)) * K % mod;
				if(ans < 0)ans += mod;
				out.println(ans);
			}else if(type == 1) {
				sts.mul(l, r+1, 0);
				sts.add(l, r+1, K);
				stsf.mul(l, r+1, 0);
			}else if(type == 2) {
				sts.add(l, r+1, K);
			}else if(type == 3) {
				sts.mul(l, r+1, K);
				stsf.mul(l, r+1, K);
			}else {
				stsf.add(l, r+1, K);
			}
		}
	}
	
	public static class SegmentTreeOverwriteRAddRMQL {
		public int M, H, N;
		public long[] st;
		public long[] plus;
		public long[] mul;
		
		public long[] cumw;
		
		public SegmentTreeOverwriteRAddRMQL(int n, long[] cw)
		{
			this.cumw = cw;
			N = n;
			M = Integer.highestOneBit(Math.max(N-1, 1))<<2;
			H = M>>>1;
			plus = new long[H];
			mul = new long[H];
			Arrays.fill(mul, 1L);
			st = new long[M];
			for(int i = 0;i < N;i++){
				st[H+i] = 0;
			}
			for(int i = H-1;i >= 1;i--){
				propagate(i);
			}
		}
		
		// 0
		// 00
		// 2100
		
		// 16
		// 16 0
		// 11 5 0 0
		// 3 4 3 0 0 0 0 0
		
		private void propagate(int i)
		{
			int pos = i-Integer.highestOneBit(i);
			int w = H/Integer.highestOneBit(i);
			st[i] = (st[2*i] + st[2*i+1]) * mul[i] + plus[i] * (cumw[(pos+1)*w] - cumw[pos*w]);
			st[i] %= mod;
		}
		
		private void fall(int cur)
		{
			if(2*cur >= H){
				st[2*cur] = (st[2*cur] * mul[cur] + plus[cur] * (cumw[2*cur-H+1] - cumw[2*cur-H])) % mod;
				st[2*cur+1] = (st[2*cur+1] * mul[cur] + plus[cur] * (cumw[2*cur+1-H+1] - cumw[2*cur+1-H])) % mod;
			}else{
				for(int i = 2*cur;i < 2*cur+2;i++){
					mul[i] = mul[i] * mul[cur] % mod;
					plus[i] = (plus[i] * mul[cur] + plus[cur]) % mod;
					propagate(i);
				}
			}
			mul[cur] = 1;
			plus[cur] = 0;
		}
		
		public void add(int l, int r, long v){ add(l, r, v, 0, H, 1); }
		
		private void add(int l, int r, long v, int cl, int cr, int cur)
		{
			if(l <= cl && cr <= r){
				if(cr == cl+1){
					st[cur] += v * (cumw[cur-H+1] - cumw[cur-H]);
					st[cur] %= mod;
				}else {
					plus[cur] += v;
					propagate(cur);
				}
			}else{
				int mid = cl+cr>>>1;
				fall(cur);
				if(cl < r && l < mid){
					add(l, r, v, cl, mid, 2*cur);
				}
				if(mid < r && l < cr){
					add(l, r, v, mid, cr, 2*cur+1);
				}
				propagate(cur);
			}
		}
		
		public void mul(int l, int r, long v){ mul(l, r, v, 0, H, 1); }
		
		private void mul(int l, int r, long v, int cl, int cr, int cur)
		{
			if(l <= cl && cr <= r){
				if(cr == cl+1){
					st[cur] = st[cur] * v % mod;
				}else {
					mul[cur] = mul[cur] * v % mod;
					plus[cur] = plus[cur] * v % mod;
					propagate(cur);
				}
			}else{
				int mid = cl+cr>>>1;
				fall(cur);
				if(cl < r && l < mid){
					mul(l, r, v, cl, mid, 2*cur);
				}
				if(mid < r && l < cr){
					mul(l, r, v, mid, cr, 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 st[cur];
			}else{
				int mid = cl+cr>>>1;
				long S = 0;
				if(cl < r && l < mid){
					S += sum(l, r, cl, mid, 2*cur);
				}
				if(mid < r && l < cr){
					S += sum(l, r, mid, cr, 2*cur+1);
				}
				S = S * mul[cur] + plus[cur] * (cumw[Math.min(cr, r)] - cumw[Math.max(cl, l)]);
				return S % mod;
			}
		}
//		
//		public long[] toArray() { return toArray(1, 0, H, new long[H]); }
//		
//		private long[] toArray(int cur, int l, int r, long[] ret)
//		{
//			if(r-l == 1){
//				ret[cur-H] = st[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);
//				for(int i = l;i < r;i++){
//					ret[i] += plus[cur];
//				}
//			}
//			return ret;
//		}
	}
	
	public static int mod = 1000000007;

	
	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");
//		Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){
//			@Override
//			public void run() {
//				long s = System.currentTimeMillis();
//				solve();
//				out.flush();
//				if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//			}
//		};
//		t.start();
//		t.join();
	}
	
	public static void main(String[] args) throws Exception { new F2().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 int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private long[] nal(int n)
	{
		long[] a = new long[n];
		for(int i = 0;i < n;i++)a[i] = nl();
		return a;
	}
	
	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[][] nmi(int n, int m) {
		int[][] map = new int[n][];
		for(int i = 0;i < n;i++)map[i] = na(m);
		return map;
	}
	
	private int ni() { return (int)nl(); }
	
	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