結果

問題 No.1347 HS Railway
ユーザー 57tggx
提出日時 2021-03-05 14:44:13
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 2,004 bytes
コンパイル時間 2,488 ms
コンパイル使用メモリ 256,828 KB
最終ジャッジ日時 2025-01-19 10:21:55
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/extc++.h>

using lint = long long;

int main(){
	lint n;
	std::cin >> n;


	std::vector<lint> l(5), r(5);
	std::vector<std::vector<lint>> t(5, std::vector<lint>(n));
	for(lint i = 0; i < 5; ++i){
		std::cin >> l[i] >> r[i];
		--l[i];
		--r[i];
		for(lint j = l[i]; j < n - 1; ++j){
			lint a = 0;
			if(j < r[i]){
				std::cin >> a;
			}
			t[i][j + 1] = t[i][j] + a;
		}
	}

	/*
	for(lint i = 0; i < n; ++i){
		std::cout << std::setw(2) << i << " ";
	}
	std::cout << std::endl;
	for(lint i = 0; i < 5; ++i){
		for(lint j = 0; j < n; ++j){
			std::cout << std::setw(2) << t[i][j] << " ";
		}
		std::cout << std::endl;
	}
	*/

	lint m;
	std::cin >> m;

	std::vector<std::vector<lint>> s(5);
	for(lint i = 0; i < m; ++i){
		lint b, st;
		std::cin >> b >> st;
		--b;
		s[b].push_back(st);
	}
	for(lint i = 0; i < 5; ++i){
		std::sort(s[i].begin(), s[i].end());
	}

	/*
	for(lint i = 0; i < 5; ++i){
		for(lint j : s[i]){
			std::cout << j << " ";
		}
		std::cout << std::endl;
	}
	*/

	lint ans = 0;
	for(lint i = 0; i < 5; ++i){
		for(lint j = i + 1; j < 5; ++j){
			if(i == j) continue;
			lint from = std::max(l[i], l[j]);
			lint to = std::min(r[i], r[j]);
			if(from > to) continue;
			/*
			printf("%d %d\n", i, j);
			printf("t[%2d][%2d] = %2d\n", i, from, t[i][from]);
			printf("t[%2d][%2d] = %2d\n", j, from, t[j][from]);
			printf("t[%2d][%2d] = %2d\n", i, to, t[i][to]);
			printf("t[%2d][%2d] = %2d\n", j, to, t[j][to]);
			*/
			lint index1 = 0, index2 = 0;
			for(lint k : s[i]){
				while(index1 < s[j].size() && k + t[i][from] >= s[j][index1] + t[j][from]){
					// printf("%d + %d >= %d + %d\n", k, t[i][from], s[j][index1], t[j][from]);
					++index1;
				}
				while(index2 < s[j].size() && k + t[i][to] > s[j][index2] + t[j][to]){
					// printf("%d + %d > %d + %d\n", k, t[i][to], s[j][index2], t[j][to]);
					++index2;
				}
				// std::cout << k << " " << index1 << " " << index2 << std::endl;
				ans += index1 - index2;
			}
		}
	}
	std::cout << ans << std::endl;
}
0