CSES - Datatähti 2025 alku - Results
Submission details
Task:Niitty
Sender:DLPS
Submission time:2024-10-31 23:09:36 +0200
Language:C++ (C++20)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
#40
#50
#60
Test results
testverdicttimegroup
#10.00 s1, 2, 3, 4, 5, 6details
#2ACCEPTED0.00 s1, 2, 3, 4, 5, 6details
#30.00 s1, 2, 3, 4, 5, 6details
#4ACCEPTED0.00 s1, 2, 3, 4, 5, 6details
#5ACCEPTED0.00 s1, 2, 3, 4, 5, 6details
#60.00 s2, 3, 4, 5, 6details
#70.01 s2, 3, 4, 5, 6details
#80.00 s2, 3, 4, 5, 6details
#9ACCEPTED0.00 s2, 3, 4, 5, 6details
#100.01 s3, 4, 5, 6details
#110.01 s3, 4, 5, 6details
#120.01 s3, 4, 5, 6details
#13ACCEPTED0.01 s3, 4, 5, 6details
#140.01 s4, 5, 6details
#150.05 s4, 5, 6details
#160.01 s4, 5, 6details
#170.02 s4, 5, 6details
#180.06 s5, 6details
#190.20 s5, 6details
#200.04 s5, 6details
#21--5, 6details
#220.39 s6details
#23--6details
#240.31 s6details
#25--6details

Compiler report

input/code.cpp:1:22: warning: extra tokens at end of #include directive
    1 | #include "bits/stdc++.h";
      |                      ^
input/code.cpp:2:25: warning: extra tokens at end of #include directive
    2 | #include "unordered_map";
      |                         ^
input/code.cpp:8:776: warning: integer constant is so large that it is unsigned
    8 | const uint64_t bits[64] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648, 4294967296, 8589934592, 17179869184, 34359738368, 68719476736, 137438953472, 274877906944, 549755813888, 1099511627776, 2199023255552, 4398046511104, 8796093022208, 17592186044416, 35184372088832, 70368744177664, 140737488355328, 281474976710656, 562949953421312, 1125899906842624, 2251799813685248, 4503599627370496, 9007199254740992, 18014398509481984, 36028797018963968,...

Code

#include "bits/stdc++.h";
#include "unordered_map";

using namespace std;

#define byte unsigned char

const uint64_t bits[64] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648, 4294967296, 8589934592, 17179869184, 34359738368, 68719476736, 137438953472, 274877906944, 549755813888, 1099511627776, 2199023255552, 4398046511104, 8796093022208, 17592186044416, 35184372088832, 70368744177664, 140737488355328, 281474976710656, 562949953421312, 1125899906842624, 2251799813685248, 4503599627370496, 9007199254740992, 18014398509481984, 36028797018963968, 72057594037927936, 144115188075855872, 288230376151711744, 576460752303423488, 1152921504606846976, 2305843009213693952, 4611686018427387904, 9223372036854775808 };
const uint64_t rbits[64] = { 18446744073709551614, 18446744073709551613, 18446744073709551611, 18446744073709551607, 18446744073709551599, 18446744073709551583, 18446744073709551551, 18446744073709551487, 18446744073709551359, 18446744073709551103, 18446744073709550591, 18446744073709549567, 18446744073709547519, 18446744073709543423, 18446744073709535231, 18446744073709518847, 18446744073709486079, 18446744073709420543, 18446744073709289471, 18446744073709027327, 18446744073708503039, 18446744073707454463, 18446744073705357311, 18446744073701163007, 18446744073692774399, 18446744073675997183, 18446744073642442751, 18446744073575333887, 18446744073441116159, 18446744073172680703, 18446744072635809791, 18446744071562067967, 18446744069414584319, 18446744065119617023, 18446744056529682431, 18446744039349813247, 18446744004990074879, 18446743936270598143, 18446743798831644671, 18446743523953737727, 18446742974197923839, 18446741874686296063, 18446739675663040511, 18446735277616529407, 18446726481523507199, 18446708889337462783, 18446673704965373951, 18446603336221196287, 18446462598732840959, 18446181123756130303, 18445618173802708991, 18444492273895866367, 18442240474082181119, 18437736874454810623, 18428729675200069631, 18410715276690587647, 18374686479671623679, 18302628885633695743, 18158513697557839871, 17870283321406128127, 17293822569102704639, 16140901064495857663, 13835058055282163711, 9223372036854775807 };
const uint64_t smasks[64] = { 18446744073709551615, 18446744073709551614, 18446744073709551612, 18446744073709551608, 18446744073709551600, 18446744073709551584, 18446744073709551552, 18446744073709551488, 18446744073709551360, 18446744073709551104, 18446744073709550592, 18446744073709549568, 18446744073709547520, 18446744073709543424, 18446744073709535232, 18446744073709518848, 18446744073709486080, 18446744073709420544, 18446744073709289472, 18446744073709027328, 18446744073708503040, 18446744073707454464, 18446744073705357312, 18446744073701163008, 18446744073692774400, 18446744073675997184, 18446744073642442752, 18446744073575333888, 18446744073441116160, 18446744073172680704, 18446744072635809792, 18446744071562067968, 18446744069414584320, 18446744065119617024, 18446744056529682432, 18446744039349813248, 18446744004990074880, 18446743936270598144, 18446743798831644672, 18446743523953737728, 18446742974197923840, 18446741874686296064, 18446739675663040512, 18446735277616529408, 18446726481523507200, 18446708889337462784, 18446673704965373952, 18446603336221196288, 18446462598732840960, 18446181123756130304, 18445618173802708992, 18444492273895866368, 18442240474082181120, 18437736874454810624, 18428729675200069632, 18410715276690587648, 18374686479671623680, 18302628885633695744, 18158513697557839872, 17870283321406128128, 17293822569102704640, 16140901064495857664, 13835058055282163712, 9223372036854775808 };
const uint64_t emasks[64] = { 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287, 1048575, 2097151, 4194303, 8388607, 16777215, 33554431, 67108863, 134217727, 268435455, 536870911, 1073741823, 2147483647, 4294967295, 8589934591, 17179869183, 34359738367, 68719476735, 137438953471, 274877906943, 549755813887, 1099511627775, 2199023255551, 4398046511103, 8796093022207, 17592186044415, 35184372088831, 70368744177663, 140737488355327, 281474976710655, 562949953421311, 1125899906842623, 2251799813685247, 4503599627370495, 9007199254740991, 18014398509481983, 36028797018963967, 72057594037927935, 144115188075855871, 288230376151711743, 576460752303423487, 1152921504606846975, 2305843009213693951, 4611686018427387903, 9223372036854775807, 18446744073709551615 };

// Debug
int CALLS = 0;


int fieldSize = 0;

struct flowerData
{
	vector<vector<uint64_t>> rows = vector<vector<uint64_t>>();

	int flowerCount = 0;

	bool init = false;

	void Init()
	{
		if (init)
		{
			return;
		}
		init = true;

		vector<uint64_t> filler(fieldSize / 64 + 1, 0);
		rows.reserve(fieldSize);

		for (int i = 0; i < fieldSize; i++)
		{
			rows.push_back(filler);
		}
	}

	void Insert(int x, int y)
	{
		flowerCount++;

		vector<uint64_t>& row = rows[y];

		// Maybe x - 1
		//uint64_t& workingByte = row[ceil(x / 64)];
		// Maybe x - 1
		row[ceil(x / 64)] |= bits[x % 64];
	}

	bool InBox(int left, int right, int top, int bottom)
	{
		for (int y = top; y <= bottom; y++)
		{
			vector<uint64_t>& row = rows[y];

			byte minByte = left / 64;
			byte minByteStart = left % 64;
			byte maxByte = right / 64;
			byte maxByteEnd = right % 64;

			if (minByte == maxByte)
			{
				// Masked from left and right
				if ((row[minByte] & smasks[minByteStart] & emasks[maxByteEnd]) > 0)
				{
					return true;
				}
			}
			else
			{
				// Left
				if ((row[minByte] & smasks[minByteStart]) > 0)
				{
					return true;
				}
				// Center
				for (int i = minByte + 1; i < maxByte; i++)
				{
					if (row[i] > 0)
					{
						return true;
					}
				}
				// Right
				if ((row[maxByte] & emasks[maxByteEnd]) > 0)
				{
					return true;
				}
			}
		}

		return false;
	}
};

const unordered_map<char, int> flowerIndecies = { {'A',0},{'B',1},{'C',2},{'D',3},{'E',4},{'F',5},{'G',6},{'H',7},{'I',8},{'J',9},{'K',10},{'L',11},{'M',12},{'N',13},{'O',14},{'P',15},{'Q',16},{'R',17},{'S',18},{'T',19},{'U',20},{'V',21},{'W',22},{'X',23},{'Y',24},{'Z',25} };
vector<flowerData> differentFlowers(26);

static bool ContainsAllFlowers(int left, int right, int top, int bottom)
{
	CALLS++;
	for (flowerData& flowerType : differentFlowers)
	{

		if (flowerType.flowerCount <= 0)
		{
			continue;
		}

		if (!flowerType.InBox(left, right, top, bottom))
		{
			return false;
		}
	}

	return true;
}

static int Y2Solver(int min, int max, int x1, int x2, int y1, int& outY2Min)
{
	// Esimerkiksi: min: 12 ja max: 25
	// Tällöin current = 12 + (13) / 2 = 18

	int localMin = min;
	int localMax = max;

	int current = (min + max) / 2;

	bool first = true;
	int change = 0;
	bool wasSuccess = false;

	while (true)
	{
		if (ContainsAllFlowers(x1, x2, y1, current))
		{
			// Nousi yhdellä ja ei sisältänyt ennen.
			if (change == 1 && !wasSuccess)
			{
				break;
			}
			if (change == 0 && wasSuccess)
			{
				break;
			}

			localMax = current;

			change = (localMin + current) / 2 - current;
			current += change;
			wasSuccess = true;
		}
		else
		{
			// laski yhdellä ja nyt ei sisällä.
			if (change == -1 && wasSuccess)
			{
				// Siirretään takaisin oikeaan.
				current++;
				break;
			}
			if (change == 0 && !first)
			{
				return 0;
			}


			localMin = current;
			change = ceil((current + localMax) / 2.0f) - current;
			current += change;
			wasSuccess = false;
		}
		first = false;
	}

	outY2Min = current;
	return (max - current + 1);
}

static int Y1Solver(int max, int x1, int x2) 
{
	int localMin = 0;
	int localMax = fieldSize - 1;

	int current = (localMin + localMax) / 2;

	bool first = true;
	int change = 0;
	bool wasSuccess = false;

	// Pienin mahdollinen Y2
	int Y2min = 0;
	if (Y2Solver(0, max, x1, x2, 0, Y2min) > 0)
	{
		while (true)
		{
			Y2Solver(current, max, x1, x2, current, Y2min);
			if (ContainsAllFlowers(x1, x2, current, max))
			{
				// laski yhdellä ja nyt ei sisällä.
				if (change == -1 && !wasSuccess)
				{
					// Siirretään takaisin oikeaan.
					break;
				}
				if (change == 0 && wasSuccess)
				{
					break;
				}
				


				localMin = current;
				change = ceil((current + localMax) / 2.0f) - current;
				current += change;
				wasSuccess = true;
			}
			else
			{
				
				// Nousi yhdellä ja ei sisältänyt ennen.
				if (change == 1 && wasSuccess)
				{
					current--;
					break;
				}
				if (change == 0 && !first)
				{
					return 0;
				}

				localMax = current;

				change = (localMin + current) / 2 - current;
				current += change;
				wasSuccess = false;
			}
			first = false;
		}

		return (current + 1) * (fieldSize - Y2min);
	}
	return 0;
}

int main()
{
	cin >> fieldSize;

	cout << "";

	for (int y = 0; y < fieldSize; y++)
	{
		string row;
		cin >> row;

		for (int x = 0; x < row.size(); x++)
		{
			flowerData& currentFlower = differentFlowers[flowerIndecies.at(row[x])];

			currentFlower.Init();

			currentFlower.Insert(x, y);
		}
	}

	int count = 0;


	for (int x1 = 0; x1 < fieldSize; x1++)
	{
		bool hadSuccessX2 = false;
		for (int x2 = fieldSize - 1; x2 >= x1; x2--)
		{
			int ans = Y1Solver(fieldSize - 1, x1, x2);

			if (ans > 0)
			{
				count += ans;
				hadSuccessX2 = true;
			}
			else
			{
				break;
			}
		}
		if (!hadSuccessX2)
		{
			break;
		}
	}

	cout << count;
}

Test details

Test 1

Group: 1, 2, 3, 4, 5, 6

Verdict:

input
10
TNCTNPNTPC
NPPNTNTPTP
NTNTTCNTCT
NPCPNPPNTT
...

correct output
2035

user output
603

Test 2

Group: 1, 2, 3, 4, 5, 6

Verdict: ACCEPTED

input
10
NFWQLWNWYS
DZOQJVXFPJ
CNHXPXMCQD
QRTBVNLTQC
...

correct output
9

user output
9

Test 3

Group: 1, 2, 3, 4, 5, 6

Verdict:

input
10
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
...

correct output
3025

user output
550

Test 4

Group: 1, 2, 3, 4, 5, 6

Verdict: ACCEPTED

input
10
FFFFFFFFFF
FFFFFCFFFF
FFFFFFJFFF
FFFFFFFFFF
...

correct output
12

user output
12

Test 5

Group: 1, 2, 3, 4, 5, 6

Verdict: ACCEPTED

input
1
X

correct output
1

user output
1

Test 6

Group: 2, 3, 4, 5, 6

Verdict:

input
20
BBCBUBOUOBBCUUBBCOUO
BOUCOOCUBCOOOCOBOCUO
UCCUUUOBCOCBCBUBUCOO
BUOBUCUCUOOBCOOUBUOO
...

correct output
38724

user output
5208

Test 7

Group: 2, 3, 4, 5, 6

Verdict:

input
20
CBGLSHGZHYZDWBNDBJUG
SMUXOJQYPXZDTMJUIWOJ
XIDSTNBGHKRKOVUVMINB
MTQGCFRUHQKALXRNCQGS
...

correct output
8334

user output
2929

Test 8

Group: 2, 3, 4, 5, 6

Verdict:

input
20
KKKKKKKKKKKKKKKKKKKK
KKKKKKKKKKKKKKKKKKKK
KKKKKKKKKKKKKKKKKKKK
KKKKKKKKKKKKKKKKKKKK
...

correct output
44100

user output
4200

Test 9

Group: 2, 3, 4, 5, 6

Verdict: ACCEPTED

input
20
AAAAAAAAXAAAAAAAAAAA
AAAWAAAAAAAAAAAAAOAA
AAAAAAAAAAAAAAAAAPAA
AAAAAAAAKAAAAAAAAAAZ
...

correct output
18

user output
18

Test 10

Group: 3, 4, 5, 6

Verdict:

input
50
GRGREEEGREGXRXXEGXXREXGRRRGRRR...

correct output
1584665

user output
83309

Test 11

Group: 3, 4, 5, 6

Verdict:

input
50
AITIISJUHCCRZNKSDCNQKYSQRINFWJ...

correct output
1077746

user output
113672

Test 12

Group: 3, 4, 5, 6

Verdict:

input
50
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOO...

correct output
1625625

user output
63750

Test 13

Group: 3, 4, 5, 6

Verdict: ACCEPTED

input
50
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF...

correct output
1680

user output
1680

Test 14

Group: 4, 5, 6

Verdict:

input
100
NNCMDCDDCCNNNDNCMMNCDCDCCDCDNM...

correct output
25325366

user output
573112

Test 15

Group: 4, 5, 6

Verdict:

input
100
LIMQQIHASECROEVILNVULGWZJPPKOG...

correct output
22342463

user output
927661

Test 16

Group: 4, 5, 6

Verdict:

input
100
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTT...

correct output
25502500

user output
505000

Test 17

Group: 4, 5, 6

Verdict:

input
100
QXQQQQQQQQQQQQQQQQQQQQQQQQQQQQ...

correct output
25650

user output
13650

Test 18

Group: 5, 6

Verdict:

input
200
NAANANMMKNKKAKMKMAKNKMNKMMNNAA...

correct output
403292767

user output
4301353

Test 19

Group: 5, 6

Verdict:

input
200
OMYWATTLURKQPTKEFMGGYAOONXWVSC...

correct output
388111321

user output
7582971

Test 20

Group: 5, 6

Verdict:

input
200
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC...

correct output
404010000

user output
4020000

Test 21

Group: 5, 6

Verdict:

input
200
LLLLLLLLLLLLLLLLLHLLLLLLLLLLLL...

correct output
14159445

user output
(empty)

Test 22

Group: 6

Verdict:

input
500
VVHWVUHVHUWWWVUUUWVUUHUUWHWUVW...

correct output
15683003812

user output
64598199

Test 23

Group: 6

Verdict:

input
500
OIMZGEQSBMBDSDXSWRFNKSGFEBBTJE...

correct output
15575906951

user output
(empty)

Test 24

Group: 6

Verdict:

input
500
IIIIIIIIIIIIIIIIIIIIIIIIIIIIII...

correct output
15687562500

user output
62625000

Test 25

Group: 6

Verdict:

input
500
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW...

correct output
3058970930

user output
(empty)