結果

問題 No.122 傾向と対策:門松列(その3)
ユーザー 👑 hos.lyrichos.lyric
提出日時 2015-01-09 13:07:50
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 3,241 ms / 5,000 ms
コード長 3,483 bytes
コンパイル時間 2,904 ms
コンパイル使用メモリ 109,320 KB
実行使用メモリ 4,492 KB
最終ジャッジ日時 2023-09-02 19:38:35
合計ジャッジ時間 24,183 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,281 ms
4,368 KB
testcase_01 AC 2,078 ms
4,368 KB
testcase_02 AC 3,241 ms
4,372 KB
testcase_03 AC 2,610 ms
4,368 KB
testcase_04 AC 2,209 ms
4,368 KB
testcase_05 AC 3,181 ms
4,368 KB
testcase_06 AC 2,072 ms
4,368 KB
testcase_07 AC 2,030 ms
4,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;

//	Input
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }
int readInt() { return to!int(readToken); }
long readLong() { return to!long(readToken); }
real readReal() { return to!real(readToken); }

//	chmin/chmax
void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(    const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }


int root(int[] uf, int u) {
	return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool conn(int[] uf, int u, int v) {
	u = uf.root(u);
	v = uf.root(v);
	if (u == v) return false;
	if (uf[u] > uf[v]) swap(u, v);
	uf[u] += uf[v];
	uf[v] = u;
	return true;
}

immutable long MO = 10^^9 + 7;
immutable long LIM = 20005;

long calc(long[] a, long[] b) {
	int n = cast(int)(a.length);
	foreach (u; 0 .. n) {
		if (a[u] > b[u]) {
			return 0;
		}
	}
debug{
// writeln("calc ",a," ",b);
}
	long ret;
	foreach (h; 0 .. 1 << (n * (n - 1) / 2)) {
		auto uf = new int[n];
		uf[] = -1;
		int pos;
		long s = +1;
		foreach (u; 0 .. n) foreach (v; u + 1 .. n) {
			if (h & 1 << pos++) {
				uf.conn(u, v);
				s *= -1;
			}
		}
		auto lbs = new long[n];
		auto ubs = new long[n];
		lbs[] = long.min;
		ubs[] = long.max;
		foreach (u; 0 .. n) {
			const r = uf.root(u);
			chmax(lbs[r], a[u]);
			chmin(ubs[r], b[u]);
		}
		long tmp = 1;
		foreach (r; 0 .. n) if (uf[r] < 0) {
			(tmp *= max(ubs[r] - lbs[r] + 1, 0)) %= MO;
		}
		(ret += s * tmp) %= MO;
	}
	return ret;
}

long[7] A, B;

void main(string[] args) {
	try {
	for (; ; ) {
		foreach (i; 0 .. 7) {
			A[i] = readLong;
			B[i] = readLong;
		}
		
		long ans;
		foreach (phase; 0 .. 2) {
			foreach (x; -LIM .. +LIM) {
				//	max(0,2,4,6) = x
				//	1,3,5 >= x + 1
				const res0 = calc(
					[ A[0], A[2], A[4], A[6] ], 
					[ min(B[0], x), min(B[2], x), min(B[4], x), min(B[6], x) ]
				);
				const res1 = calc(
					[ A[0], A[2], A[4], A[6] ], 
					[ min(B[0], x - 1), min(B[2], x - 1), min(B[4], x - 1), min(B[6], x - 1) ]
				);
				const res2 = calc(
					[ max(A[1], x + 1), max(A[3], x + 1), max(A[5], x + 1) ], 
					[ B[1], B[3], B[5] ]
				);
				(ans += (res0 - res1) * res2) %= MO;
			}
			A[] *= -1;
			B[] *= -1;
			swap(A, B);
		}
		ans = (ans % MO + MO) % MO;
		writeln(ans);
// break;
	}
	} catch (EOFException) {}
}
0