답안 #348603

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
348603 2021-01-15T09:28:29 Z Return_0 코끼리 (Dancing Elephants) (IOI11_elephants) C++17
100 / 100
6689 ms 22588 KB
#include "elephants.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
#pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx,avx2,fma")
 
using namespace std;
using namespace __gnu_pbds;
 
typedef int ll;
typedef long double ld;
typedef pair <ll, ll> pll;
 
#ifdef SINA
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) {
	const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#define dbg2(x, j, n) cout<< #x << " : "; output((j), (n), x, 1);
#else
#define dbg(...) 0
#define dbg2(x, j, n) 0
#endif
#define SZ(x) ((ll)((x).size()))
#define File(s, t) freopen(s ".txt", "r", stdin); freopen(t ".txt", "w", stdout);
#define input(j, n, a) for (int _i = (j); _i < (n)+(j); _i++) cin>> a[_i];
#define output(j, n, a, t) for (int _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n');
#define kill(x) return cout<< x << endl, 0
#define cl const ll
#define fr first
#define sc second
#define lc (v << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define All(x) (x).begin(), (x).end()
 
cl inf = sizeof(ll) == 4 ? (1e9 + 10) : (3e18), mod = 1e9 + 7, MOD = 998244353;
 
template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
	return out << '(' << a.first << ", " << a.second << ')';    }
template <class A> ostream& operator << (ostream& out, const vector<A> &a) {
	if(!a.size())return cout<<"[]";cout<< '[' << a[0]; for (int i = 0; ++i < (int)(a.size());) cout<< ", " << a[i];cout<< ']';  }
template <class T, typename _t = less <T> > using Tree = tree <T, null_type, _t, rb_tree_tag, tree_order_statistics_node_update>;
 
cl N = 1.5e5 + 7, SQ = 400;
 
ll a [N], pos [N], n, L, dp [SQ][3 * SQ + 3], dp2 [SQ][3 * SQ + 3], mxsz, idx [N];
vector <ll> block [SQ];
multiset <ll> st;
 
ll Find (ll x) {
	if (st.empty() || x > *st.rbegin()) return ((n - 1) / SQ);
	ll t;
	for (t = 0; t < SQ && (block[t].empty() || block[t].back() < x); t++);
	return t;
}
 
void Ins (ll id, ll x) {
	block[id].push_back(x);
	for (ll i = SZ(block[id]) - 2; i >= 0; i--) {
		if (block[id][i] > block[id][i+1])  swap(block[id][i], block[id][i+1]);
		else break;
	}
}
 
void build (ll id) {
	mxsz = max(mxsz, SZ(block[id]));
	dp[id][SZ(block[id])] = 0;
	for (ll l = SZ(block[id]), r = SZ(block[id]) - 1; l--;) {
		while (block[id][r] - block[id][l] > L)	r--;
		dp[id][l] = dp[id][r + 1] + 1;
		if (r + 1 == SZ(block[id]))	dp2[id][l] = block[id][l] + L;
		else dp2[id][l] = dp2[id][r + 1];
	}
}
 
ll solve () {
	ll ans = 0, cur = -1;
	for (ll i = 0; i < SQ; i++) {
		if (block[i].empty() || block[i].back() <= cur)	continue;
		ll ptr = upper_bound(All(block[i]), cur) - block[i].begin();
		ans += dp[i][ptr];
		cur = dp2[i][ptr];
  	}
  	return ans;
}
 
void rebuild () {
	mxsz = 0;
	iota(idx, idx + n, 0);
	sort(idx, idx + n, [&](ll x, ll y) { return a[x] < a[y]; });
	for (ll i = 0; i < SQ; i++) block[i].clear();
	for (ll i = 0; i < n; i++) block[i / SQ].push_back(a[idx[i]]), pos[idx[i]] = i / SQ;
	for (ll i = 0; i < SQ; i++) build(i);
}
 
void init (ll _n, ll _L, ll arr []) {
	n = _n, L = _L;
	for (ll i = 0; i < n; i++)	a[i] = arr[i], pos[i] = i / SQ, block[i / SQ].push_back(a[i]), st.insert(a[i]);
	for (ll i = 0; i < SQ; i++)	build(i);
}
 
ll update (ll i, ll y) {
	if (mxsz > 3 * SQ)  rebuild();
	block[pos[i]].erase(lower_bound(All(block[pos[i]]), a[i]));
	build(pos[i]);
	st.erase(st.find(a[i]));
	a[i] = y;
	pos[i] = Find(a[i]);
	st.insert(a[i]);
	Ins(pos[i], a[i]);
	build(pos[i]);
	return solve();
}

Compilation message

elephants.cpp: In function 'std::ostream& operator<<(std::ostream&, const std::vector<_Tp>&)':
elephants.cpp:44:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   44 |  if(!a.size())return cout<<"[]";cout<< '[' << a[0]; for (int i = 0; ++i < (int)(a.size());) cout<< ", " << a[i];cout<< ']';  }
      |  ^~
elephants.cpp:44:33: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   44 |  if(!a.size())return cout<<"[]";cout<< '[' << a[0]; for (int i = 0; ++i < (int)(a.size());) cout<< ", " << a[i];cout<< ']';  }
      |                                 ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2028 KB Output is correct
2 Correct 1 ms 2028 KB Output is correct
3 Correct 1 ms 2028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2028 KB Output is correct
2 Correct 1 ms 2028 KB Output is correct
3 Correct 1 ms 2028 KB Output is correct
4 Correct 2 ms 2028 KB Output is correct
5 Correct 2 ms 2028 KB Output is correct
6 Correct 1 ms 2028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2028 KB Output is correct
2 Correct 1 ms 2028 KB Output is correct
3 Correct 1 ms 2028 KB Output is correct
4 Correct 2 ms 2028 KB Output is correct
5 Correct 2 ms 2028 KB Output is correct
6 Correct 1 ms 2028 KB Output is correct
7 Correct 296 ms 3820 KB Output is correct
8 Correct 321 ms 4076 KB Output is correct
9 Correct 319 ms 6508 KB Output is correct
10 Correct 617 ms 6764 KB Output is correct
11 Correct 592 ms 6764 KB Output is correct
12 Correct 616 ms 6892 KB Output is correct
13 Correct 599 ms 6636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2028 KB Output is correct
2 Correct 1 ms 2028 KB Output is correct
3 Correct 1 ms 2028 KB Output is correct
4 Correct 2 ms 2028 KB Output is correct
5 Correct 2 ms 2028 KB Output is correct
6 Correct 1 ms 2028 KB Output is correct
7 Correct 296 ms 3820 KB Output is correct
8 Correct 321 ms 4076 KB Output is correct
9 Correct 319 ms 6508 KB Output is correct
10 Correct 617 ms 6764 KB Output is correct
11 Correct 592 ms 6764 KB Output is correct
12 Correct 616 ms 6892 KB Output is correct
13 Correct 599 ms 6636 KB Output is correct
14 Correct 404 ms 4460 KB Output is correct
15 Correct 366 ms 4716 KB Output is correct
16 Correct 898 ms 6764 KB Output is correct
17 Correct 1010 ms 8940 KB Output is correct
18 Correct 1246 ms 8756 KB Output is correct
19 Correct 1174 ms 8548 KB Output is correct
20 Correct 1135 ms 8556 KB Output is correct
21 Correct 1073 ms 8812 KB Output is correct
22 Correct 1158 ms 8548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2028 KB Output is correct
2 Correct 1 ms 2028 KB Output is correct
3 Correct 1 ms 2028 KB Output is correct
4 Correct 2 ms 2028 KB Output is correct
5 Correct 2 ms 2028 KB Output is correct
6 Correct 1 ms 2028 KB Output is correct
7 Correct 296 ms 3820 KB Output is correct
8 Correct 321 ms 4076 KB Output is correct
9 Correct 319 ms 6508 KB Output is correct
10 Correct 617 ms 6764 KB Output is correct
11 Correct 592 ms 6764 KB Output is correct
12 Correct 616 ms 6892 KB Output is correct
13 Correct 599 ms 6636 KB Output is correct
14 Correct 404 ms 4460 KB Output is correct
15 Correct 366 ms 4716 KB Output is correct
16 Correct 898 ms 6764 KB Output is correct
17 Correct 1010 ms 8940 KB Output is correct
18 Correct 1246 ms 8756 KB Output is correct
19 Correct 1174 ms 8548 KB Output is correct
20 Correct 1135 ms 8556 KB Output is correct
21 Correct 1073 ms 8812 KB Output is correct
22 Correct 1158 ms 8548 KB Output is correct
23 Correct 3395 ms 15468 KB Output is correct
24 Correct 3726 ms 15368 KB Output is correct
25 Correct 2935 ms 15468 KB Output is correct
26 Correct 6101 ms 15964 KB Output is correct
27 Correct 6689 ms 16108 KB Output is correct
28 Correct 1133 ms 7148 KB Output is correct
29 Correct 1200 ms 7260 KB Output is correct
30 Correct 1121 ms 7276 KB Output is correct
31 Correct 1156 ms 7276 KB Output is correct
32 Correct 4789 ms 20552 KB Output is correct
33 Correct 4541 ms 19760 KB Output is correct
34 Correct 5787 ms 20588 KB Output is correct
35 Correct 3918 ms 22588 KB Output is correct
36 Correct 2205 ms 20292 KB Output is correct
37 Correct 3325 ms 20888 KB Output is correct
38 Correct 5993 ms 19660 KB Output is correct
39 Correct 5983 ms 20692 KB Output is correct
40 Correct 5874 ms 19568 KB Output is correct
41 Correct 4206 ms 20588 KB Output is correct
42 Correct 4595 ms 20972 KB Output is correct