답안 #603411

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
603411 2022-07-24T06:28:59 Z 장태환(#8428) Collapse (JOI18_collapse) C++17
35 / 100
15000 ms 152776 KB
#include "collapse.h"
#include <bits/stdc++.h>
#include <assert.h>
using namespace std;
int uf[100100];
int siz[100100];
int rb[10000100][2];
int c;
vector<pair<int, int>>stree[1 << 20];
int q1[100100];
int q2[100100];
int ty[100100];
vector<pair<int, int>>cq;
vector<pair<int,int>>q[100100];
int treeN;
int sc, ec;
int f(int n)
{
	while (n != uf[n])
	{
		n = uf[n];
	}
	return n;
}
int u(int n, int m)
{
	n = f(n);
	m = f(m);
	if (n == m)
		return 0;
	if (siz[n] > siz[m])
		swap(n, m);
	siz[m] += siz[n];
	rb[c][0] = n;
	rb[c][1] = n;
	c++;
	uf[n] = uf[m];
	return 1;
}
int ans[100100];
int tgt[100100];
int ert[100100];
void odc(int p,int s=0, int e=treeN-1, int i=1)
{
	int rbc = 0;
	int j;
	for (j = 0; j < stree[i].size(); j++)
	{
		int v= u(stree[i][j].first, stree[i][j].second);
		p -= v;
		rbc += v;
	}
	if (s == e)
	{
		if (cq.size() <= s)
			goto T;
		for (j = sc; j <= ec;j++)
		{
			if (ty[j]!=(j<=cq[s].second)&&((q1[j] <= cq[s].first) ^ (q2[j] > cq[s].first))&&tgt[j]<=cq[s].second&&ert[j]>cq[s].second)
			{
				int v= u(q1[j], q2[j]);
				rbc += v;
				p -= v;
			}
		}
		ans[s] = p;
		T:
		p += rbc;
		while (rbc--)
		{
			c--;
			uf[rb[c][0]] = rb[c][1];
		}
		return;
	}

	odc(p,s, (s + e) / 2, i * 2);
	odc(p,(s + e) / 2 + 1, e, i * 2 + 1);
	p += rbc;
	while (rbc--)
	{
		c--;
		uf[rb[c][0]] = rb[c][1];
	}
}
void pu(int qs, int qe, pair<int,int>va, int s = 0, int e = treeN - 1, int i = 1)
{
	if (qs > qe || s > qe || qs > e)
		return;
	if (qs <= s && e <= qe)
	{
		stree[i].push_back(va);
		return;
	}
	pu(qs, qe, va, s, (s + e) / 2, i * 2);
	pu(qs, qe, va, (s + e) / 2 + 1, e, i * 2 + 1);
}
int ct[100100];
int rct[100100];
int ai[100100];
map<pair<int, int>, int>r;
std::vector<int> simulateCollapse(
	int N,
	std::vector<int> T,
	std::vector<int> X,
	std::vector<int> Y,
	std::vector<int> W,
	std::vector<int> P
) {
	memset(ert, 10, sizeof(ert));
	vector<int>rans(W.size());
	int i;
	for (i = 0; i < T.size(); i++)
	{
		ty[i] = T[i];
		q1[i] = min(X[i], Y[i]);
		q2[i] = max(X[i], Y[i]);
		if (ty[i] == 1)
		{
			ert[r[make_pair(q1[i], q2[i])]] = i;
			tgt[i] = r[make_pair(q1[i], q2[i])];
		}
		r[make_pair(q1[i], q2[i])] = i;
	}
	for (i = 0; i < P.size(); i++)
	{
		q[W[i]].push_back({ P[i],i });
	}
	int bc = 1000;
	for (i = 0; i < T.size(); i += bc)
	{
		sc = i;
		ec = min(i + bc - 1, (int)T.size() - 1);
		
		int j;
		
		for (j = 0; j < N; j++)
		{
			uf[j] = j;
			siz[j] = 1;
		}
		cq.clear();
		vector<pair<pair<int, int>,int>>ccq;
		for (j = sc; j <= ec; j++)
		{
			if (ty[j] == 1)
			{
				ct[r[make_pair(q1[j], q2[j])]] = 0;
			}
			int k;
			for (k = 0; k < q[j].size(); k++)
			{
				ccq.push_back({ {q[j][k].first,j},q[j][k].second });
			}
		}
		sort(ccq.begin(), ccq.end());
		for (j = 0; j < ccq.size(); j++)
		{
			cq.push_back(ccq[j].first);
			ai[j] = ccq[j].second;
		}
		for (treeN = 1; treeN < cq.size(); treeN *= 2);
		for (j = 1; j < 2 * treeN; j++)
		{
			stree[j].clear();
		}
		for (j = 0; j < T.size(); j++)
		{
			if (ct[r[make_pair(q1[j], q2[j])]])
			{
				int sp = lower_bound(cq.begin(), cq.end(), make_pair(q1[j],0)) - cq.begin();
				int ep = lower_bound(cq.begin(), cq.end(), make_pair(q2[j], 0)) - cq.begin();
				pu(0, sp - 1, make_pair(q1[j], q2[j]));
				pu(ep, (int)cq.size() - 1, make_pair(q1[j], q2[j]));
			}
		}
		if(cq.size())
			odc(N);
		for (j = 0; j < cq.size(); j++)
		{
			rans[ai[j]] = ans[j];
		}
		for (j = sc; j <= ec; j++)
		{
			rct[r[make_pair(q1[j], q2[j])]] = !ty[j];
			ct[r[make_pair(q1[j], q2[j])]] = rct[r[make_pair(q1[j], q2[j])]];
		}
		assert(c == 0);

	}
	return rans;
}

Compilation message

collapse.cpp: In function 'void odc(int, int, int, int)':
collapse.cpp:47:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |  for (j = 0; j < stree[i].size(); j++)
      |              ~~^~~~~~~~~~~~~~~~~
collapse.cpp:55:17: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   55 |   if (cq.size() <= s)
      |       ~~~~~~~~~~^~~~
collapse.cpp: In function 'std::vector<int> simulateCollapse(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
collapse.cpp:113:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |  for (i = 0; i < T.size(); i++)
      |              ~~^~~~~~~~~~
collapse.cpp:125:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  125 |  for (i = 0; i < P.size(); i++)
      |              ~~^~~~~~~~~~
collapse.cpp:130:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  130 |  for (i = 0; i < T.size(); i += bc)
      |              ~~^~~~~~~~~~
collapse.cpp:151:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  151 |    for (k = 0; k < q[j].size(); k++)
      |                ~~^~~~~~~~~~~~~
collapse.cpp:157:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  157 |   for (j = 0; j < ccq.size(); j++)
      |               ~~^~~~~~~~~~~~
collapse.cpp:162:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |   for (treeN = 1; treeN < cq.size(); treeN *= 2);
      |                   ~~~~~~^~~~~~~~~~~
collapse.cpp:167:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  167 |   for (j = 0; j < T.size(); j++)
      |               ~~^~~~~~~~~~
collapse.cpp:179:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  179 |   for (j = 0; j < cq.size(); j++)
      |               ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 28236 KB Output is correct
2 Correct 22 ms 28116 KB Output is correct
3 Correct 17 ms 28116 KB Output is correct
4 Correct 17 ms 28116 KB Output is correct
5 Correct 38 ms 28200 KB Output is correct
6 Correct 71 ms 29388 KB Output is correct
7 Correct 25 ms 28104 KB Output is correct
8 Correct 18 ms 28164 KB Output is correct
9 Correct 54 ms 28328 KB Output is correct
10 Correct 68 ms 28584 KB Output is correct
11 Correct 113 ms 29196 KB Output is correct
12 Correct 74 ms 29388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 34204 KB Output is correct
2 Correct 69 ms 34524 KB Output is correct
3 Correct 866 ms 36768 KB Output is correct
4 Correct 69 ms 35328 KB Output is correct
5 Correct 1971 ms 39280 KB Output is correct
6 Correct 625 ms 33772 KB Output is correct
7 Correct 5351 ms 47932 KB Output is correct
8 Correct 2988 ms 43044 KB Output is correct
9 Correct 79 ms 35760 KB Output is correct
10 Correct 58 ms 35864 KB Output is correct
11 Correct 520 ms 36284 KB Output is correct
12 Correct 2827 ms 43908 KB Output is correct
13 Correct 3884 ms 46528 KB Output is correct
14 Correct 5646 ms 49544 KB Output is correct
15 Correct 5371 ms 49812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 34108 KB Output is correct
2 Correct 68 ms 34272 KB Output is correct
3 Correct 69 ms 34500 KB Output is correct
4 Correct 81 ms 34752 KB Output is correct
5 Correct 761 ms 34556 KB Output is correct
6 Correct 879 ms 33652 KB Output is correct
7 Correct 5272 ms 86788 KB Output is correct
8 Correct 10870 ms 118056 KB Output is correct
9 Correct 76 ms 34916 KB Output is correct
10 Correct 896 ms 35460 KB Output is correct
11 Correct 10367 ms 152776 KB Output is correct
12 Execution timed out 15047 ms 94216 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 28236 KB Output is correct
2 Correct 22 ms 28116 KB Output is correct
3 Correct 17 ms 28116 KB Output is correct
4 Correct 17 ms 28116 KB Output is correct
5 Correct 38 ms 28200 KB Output is correct
6 Correct 71 ms 29388 KB Output is correct
7 Correct 25 ms 28104 KB Output is correct
8 Correct 18 ms 28164 KB Output is correct
9 Correct 54 ms 28328 KB Output is correct
10 Correct 68 ms 28584 KB Output is correct
11 Correct 113 ms 29196 KB Output is correct
12 Correct 74 ms 29388 KB Output is correct
13 Correct 64 ms 34204 KB Output is correct
14 Correct 69 ms 34524 KB Output is correct
15 Correct 866 ms 36768 KB Output is correct
16 Correct 69 ms 35328 KB Output is correct
17 Correct 1971 ms 39280 KB Output is correct
18 Correct 625 ms 33772 KB Output is correct
19 Correct 5351 ms 47932 KB Output is correct
20 Correct 2988 ms 43044 KB Output is correct
21 Correct 79 ms 35760 KB Output is correct
22 Correct 58 ms 35864 KB Output is correct
23 Correct 520 ms 36284 KB Output is correct
24 Correct 2827 ms 43908 KB Output is correct
25 Correct 3884 ms 46528 KB Output is correct
26 Correct 5646 ms 49544 KB Output is correct
27 Correct 5371 ms 49812 KB Output is correct
28 Correct 48 ms 34108 KB Output is correct
29 Correct 68 ms 34272 KB Output is correct
30 Correct 69 ms 34500 KB Output is correct
31 Correct 81 ms 34752 KB Output is correct
32 Correct 761 ms 34556 KB Output is correct
33 Correct 879 ms 33652 KB Output is correct
34 Correct 5272 ms 86788 KB Output is correct
35 Correct 10870 ms 118056 KB Output is correct
36 Correct 76 ms 34916 KB Output is correct
37 Correct 896 ms 35460 KB Output is correct
38 Correct 10367 ms 152776 KB Output is correct
39 Execution timed out 15047 ms 94216 KB Time limit exceeded
40 Halted 0 ms 0 KB -