Submission #933601

# Submission time Handle Problem Language Result Execution time Memory
933601 2024-02-26T01:12:38 Z Joshua_Andersson Joker (BOI20_joker) C++14
100 / 100
347 ms 28348 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define int ll
const int inf = int(1e18);

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> p2;

#define rep(i, high) for (int i = 0; i < high; i++)
#define repp(i, low, high) for (int i = low; i < high; i++)
#define repe(i, container) for (auto& i : container)
#define sz(container) ((int)container.size())
#define all(x) begin(x),end(x)
#define ceildiv(x,y) ((x + y - 1) / (y))

inline void fast() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); }

auto Start = chrono::high_resolution_clock::now();
void resettimer() { Start = chrono::high_resolution_clock::now(); }
int elapsedmillis() { return chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - Start).count(); }

#define _LOCAL _DEBUG
#if _LOCAL
#define assert(x) if (!(x)) __debugbreak()
#endif

struct change
{
	int a, b;
	int oldpar, oldxor, oldsize;
};

struct UF
{
	vector<change> updates;
	vi size;
	vi par;
	vi parity;

	UF(int n) : size(n, 1), parity(n), par(n)
	{
		rep(i, n)par[i] = i;
	}

	int find(int x) { return x == par[x] ? x : find(par[x]); }
	int getparity(int x) { return x == par[x] ? parity[x] : parity[x] ^ getparity(par[x]); }
	bool oddcycle(int a, int b)
	{
		return getparity(a) == getparity(b);
	}

	void merge(int a, int b)
	{
		if (size[find(a)] < size[find(b)]) swap(a, b);
		int childa = a;
		int childb = b;
		a = find(a); b = find(b);
		change c({ a,b,par[b],parity[b],size[a] });
		updates.push_back(c);

		if (a != b)
		{
			parity[b] = getparity(childb) ^ getparity(childa) ^ 1;
			size[a] += size[b];
			par[b] = a;
		}
	}

	void rollback()
	{
		change c = updates.back();
		updates.pop_back();
		par[c.b] = c.oldpar;
		parity[c.b] = c.oldxor;
		size[c.a] = c.oldsize;
	}
};

UF uf(0);
vector<p2> edgelist;
bool add(int i)
{
	int a, b;
	tie(a, b) = edgelist[i];
	int ret = 0;
	if (uf.find(a) == uf.find(b)) ret = uf.oddcycle(a, b);
	uf.merge(a, b);
	return ret;
};

vi rightp;
void dc(int l, int r, int hr, bool yes) // we have added edges [0,i) and (r, M)
{
	if (l==r)
	{
		//cout << l << ": " << hr << "\n";
		rightp[l] = (yes ? inf : hr);
		return;
	}
	int mid = (l + r) / 2;


	// left side
	// update hr
	repp(i, l, mid) add(i);

	int cnt = 0;
	int newhr = hr;
	for (int i = hr; i >= mid; i--)
	{
		cnt++;
		newhr = i;
		if (add(i))
		{
			break;
		}
	}
	rep(i, cnt) uf.rollback();
	repp(i, l, mid) uf.rollback();

	for (int i = hr, j = cnt-1; j > 0; i--, j--) add(i);
	dc(l, mid, newhr, yes);
	rep(i, cnt-1) uf.rollback();
	
	// right side
	bool newyes = yes;
	int newhl = newhr;
	repp(i, l, mid+1) newyes |= add(i);
	dc(mid + 1, r, hr, newyes);

	repp(i, l, mid+1) uf.rollback();

}

signed main()
{
	fast();

	//ifstream in("C:\\Users\\Matis\\Desktop\\comp_prog\\x64\\debug\\in.txt");
	//cin.rdbuf(in.rdbuf());

	int n, m, q;
	cin >> n >> m >> q;

	rep(i, m)
	{
		int a, b;
		cin >> a >> b;
		a--; b--;
		edgelist.emplace_back(a, b);
	}
	//shuffle(edgelist.begin(), prev(edgelist.end()), mt19937(3436876));

	/*uf = UF(n);

	vi right(m, inf);
	bool anyodd = 0;

	rep(i, m)
	{
		int lo = i;
		int hi = m;
		while (lo + 1 < hi)
		{
			int mid = (lo + hi) / 2;
			bool odd = 0;
			for (int j = m - 1; j >= mid; j--) odd |= add(j);
			for (int j = m - 1; j >= mid; j--) uf.rollback();
			if (odd) lo = mid;
			else hi = mid;
		}
		right[i] = lo;

		if (anyodd) right[i] = inf;
		anyodd |= add(i);
	}*/
	uf = UF(n);
	rightp.resize(m);
	dc(0, m-1, m-1, 0);
	//assert(right == rightp);
	//vvi queries(m);
	while (q--)
	{
		int l, r;
		cin >> l >> r;
		l--; r--;

		cout << (r + 1 <= rightp[l] ? "YES" : "NO") << "\n";
	}

	return 0;
}

Compilation message

Joker.cpp: In constructor 'UF::UF(ll)':
Joker.cpp:41:5: warning: 'UF::parity' will be initialized after [-Wreorder]
   41 |  vi parity;
      |     ^~~~~~
Joker.cpp:40:5: warning:   'vi UF::par' [-Wreorder]
   40 |  vi par;
      |     ^~~
Joker.cpp:43:2: warning:   when initialized here [-Wreorder]
   43 |  UF(int n) : size(n, 1), parity(n), par(n)
      |  ^~
Joker.cpp: In function 'void dc(ll, ll, ll, bool)':
Joker.cpp:130:6: warning: unused variable 'newhl' [-Wunused-variable]
  130 |  int newhl = newhr;
      |      ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 452 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 456 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 452 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 456 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 2 ms 600 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 2 ms 604 KB Output is correct
34 Correct 2 ms 748 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 2 ms 604 KB Output is correct
38 Correct 2 ms 604 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 604 KB Output is correct
42 Correct 2 ms 604 KB Output is correct
43 Correct 2 ms 604 KB Output is correct
44 Correct 2 ms 604 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 2 ms 720 KB Output is correct
48 Correct 2 ms 600 KB Output is correct
49 Correct 2 ms 604 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 2 ms 724 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 3 ms 732 KB Output is correct
54 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 182 ms 22816 KB Output is correct
4 Correct 269 ms 23120 KB Output is correct
5 Correct 162 ms 22140 KB Output is correct
6 Correct 223 ms 22756 KB Output is correct
7 Correct 222 ms 20628 KB Output is correct
8 Correct 266 ms 19948 KB Output is correct
9 Correct 278 ms 23008 KB Output is correct
10 Correct 297 ms 23112 KB Output is correct
11 Correct 254 ms 20656 KB Output is correct
12 Correct 249 ms 22960 KB Output is correct
13 Correct 258 ms 20176 KB Output is correct
14 Correct 269 ms 21292 KB Output is correct
15 Correct 297 ms 23240 KB Output is correct
16 Correct 298 ms 25012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 452 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 456 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 182 ms 22816 KB Output is correct
30 Correct 269 ms 23120 KB Output is correct
31 Correct 162 ms 22140 KB Output is correct
32 Correct 223 ms 22756 KB Output is correct
33 Correct 222 ms 20628 KB Output is correct
34 Correct 266 ms 19948 KB Output is correct
35 Correct 278 ms 23008 KB Output is correct
36 Correct 297 ms 23112 KB Output is correct
37 Correct 254 ms 20656 KB Output is correct
38 Correct 249 ms 22960 KB Output is correct
39 Correct 258 ms 20176 KB Output is correct
40 Correct 269 ms 21292 KB Output is correct
41 Correct 297 ms 23240 KB Output is correct
42 Correct 298 ms 25012 KB Output is correct
43 Correct 218 ms 25532 KB Output is correct
44 Correct 280 ms 26232 KB Output is correct
45 Correct 237 ms 27624 KB Output is correct
46 Correct 215 ms 24500 KB Output is correct
47 Correct 223 ms 25012 KB Output is correct
48 Correct 280 ms 23476 KB Output is correct
49 Correct 343 ms 26120 KB Output is correct
50 Correct 235 ms 23220 KB Output is correct
51 Correct 259 ms 25772 KB Output is correct
52 Correct 264 ms 26604 KB Output is correct
53 Correct 263 ms 22812 KB Output is correct
54 Correct 280 ms 24648 KB Output is correct
55 Correct 297 ms 25008 KB Output is correct
56 Correct 347 ms 26804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 452 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 456 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 2 ms 600 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 2 ms 604 KB Output is correct
34 Correct 2 ms 748 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 2 ms 604 KB Output is correct
38 Correct 2 ms 604 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 604 KB Output is correct
42 Correct 2 ms 604 KB Output is correct
43 Correct 2 ms 604 KB Output is correct
44 Correct 2 ms 604 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 2 ms 720 KB Output is correct
48 Correct 2 ms 600 KB Output is correct
49 Correct 2 ms 604 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 2 ms 724 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 3 ms 732 KB Output is correct
54 Correct 2 ms 604 KB Output is correct
55 Correct 151 ms 24088 KB Output is correct
56 Correct 264 ms 26044 KB Output is correct
57 Correct 144 ms 26808 KB Output is correct
58 Correct 207 ms 24576 KB Output is correct
59 Correct 259 ms 23220 KB Output is correct
60 Correct 278 ms 24796 KB Output is correct
61 Correct 214 ms 23496 KB Output is correct
62 Correct 242 ms 24808 KB Output is correct
63 Correct 229 ms 21368 KB Output is correct
64 Correct 272 ms 25524 KB Output is correct
65 Correct 284 ms 26032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 452 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 456 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 2 ms 600 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 2 ms 604 KB Output is correct
34 Correct 2 ms 748 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 2 ms 604 KB Output is correct
38 Correct 2 ms 604 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 604 KB Output is correct
42 Correct 2 ms 604 KB Output is correct
43 Correct 2 ms 604 KB Output is correct
44 Correct 2 ms 604 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 2 ms 720 KB Output is correct
48 Correct 2 ms 600 KB Output is correct
49 Correct 2 ms 604 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 2 ms 724 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 3 ms 732 KB Output is correct
54 Correct 2 ms 604 KB Output is correct
55 Correct 182 ms 22816 KB Output is correct
56 Correct 269 ms 23120 KB Output is correct
57 Correct 162 ms 22140 KB Output is correct
58 Correct 223 ms 22756 KB Output is correct
59 Correct 222 ms 20628 KB Output is correct
60 Correct 266 ms 19948 KB Output is correct
61 Correct 278 ms 23008 KB Output is correct
62 Correct 297 ms 23112 KB Output is correct
63 Correct 254 ms 20656 KB Output is correct
64 Correct 249 ms 22960 KB Output is correct
65 Correct 258 ms 20176 KB Output is correct
66 Correct 269 ms 21292 KB Output is correct
67 Correct 297 ms 23240 KB Output is correct
68 Correct 298 ms 25012 KB Output is correct
69 Correct 218 ms 25532 KB Output is correct
70 Correct 280 ms 26232 KB Output is correct
71 Correct 237 ms 27624 KB Output is correct
72 Correct 215 ms 24500 KB Output is correct
73 Correct 223 ms 25012 KB Output is correct
74 Correct 280 ms 23476 KB Output is correct
75 Correct 343 ms 26120 KB Output is correct
76 Correct 235 ms 23220 KB Output is correct
77 Correct 259 ms 25772 KB Output is correct
78 Correct 264 ms 26604 KB Output is correct
79 Correct 263 ms 22812 KB Output is correct
80 Correct 280 ms 24648 KB Output is correct
81 Correct 297 ms 25008 KB Output is correct
82 Correct 347 ms 26804 KB Output is correct
83 Correct 151 ms 24088 KB Output is correct
84 Correct 264 ms 26044 KB Output is correct
85 Correct 144 ms 26808 KB Output is correct
86 Correct 207 ms 24576 KB Output is correct
87 Correct 259 ms 23220 KB Output is correct
88 Correct 278 ms 24796 KB Output is correct
89 Correct 214 ms 23496 KB Output is correct
90 Correct 242 ms 24808 KB Output is correct
91 Correct 229 ms 21368 KB Output is correct
92 Correct 272 ms 25524 KB Output is correct
93 Correct 284 ms 26032 KB Output is correct
94 Correct 184 ms 24968 KB Output is correct
95 Correct 225 ms 27776 KB Output is correct
96 Correct 168 ms 25724 KB Output is correct
97 Correct 229 ms 23980 KB Output is correct
98 Correct 236 ms 23684 KB Output is correct
99 Correct 274 ms 23732 KB Output is correct
100 Correct 288 ms 28348 KB Output is correct
101 Correct 235 ms 23484 KB Output is correct
102 Correct 290 ms 25524 KB Output is correct
103 Correct 279 ms 27568 KB Output is correct
104 Correct 274 ms 22408 KB Output is correct
105 Correct 291 ms 25772 KB Output is correct
106 Correct 326 ms 27168 KB Output is correct
107 Correct 132 ms 28344 KB Output is correct
108 Correct 185 ms 23472 KB Output is correct
109 Correct 190 ms 23864 KB Output is correct
110 Correct 185 ms 25272 KB Output is correct
111 Correct 221 ms 23472 KB Output is correct
112 Correct 182 ms 23472 KB Output is correct
113 Correct 185 ms 23476 KB Output is correct
114 Correct 179 ms 23984 KB Output is correct
115 Correct 186 ms 24240 KB Output is correct
116 Correct 181 ms 24952 KB Output is correct