답안 #965456

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
965456 2024-04-18T16:02:33 Z red24 Pilot (NOI19_pilot) C++14
89 / 100
1000 ms 94112 KB
#include <bits/stdc++.h>
using namespace std;
 
#define int long long
#define vi vector<int>
#define pi pair<int, int>
#define vpi vector<pi>
#define vvi vector<vi>
#define mp make_pair
#define pb push_back
#define all(x) x.begin()+1, x.end()
const int INF = (int)1e17;

vi parent, siz;

void create(int x)
{
	parent[x] = x;
	siz[x] = 1;
}

int find(int x)
{
	if (parent[x] == x) return x;
	return parent[x] = find(parent[x]);
}

void unite(int x, int y)
{
	x = find(x);
	y = find(y);

	if (siz[x] < siz[y]) swap(x, y);

	parent[y] = x;
	siz[x] += siz[y];
}

int f(pi x)
{
	return ((x.second-x.first+1)*(x.second-x.first+2))/2;
}

void solve()
{
	int n, q; cin >> n >> q;
	vi a(n+1);
	for (int i = 1; i <= n; i++) cin >> a[i];

	const int mx = (int)1e6+100;
	vi ans(mx+1);
	vvi idx(mx+1);

	for (int i = 1; i <= n; i++)
	{
		idx[a[i]].pb(i);
	}

	int res = 0;

	set<pi> st;
	for (int el = 1; el <= mx; el++)
	{
		for (auto i : idx[el])
		{
			// find next interval
			auto nxt_it = st.upper_bound(mp(i, 0));
			if (nxt_it != st.end())
			{
				// if there is no prev
				if (nxt_it == st.begin())
				{
					if ((*nxt_it).first == i+1)
					{
						pi nxt_replace = mp(i,(*nxt_it).second);
						res -= f(*nxt_it);
						res += f(nxt_replace);
						st.erase(nxt_it);
						st.insert(nxt_replace);
					}
					else
					{
						st.insert(mp(i, i));
						res += 1;
					}
				}
				else
				{
					auto prev_it = prev(nxt_it);
					if ((*nxt_it).first == i+1 && (*prev_it).second == i-1)
					{
						pi nxt_replace = mp((*prev_it).first, (*nxt_it).second);
						res -= f(*nxt_it);
						res -= f(*prev_it);
						res += f(nxt_replace);
						st.erase(nxt_it);
						st.erase(prev_it);
						st.insert(nxt_replace);
					}
					else if ((*nxt_it).first == i+1)
					{
						pi nxt_replace = mp(i, (*nxt_it).second);
						res -= f(*nxt_it);
						res += f(nxt_replace);
						st.erase(nxt_it);
						st.insert(nxt_replace);
					}
					else if ((*prev_it).second == i-1)
					{
						pi nxt_replace = mp((*prev_it).first, i);
						res -= f(*prev_it);
						res += f(nxt_replace);
						st.erase(prev_it);
						st.insert(nxt_replace);
					}
					else
					{
						st.insert(mp(i,i));
						res++;
					}
				}
			}
			else
			{
				if (st.size() == 0)
				{
					st.insert(mp(i,i));
					res++;
					continue;
				}
				auto prev_it = prev(nxt_it);
				if ((*prev_it).second == i-1)
				{
					pi prev_replace = mp((*prev_it).first, i);
					res -= f(*prev_it);
					res += f(prev_replace);
					st.erase(prev_it);
					st.insert(prev_replace);
				}
				else
				{
					st.insert(mp(i,i));
					res++;
				}
			}
		}
		ans[el] = res;
	}
	while (q--)
	{
		int x; cin >> x;
		cout << ans[x] << '\n';
	}
}
 
int32_t main()
{
	auto begin = std::chrono::high_resolution_clock::now();
	// FASTIO
	ios_base::sync_with_stdio(0);
	cin.tie(NULL); cout.tie(NULL);
 
 
 
	int T = 1;
	//cin >> T;
	for (int i = 1; i <= T; i++)
	{
		//cout << "Case #" << i << ": ";
		solve();
	}
	auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 31576 KB Output is correct
2 Correct 11 ms 31576 KB Output is correct
3 Correct 11 ms 31580 KB Output is correct
4 Correct 10 ms 31780 KB Output is correct
5 Correct 12 ms 31576 KB Output is correct
6 Correct 10 ms 31580 KB Output is correct
7 Correct 11 ms 31644 KB Output is correct
8 Correct 10 ms 31580 KB Output is correct
9 Correct 10 ms 31580 KB Output is correct
10 Correct 10 ms 31580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 31576 KB Output is correct
2 Correct 11 ms 31576 KB Output is correct
3 Correct 11 ms 31580 KB Output is correct
4 Correct 10 ms 31780 KB Output is correct
5 Correct 12 ms 31576 KB Output is correct
6 Correct 10 ms 31580 KB Output is correct
7 Correct 11 ms 31644 KB Output is correct
8 Correct 10 ms 31580 KB Output is correct
9 Correct 10 ms 31580 KB Output is correct
10 Correct 10 ms 31580 KB Output is correct
11 Correct 11 ms 31580 KB Output is correct
12 Correct 11 ms 31580 KB Output is correct
13 Correct 11 ms 31580 KB Output is correct
14 Correct 10 ms 31580 KB Output is correct
15 Correct 11 ms 31604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 31576 KB Output is correct
2 Correct 11 ms 31576 KB Output is correct
3 Correct 11 ms 31580 KB Output is correct
4 Correct 10 ms 31780 KB Output is correct
5 Correct 12 ms 31576 KB Output is correct
6 Correct 10 ms 31580 KB Output is correct
7 Correct 11 ms 31644 KB Output is correct
8 Correct 10 ms 31580 KB Output is correct
9 Correct 10 ms 31580 KB Output is correct
10 Correct 10 ms 31580 KB Output is correct
11 Correct 11 ms 31580 KB Output is correct
12 Correct 11 ms 31580 KB Output is correct
13 Correct 11 ms 31580 KB Output is correct
14 Correct 10 ms 31580 KB Output is correct
15 Correct 11 ms 31604 KB Output is correct
16 Correct 12 ms 31580 KB Output is correct
17 Correct 13 ms 31580 KB Output is correct
18 Correct 10 ms 31580 KB Output is correct
19 Correct 12 ms 31832 KB Output is correct
20 Correct 10 ms 31580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 31576 KB Output is correct
2 Correct 11 ms 31576 KB Output is correct
3 Correct 11 ms 31580 KB Output is correct
4 Correct 10 ms 31780 KB Output is correct
5 Correct 12 ms 31576 KB Output is correct
6 Correct 10 ms 31580 KB Output is correct
7 Correct 11 ms 31644 KB Output is correct
8 Correct 10 ms 31580 KB Output is correct
9 Correct 10 ms 31580 KB Output is correct
10 Correct 10 ms 31580 KB Output is correct
11 Correct 11 ms 31580 KB Output is correct
12 Correct 11 ms 31580 KB Output is correct
13 Correct 11 ms 31580 KB Output is correct
14 Correct 10 ms 31580 KB Output is correct
15 Correct 11 ms 31604 KB Output is correct
16 Correct 12 ms 31580 KB Output is correct
17 Correct 13 ms 31580 KB Output is correct
18 Correct 10 ms 31580 KB Output is correct
19 Correct 12 ms 31832 KB Output is correct
20 Correct 10 ms 31580 KB Output is correct
21 Correct 12 ms 31836 KB Output is correct
22 Correct 12 ms 32088 KB Output is correct
23 Correct 11 ms 31836 KB Output is correct
24 Correct 11 ms 31836 KB Output is correct
25 Correct 11 ms 31836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 35928 KB Output is correct
2 Correct 73 ms 37428 KB Output is correct
3 Correct 53 ms 36444 KB Output is correct
4 Correct 71 ms 37056 KB Output is correct
5 Correct 53 ms 36432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 36180 KB Output is correct
2 Correct 48 ms 36188 KB Output is correct
3 Correct 42 ms 36176 KB Output is correct
4 Correct 38 ms 36432 KB Output is correct
5 Correct 38 ms 36188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 36428 KB Output is correct
2 Correct 46 ms 36248 KB Output is correct
3 Correct 43 ms 36176 KB Output is correct
4 Correct 46 ms 36432 KB Output is correct
5 Correct 49 ms 36640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 31576 KB Output is correct
2 Correct 11 ms 31576 KB Output is correct
3 Correct 11 ms 31580 KB Output is correct
4 Correct 10 ms 31780 KB Output is correct
5 Correct 12 ms 31576 KB Output is correct
6 Correct 10 ms 31580 KB Output is correct
7 Correct 11 ms 31644 KB Output is correct
8 Correct 10 ms 31580 KB Output is correct
9 Correct 10 ms 31580 KB Output is correct
10 Correct 10 ms 31580 KB Output is correct
11 Correct 58 ms 35928 KB Output is correct
12 Correct 73 ms 37428 KB Output is correct
13 Correct 53 ms 36444 KB Output is correct
14 Correct 71 ms 37056 KB Output is correct
15 Correct 53 ms 36432 KB Output is correct
16 Correct 51 ms 36416 KB Output is correct
17 Correct 73 ms 37644 KB Output is correct
18 Correct 68 ms 37492 KB Output is correct
19 Correct 51 ms 36188 KB Output is correct
20 Correct 62 ms 37552 KB Output is correct
21 Correct 53 ms 36188 KB Output is correct
22 Correct 63 ms 37168 KB Output is correct
23 Correct 52 ms 36692 KB Output is correct
24 Correct 60 ms 37260 KB Output is correct
25 Correct 61 ms 36944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 31576 KB Output is correct
2 Correct 11 ms 31576 KB Output is correct
3 Correct 11 ms 31580 KB Output is correct
4 Correct 10 ms 31780 KB Output is correct
5 Correct 12 ms 31576 KB Output is correct
6 Correct 10 ms 31580 KB Output is correct
7 Correct 11 ms 31644 KB Output is correct
8 Correct 10 ms 31580 KB Output is correct
9 Correct 10 ms 31580 KB Output is correct
10 Correct 10 ms 31580 KB Output is correct
11 Correct 11 ms 31580 KB Output is correct
12 Correct 11 ms 31580 KB Output is correct
13 Correct 11 ms 31580 KB Output is correct
14 Correct 10 ms 31580 KB Output is correct
15 Correct 11 ms 31604 KB Output is correct
16 Correct 12 ms 31580 KB Output is correct
17 Correct 13 ms 31580 KB Output is correct
18 Correct 10 ms 31580 KB Output is correct
19 Correct 12 ms 31832 KB Output is correct
20 Correct 10 ms 31580 KB Output is correct
21 Correct 12 ms 31836 KB Output is correct
22 Correct 12 ms 32088 KB Output is correct
23 Correct 11 ms 31836 KB Output is correct
24 Correct 11 ms 31836 KB Output is correct
25 Correct 11 ms 31836 KB Output is correct
26 Correct 58 ms 35928 KB Output is correct
27 Correct 73 ms 37428 KB Output is correct
28 Correct 53 ms 36444 KB Output is correct
29 Correct 71 ms 37056 KB Output is correct
30 Correct 53 ms 36432 KB Output is correct
31 Correct 37 ms 36180 KB Output is correct
32 Correct 48 ms 36188 KB Output is correct
33 Correct 42 ms 36176 KB Output is correct
34 Correct 38 ms 36432 KB Output is correct
35 Correct 38 ms 36188 KB Output is correct
36 Correct 46 ms 36428 KB Output is correct
37 Correct 46 ms 36248 KB Output is correct
38 Correct 43 ms 36176 KB Output is correct
39 Correct 46 ms 36432 KB Output is correct
40 Correct 49 ms 36640 KB Output is correct
41 Correct 51 ms 36416 KB Output is correct
42 Correct 73 ms 37644 KB Output is correct
43 Correct 68 ms 37492 KB Output is correct
44 Correct 51 ms 36188 KB Output is correct
45 Correct 62 ms 37552 KB Output is correct
46 Correct 53 ms 36188 KB Output is correct
47 Correct 63 ms 37168 KB Output is correct
48 Correct 52 ms 36692 KB Output is correct
49 Correct 60 ms 37260 KB Output is correct
50 Correct 61 ms 36944 KB Output is correct
51 Correct 70 ms 37796 KB Output is correct
52 Correct 80 ms 38724 KB Output is correct
53 Correct 82 ms 37972 KB Output is correct
54 Correct 91 ms 38764 KB Output is correct
55 Correct 70 ms 38004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 31576 KB Output is correct
2 Correct 11 ms 31576 KB Output is correct
3 Correct 11 ms 31580 KB Output is correct
4 Correct 10 ms 31780 KB Output is correct
5 Correct 12 ms 31576 KB Output is correct
6 Correct 10 ms 31580 KB Output is correct
7 Correct 11 ms 31644 KB Output is correct
8 Correct 10 ms 31580 KB Output is correct
9 Correct 10 ms 31580 KB Output is correct
10 Correct 10 ms 31580 KB Output is correct
11 Correct 11 ms 31580 KB Output is correct
12 Correct 11 ms 31580 KB Output is correct
13 Correct 11 ms 31580 KB Output is correct
14 Correct 10 ms 31580 KB Output is correct
15 Correct 11 ms 31604 KB Output is correct
16 Correct 12 ms 31580 KB Output is correct
17 Correct 13 ms 31580 KB Output is correct
18 Correct 10 ms 31580 KB Output is correct
19 Correct 12 ms 31832 KB Output is correct
20 Correct 10 ms 31580 KB Output is correct
21 Correct 12 ms 31836 KB Output is correct
22 Correct 12 ms 32088 KB Output is correct
23 Correct 11 ms 31836 KB Output is correct
24 Correct 11 ms 31836 KB Output is correct
25 Correct 11 ms 31836 KB Output is correct
26 Correct 58 ms 35928 KB Output is correct
27 Correct 73 ms 37428 KB Output is correct
28 Correct 53 ms 36444 KB Output is correct
29 Correct 71 ms 37056 KB Output is correct
30 Correct 53 ms 36432 KB Output is correct
31 Correct 37 ms 36180 KB Output is correct
32 Correct 48 ms 36188 KB Output is correct
33 Correct 42 ms 36176 KB Output is correct
34 Correct 38 ms 36432 KB Output is correct
35 Correct 38 ms 36188 KB Output is correct
36 Correct 46 ms 36428 KB Output is correct
37 Correct 46 ms 36248 KB Output is correct
38 Correct 43 ms 36176 KB Output is correct
39 Correct 46 ms 36432 KB Output is correct
40 Correct 49 ms 36640 KB Output is correct
41 Correct 51 ms 36416 KB Output is correct
42 Correct 73 ms 37644 KB Output is correct
43 Correct 68 ms 37492 KB Output is correct
44 Correct 51 ms 36188 KB Output is correct
45 Correct 62 ms 37552 KB Output is correct
46 Correct 53 ms 36188 KB Output is correct
47 Correct 63 ms 37168 KB Output is correct
48 Correct 52 ms 36692 KB Output is correct
49 Correct 60 ms 37260 KB Output is correct
50 Correct 61 ms 36944 KB Output is correct
51 Correct 70 ms 37796 KB Output is correct
52 Correct 80 ms 38724 KB Output is correct
53 Correct 82 ms 37972 KB Output is correct
54 Correct 91 ms 38764 KB Output is correct
55 Correct 70 ms 38004 KB Output is correct
56 Correct 939 ms 94112 KB Output is correct
57 Execution timed out 1042 ms 91840 KB Time limit exceeded
58 Halted 0 ms 0 KB -