Submission #225031

# Submission time Handle Problem Language Result Execution time Memory
225031 2020-04-19T08:16:56 Z davitmarg Dancing Elephants (IOI11_elephants) C++17
26 / 100
9000 ms 1408 KB
/*DavitMarg*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <list>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
using namespace std;


#ifndef death
#include "elephants.h"
#endif

const int N = 50004, K = 50004;


int n, len;
vector<pair<int, int>> dp[N / K + 10],block[N / K + 10],x;
pair<int, int> pos[N];

void build(int i)
{
	dp[i].clear();
	dp[i].resize(block[i].size());
	for (int j = block[i].size() - 1; j >= 0; j--)
	{
		if (block[i].empty())
			continue;
		if (block[i].back().first <= block[i][j].first + len)
		{
			dp[i][j] = MP(1, block[i][j].first + len);
			continue;
		}
		int pos = upper_bound(all(block[i]), MP(block[i][j].first + len, n)) - block[i].begin();
		dp[i][j] = MP(dp[i][pos].first + 1, dp[i][pos].second);
	}
}

void build()
{
	sort(all(x));
	for (int i = 0; i <= n / K; i++)
		block[i].clear();
	for (int i = 0; i < n; i++)
	{
		block[i / K].push_back(x[i]);
		pos[x[i].second] = MP(i / K, i % K);
	}

	for (int i = 0; i <= n / K; i++)
		build(i);
}

int get()
{
	int last = -mod;
	int ans = 0;
	for (int i = 0; i <= n / K; i++)
	{
		if (block[i].empty())
			continue;
		if (block[i].back().first <= last)
			continue;
		int pos = upper_bound(all(block[i]), MP(last, n)) - block[i].begin();
		last = dp[i][pos].second;
		ans += dp[i][pos].first;
	}
	return ans;
}

int cnt = -1;

int update(int i, int nx)
{
	cnt++;
	sort(all(x), [](pair<int, int> a, pair<int, int> b) {
		return a.second < b.second;
	});
	if (cnt % K == 0)
	{
		x[i].first = nx;
		build();
		return get();
	}

	int bi = pos[i].first;

	swap(block[bi][pos[i].second], block[bi].back());
	block[bi].pop_back();
	for (int j = 0; j < block[bi].size(); j++)
		pos[block[bi][j].second].second = j;
	build(bi);
	x[i].first = nx;

	for (int j = n/K; j >= 0; j--)
	{
		if (!block[j].empty() && block[j][0].first <= nx)
		{
			bi = j;
			break;
		}
	}

	block[bi].push_back(x[i]);
	sort(all(block[bi]));
	for (int j = 0; j < block[bi].size(); j++)
		pos[block[bi][j].second].second = j;
	build(bi);

	return get();
}

void init(int N, int L, int X[])
{
	n = N;
	len = L;
	for (int i = 0; i < n; i++)
		x.push_back(MP(X[i], i));
}



#ifdef death

int main()
{
	int n,len,x[N],q;
	cin >> n >> len;
	for (int i = 0; i < n; i++)
		cin >> x[i];
	init(n, len, x);
	cin >> q;
	while (q--)
	{
		int i, pos;
		cin >> i >> pos;
		cout << update(i, pos) << endl;
	}


	return 0;
}

#endif

/*

4 10 
10 15 17 20
5
2 16
1 25
3 35
0 38
2 0

4 5
10 15 20 25
4
0 10
1 15
2 20
3 25

*/

Compilation message

elephants.cpp: In function 'int update(int, int)':
elephants.cpp:110:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int j = 0; j < block[bi].size(); j++)
                  ~~^~~~~~~~~~~~~~~~~~
elephants.cpp:126:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int j = 0; j < block[bi].size(); j++)
                  ~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Execution timed out 9030 ms 1408 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Execution timed out 9030 ms 1408 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Execution timed out 9030 ms 1408 KB Time limit exceeded
8 Halted 0 ms 0 KB -