Submission #70454499


Source Code Expand

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 10, INF = 1e9;

#define int long long

int n;
bool s[N][3];

struct Vector {
	int a[3];
	
	Vector() { memset(a, 0x3f, sizeof a); }
	Vector(int x, int y, int z) { a[0] = x, a[1] = y, a[2] = z; }
};
struct Matrix {
	int a[3][3];
	
	Matrix() { memset(a, 0x3f, sizeof a); }
	Matrix(int i) {
		a[0][0] = s[i][0] ? 1 : INF;
		a[1][0] = s[i][0] && s[i][1] ? 2 : INF;
		a[2][0] = s[i][0] && s[i][1] && s[i][2] ? 3 : INF;
		
		a[0][1] = s[i][0] && s[i][1] ? 2 : INF;
		a[1][1] = s[i][1] ? 1 : INF;
		a[2][1] = s[i][1] && s[i][2] ? 2 : INF;
		
		a[0][2] = s[i][0] && s[i][1] && s[i][2] ? 3 : INF;
		a[1][2] = s[i][1] && s[i][2] ? 2 : INF;
		a[2][2] = s[i][2] ? 1 : INF;
	}
};

Matrix operator *(Matrix A, Matrix B) {
	Matrix res;
	for (int i = 0; i < 3; ++ i )
		for (int j = 0; j < 3; ++ j )
			for (int k = 0; k < 3; ++ k )
				res.a[i][j] = min(res.a[i][j], A.a[i][k] + B.a[k][j]);
	return res;
}

Vector operator *(Vector A, Matrix B) {
	Vector res;
	for (int i = 0; i < 3; ++ i )
		for (int j = 0; j < 3; ++ j )
			res.a[j] = min(res.a[j], A.a[i] + B.a[i][j]);
	return res;
}

struct Tree {
	struct Node {
		int l, r;
		Matrix v;
	}tr[N << 2];
	
	void pushup(int u) {
		tr[u].v = tr[u << 1].v * tr[u << 1 | 1].v;
	}
	
	void build(int u, int l, int r) {
		tr[u].l = l, tr[u].r = r;
		if (l == r) tr[u].v = Matrix(l);
		else {
			int mid = l + r >> 1;
			build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
			pushup(u);
		}
	}
	
	void modify(int u, int x) {
		if (tr[u].l == tr[u].r) tr[u].v = Matrix(x);
		else {
			int mid = tr[u].l + tr[u].r >> 1;
			if (x <= mid) modify(u << 1, x);
			else modify(u << 1 | 1, x);
			pushup(u);
		}
	}
}T;

signed main() {
	cin >> n;
	for (int i = 0; i < 3; ++ i )
		for (int j = 1; j <= n; ++ j ) {
			char c;
			cin >> c;
			s[j][i] = c == '.';
		}
	T.build(1, 1, n);
	int q;
	cin >> q;
	
	while (q -- ) {
		int x, y;
		cin >> x >> y;
		s[y][x - 1] ^= 1;
		T.modify(1, y);
		int res = (Vector(0, INF, INF) * T.tr[1].v).a[2] - 1;
		cout << (res < 1e9 ? res : -1) << '\n';
	}
	return 0;
}

Submission Info

Submission Time
Task F - Shortest Path Query
User huk2
Language C++ 20 (gcc 12.2)
Score 525
Code Size 2194 Byte
Status AC
Exec Time 565 ms
Memory 73020 KiB

Compile Error

Main.cpp: In member function ‘void Tree::build(long long int, long long int, long long int)’:
Main.cpp:68:37: warning: suggest parentheses around ‘+’ inside ‘>>’ [-Wparentheses]
   68 |                         int mid = l + r >> 1;
      |                                   ~~^~~
Main.cpp: In member function ‘void Tree::modify(long long int, long long int)’:
Main.cpp:77:43: warning: suggest parentheses around ‘+’ inside ‘>>’ [-Wparentheses]
   77 |                         int mid = tr[u].l + tr[u].r >> 1;
      |                                   ~~~~~~~~^~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 525 / 525
Status
AC × 2
AC × 30
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 24 ms 72284 KiB
00_sample_01.txt AC 24 ms 72348 KiB
01_random_00.txt AC 268 ms 72228 KiB
01_random_01.txt AC 272 ms 72280 KiB
01_random_02.txt AC 268 ms 72284 KiB
01_random_03.txt AC 269 ms 72240 KiB
01_random_04.txt AC 492 ms 72516 KiB
01_random_05.txt AC 460 ms 72448 KiB
01_random_06.txt AC 364 ms 72252 KiB
01_random_07.txt AC 518 ms 72676 KiB
01_random_08.txt AC 428 ms 72396 KiB
01_random_09.txt AC 539 ms 72824 KiB
01_random_10.txt AC 551 ms 73016 KiB
01_random_11.txt AC 545 ms 72872 KiB
01_random_12.txt AC 544 ms 72864 KiB
01_random_13.txt AC 547 ms 72880 KiB
01_random_14.txt AC 543 ms 72824 KiB
01_random_15.txt AC 539 ms 72824 KiB
01_random_16.txt AC 563 ms 72864 KiB
01_random_17.txt AC 565 ms 72800 KiB
01_random_18.txt AC 550 ms 72804 KiB
01_random_19.txt AC 539 ms 73020 KiB
01_random_20.txt AC 534 ms 72816 KiB
01_random_21.txt AC 546 ms 72704 KiB
01_random_22.txt AC 561 ms 72968 KiB
01_random_23.txt AC 548 ms 72864 KiB
01_random_24.txt AC 560 ms 72876 KiB
01_random_25.txt AC 538 ms 72868 KiB
01_random_26.txt AC 533 ms 72728 KiB
01_random_27.txt AC 533 ms 72968 KiB