답안 #112500

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
112500 2019-05-20T09:58:27 Z AngusRitossa Bitaro, who Leaps through Time (JOI19_timeleap) C++14
34 / 100
3000 ms 62648 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
	#define D(x...) printf(x)
#else
	#define D(x...)
#endif
typedef long long ll;
typedef pair<pair<int, int>, pair<int, int> > piiii;
int n, q;
int l[300010], r[300010];
piiii rangetree[300010*4];
pair<int, int> maxmin(pair<int, int> a, pair<int, int> b)
{
	if (a.first == b.first) return min(a, b);
	else return max(a, b);
}
void update(int node, pair<int, int> val, int curr = 1, int cstart = 0, int cend = 300000)
{
	if (cstart == cend)
	{
		rangetree[curr] = { { val.first, node }, { val.second, node } };
		return;
	}
	int mid = (cstart+cend)/2;
	if (node <= mid) update(node, val, 2*curr, cstart, mid);
	else update(node, val, 2*curr+1, mid+1, cend);
	rangetree[curr].first = maxmin(rangetree[2*curr].first, rangetree[2*curr+1].first);
	rangetree[curr].second = min(rangetree[2*curr].second, rangetree[2*curr+1].second);
}
piiii save[300010*4];
int upto, seen[300010*4];
piiii query(int s, int e, int curr = 1, int cstart = 0, int cend = 300000)
{
	if (s <= cstart && cend <= e) return rangetree[curr];
	if (seen[curr] == upto) return save[curr];
	seen[curr] = upto;
	int mid = (cstart+cend)/2;
	if (e <= mid) return save[curr] = query(s, e, 2*curr, cstart, mid);
	else if (s > mid) return save[curr] = query(s, e, 2*curr+1, mid+1, cend);
	else 
	{
		auto a = query(s, e, 2*curr, cstart, mid);
		auto b = query(s, e, 2*curr+1, mid+1, cend);
		return save[curr] = { maxmin(a.first, b.first), min(a.second, b.second) };
	}
}
bool rip;
int firstbefore(int node, int hei, bool checkl, int curr = 1, int cstart = 0, int cend = 300000)
{
	if (cstart == cend) return cstart;
	int mid = (cstart+cend)/2;
	if (node-1 <= mid) return firstbefore(node, hei, checkl, 2*curr, cstart, mid);
	auto q = query(mid+1, node-1, 2*curr+1, mid+1, cend);
	if (q.first.first > hei || q.second.first < hei) return firstbefore(node, hei, checkl, 2*curr+1, mid+1, cend);
	else 
	{
		if (checkl && q.first.first == hei) 
		{
			rip = 1;
			return 0;
		}
		else if (!checkl && q.second.first == hei)
		{
			rip = 1;
			return 0;
		}
		return firstbefore(node, hei, checkl, 2*curr, cstart, mid);
	}
}
int firstafter(int node, int hei, int curr = 1, int cstart = 0, int cend = 300000)
{
	if (cstart == cend) return cstart;
	int mid = (cstart+cend)/2;
	if (node >= mid) return firstafter(node, hei, 2*curr+1, mid+1, cend);
	auto q = query(node+1, mid, 2*curr, cstart, mid);
	if (q.first.first > hei || q.second.first < hei) return firstafter(node, hei, 2*curr, cstart, mid);
	else return firstafter(node, hei, 2*curr+1, mid+1, cend);
}
int firstbefore0(int node, int hei, bool checkl, int curr = 1, int cstart = 0, int cend = 300000)
{
	if (cstart == cend) return cstart;
	int mid = (cstart+cend)/2;
	if (node-1 <= mid) return firstbefore0(node, hei, checkl, 2*curr, cstart, mid);
	auto q = query(mid+1, node-1, 2*curr+1, mid+1, cend);
	if (checkl) 
	{
		if (q.first.first >= hei || q.second.first < hei) return firstbefore0(node, hei, checkl, 2*curr+1, mid+1, cend);
	}
	else 
	{
		if (q.first.first > hei || q.second.first <= hei) return firstbefore0(node, hei, checkl, 2*curr+1, mid+1, cend);
	}
	return firstbefore0(node, hei, checkl, 2*curr, cstart, mid);
}
ll rt2[300010*4];
void upd2(int node, int val, int curr = 1, int cstart = 0, int cend = 300000)
{
	if (cstart == cend) 
	{
		rt2[curr] = val;
		return;
	}
	int mid = (cstart+cend)/2;
	if (node <= mid) upd2(node, val, 2*curr, cstart, mid);
	else upd2(node, val, 2*curr+1, mid+1, cend);
	rt2[curr] = rt2[2*curr]+rt2[2*curr+1];
}
ll qu2(int s, int e, int curr = 1, int cstart = 0, int cend = 300000)
{
	if (s <= cstart && cend <= e) return rt2[curr];
	int mid = (cstart+cend)/2;
	if (e <= mid) return qu2(s, e, 2*curr, cstart, mid);
	else if (s > mid) return qu2(s, e, 2*curr+1, mid+1, cend);
	else return qu2(s, e, 2*curr, cstart, mid)+qu2(s, e, 2*curr+1, mid+1, cend);
}
pair<int, int> firstonleft(int a, int hei, bool checkl)
{
	upto++;
	rip = 0;
	int s = firstbefore(a, hei, checkl);
	int type = l[s] > hei; // if 1 above, if 0 below
	return { s, type };
}
bool firstonleftactually0(int a, int hei, bool checkl)
{
	upto++;
	int s = firstbefore0(a, hei, checkl);
	if (checkl) return l[s] == hei;
	else return r[s] == hei;
}
pair<int, int> firstonright(int a, int hei)
{
	upto++;
	int s = firstafter(a, hei);
	int type = l[s] > hei; // if 1 above, if 0 below
	return { s, type };
}
set<pair<int, int> > mnmx;
int was[300010];
void checkforlocalstuff(int a)
{
	// Check if l is local max
	if (!firstonleft(a, l[a], 1).second && !rip && !firstonright(a, l[a]).second) 
	{
		D("local max %d\n", a);
		if (was[a] != 1) mnmx.insert({a, 0}), upd2(a, l[a]);
		if (was[a] != 1 && was[a]) mnmx.erase({a, 1});
		was[a] = 1;
	}
	else
	{
		if (was[a] == 1 || was[a] == -1) mnmx.erase({a, 0});
		if (firstonleft(a, r[a], 0).second && !rip && firstonright(a, r[a]).second) 
		{
			D("local min %d\n", a);
			if (was[a] != 2) mnmx.insert({a, 1}), upd2(a, -r[a]);
			was[a] = 2;
		}
		else
		{
			if (was[a] == 2 || was[a] == -1) mnmx.erase({a, 1});
			if (was[a]) upd2(a, 0);
			was[a] = 0;
		}

	}
}
int A[300010], B[300010], C[300010], D[300010], T[300010], L[300010], R[300010];
ll ANS[300010];
void dothing(bool reverse)
{
	mnmx.clear();
	fill_n(rt2, 300010*4, 0ll);
	fill_n(was, 300010, 0);
	for (int i = 1; i < n; i++) 
	{
		l[i] = L[i], r[i] = R[i];
		if (reverse) l[i] = L[n-i], r[i] = R[n-i];
		l[i]-=i;
		r[i]-=i+1;
		D("%lld %lld\n", l[i], r[i]);
		update(i, { l[i], r[i] });
	}
	update(0, { 2e9, -2e9 });
	update(n, { 2e9, -2e9 });
	// Find local max and mins
	for (int i = 1; i < n; i++)
	{
		checkforlocalstuff(i);
	}
	for (int i = 0; i < q; i++)
	{
		if (T[i] == 1)
		{
			int a = A[i], x = B[i], y = C[i];
			if (reverse) a = n-a;
			was[a] = -1;
			l[a] = x-a, r[a] = y-a-1;
			update(a, { l[a], r[a] });
			// update me
			checkforlocalstuff(a);
			// check thing before me
			if (a != 1)
			{
				auto it = mnmx.lower_bound({a, 0});
				int thing = 1;
				if (it != mnmx.begin()) thing = (--it)->first;
				upto++;
				auto q = query(thing, a-1);
				checkforlocalstuff(q.first.second);
				checkforlocalstuff(q.second.second);
			}
			if (a != n-1)
			{
				auto it = mnmx.lower_bound({a, 2});
				int thing = n-1;
				if (it != mnmx.end()) thing = it->first;
				upto++;
				auto q = query(a+1, thing);
				checkforlocalstuff(q.first.second);
				checkforlocalstuff(q.second.second);
			}
			continue;
		}
		int a = A[i], b = B[i], c = C[i], d = D[i];
		if (a > c && !reverse) continue;
		if (reverse)
		{
			if (a <= c) continue;
			a = n-a+1, c = n-c+1;
		} 
		b-=a, d-=c;
		if (a == c)
		{
			ANS[i] = max(0, b-d);
			continue;
		}
		ll am = 0;
		if (b < l[a]) b = l[a];
		if (b > r[a]) am += b-r[a], b = r[a];
		D("b %lld d %lld am %lld\n", b, d, am);
		// find first occuring secondhalf local thingo 
		auto it = mnmx.lower_bound({a, -1});
		if (it == mnmx.end() || it->first >= c)
		{
			// There are none in between
			upto++;
			auto q = query(a, c-1);
			auto mn = q.second; // mn top
			auto mx = q.first; // mx bot
			int currloc = b;
			ll ans = am;
			if (mn.second < mx.second)
			{
				// go down to mn if its below
				if (mn.first < currloc)
				{
					ans += currloc-mn.first;
					currloc = mn.first;
				}
				// go up to mx if its above
				currloc = max(currloc, mx.first);
				// go down to end
				ans += max(0, currloc-d);
			}
			else
			{
				// go up to mx if its above
				currloc = max(currloc, mx.first);
				// go down to mn if its below
				if (mn.first < currloc)
				{
					ans += currloc-mn.first;
					currloc = mn.first;
				}
				// go down to end
				ans += max(0, currloc-d);
			}
			D("none between: ");
			ANS[i] = ans;
		}
		else
		{
			auto it2 = prev(mnmx.lower_bound({ c, -1 }));
			ll ans = am;
			if (it == it2)
			{
				// one in between
				if (it->second) // Local min
				{
					upto++;
					int mxbefore = query(a, it->first).first.first;
					mxbefore = max(mxbefore, b);
					upto++;
					int mxafter = query(it->first, c-1).first.first;
					mxafter = max(mxafter, d);
					// move down to the local mn if needed
					int pos = mxbefore;
					int lmnheight = r[it->first];
					if (lmnheight < pos) ans += pos-lmnheight, pos = lmnheight;
					// move down/up to mx after
					ans += max(0, pos-mxafter);
					pos = mxafter;
					// move up/down to end
					ans += max(0, pos-d);
				}
				else // Local max
				{
					upto++;
					int mnbefore = query(a, it->first).second.first;
					mnbefore = min(mnbefore, b);
					upto++;
					int mnafter = query(it->first, c-1).second.first;
					mnafter = min(mnafter, d);
					// move down to first thing
					int pos = b;
					ans += pos-mnbefore;
					pos = mnbefore;
					// go up to local max if needed
					pos = max(pos, l[it->first]);
					ans += max(0, pos-mnafter);					
				}
				D("one between: ");
				ANS[i] = ans;
			}
			else
			{
				// two in between
				if (it->second) // first is local min
				{
					upto++;
					int mxbefore = query(a, it->first).first.first;
					mxbefore = max(mxbefore, b);
					mxbefore = max(mxbefore, r[it->first]);
					ans += mxbefore;
				}
				else // first is local max
				{
					upto++;
					int mnbefore = query(a, it->first).second.first;
					mnbefore = min(mnbefore, b);
					mnbefore = min(mnbefore, l[it->first]);
					ans += b-mnbefore;
				}
				if (it2->second) // last is local min
				{
					upto++;
					int mxbefore = query(it2->first, c-1).first.first;
					mxbefore = max(mxbefore, d);
					mxbefore = max(mxbefore, r[it2->first]);
					ans += mxbefore-d;
				}
				else // last is local max
				{
					upto++;
					int mnbefore = query(it2->first, c-1).second.first;
					mnbefore = min(mnbefore, d);
					mnbefore = min(mnbefore, l[it2->first]);
					ans -= mnbefore;
				}
				ans += qu2(a, c-1);
				D(">1 between: ");
				ANS[i] = ans;
			}
		}
	}
}
int main()
{
	scanf("%d%d", &n, &q);
	for (int i = 1; i < n; i++) scanf("%d%d", &L[i], &R[i]);
	for (int i = 0; i < q; i++)
	{
		scanf("%d", &T[i]);
		if (T[i] == 1) scanf("%d%d%d", &A[i], &B[i], &C[i]);
		else scanf("%d%d%d%d", &A[i], &B[i], &C[i], &D[i]);
	}
	dothing(0);
	dothing(1);
	for (int i = 0; i < q; i++)
	{
		if (T[i] == 2) printf("%lld\n", ANS[i]);
	}
}

Compilation message

timeleap.cpp: In function 'int main()':
timeleap.cpp:371:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &q);
  ~~~~~^~~~~~~~~~~~~~~~
timeleap.cpp:372:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i = 1; i < n; i++) scanf("%d%d", &L[i], &R[i]);
                              ~~~~~^~~~~~~~~~~~~~~~~~~~~~
timeleap.cpp:375:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &T[i]);
   ~~~~~^~~~~~~~~~~~~
timeleap.cpp:376:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   if (T[i] == 1) scanf("%d%d%d", &A[i], &B[i], &C[i]);
                  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
timeleap.cpp:377:13: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   else scanf("%d%d%d%d", &A[i], &B[i], &C[i], &D[i]);
        ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 11136 KB Output is correct
2 Correct 11 ms 11136 KB Output is correct
3 Correct 11 ms 11136 KB Output is correct
4 Correct 12 ms 11108 KB Output is correct
5 Correct 12 ms 11136 KB Output is correct
6 Correct 11 ms 11136 KB Output is correct
7 Correct 10 ms 11136 KB Output is correct
8 Correct 11 ms 11136 KB Output is correct
9 Correct 11 ms 11180 KB Output is correct
10 Correct 11 ms 11136 KB Output is correct
11 Correct 19 ms 11264 KB Output is correct
12 Correct 20 ms 11384 KB Output is correct
13 Correct 19 ms 11264 KB Output is correct
14 Correct 19 ms 11264 KB Output is correct
15 Correct 19 ms 11264 KB Output is correct
16 Correct 18 ms 11264 KB Output is correct
17 Correct 19 ms 11264 KB Output is correct
18 Correct 20 ms 11264 KB Output is correct
19 Correct 19 ms 11264 KB Output is correct
20 Correct 20 ms 11264 KB Output is correct
21 Correct 19 ms 11264 KB Output is correct
22 Correct 19 ms 11264 KB Output is correct
23 Correct 20 ms 11264 KB Output is correct
24 Correct 19 ms 11264 KB Output is correct
25 Correct 21 ms 11264 KB Output is correct
26 Correct 22 ms 11264 KB Output is correct
27 Correct 21 ms 11264 KB Output is correct
28 Correct 31 ms 11264 KB Output is correct
29 Correct 20 ms 11264 KB Output is correct
30 Correct 21 ms 11264 KB Output is correct
31 Correct 20 ms 11264 KB Output is correct
32 Correct 19 ms 11264 KB Output is correct
33 Correct 19 ms 11264 KB Output is correct
34 Correct 20 ms 11264 KB Output is correct
35 Correct 20 ms 11264 KB Output is correct
36 Correct 20 ms 11236 KB Output is correct
37 Correct 19 ms 11264 KB Output is correct
38 Correct 19 ms 11264 KB Output is correct
39 Correct 20 ms 11264 KB Output is correct
40 Correct 20 ms 11264 KB Output is correct
41 Correct 11 ms 11008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2092 ms 56740 KB Output is correct
2 Correct 1968 ms 54836 KB Output is correct
3 Correct 1966 ms 55004 KB Output is correct
4 Correct 1900 ms 54136 KB Output is correct
5 Correct 2048 ms 56588 KB Output is correct
6 Correct 2025 ms 56056 KB Output is correct
7 Correct 2222 ms 61148 KB Output is correct
8 Correct 2384 ms 62648 KB Output is correct
9 Correct 2119 ms 57936 KB Output is correct
10 Correct 1975 ms 57136 KB Output is correct
11 Correct 1991 ms 56696 KB Output is correct
12 Correct 2132 ms 59256 KB Output is correct
13 Correct 2120 ms 59692 KB Output is correct
14 Correct 11 ms 11008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 11136 KB Output is correct
2 Correct 11 ms 11136 KB Output is correct
3 Correct 11 ms 11136 KB Output is correct
4 Correct 12 ms 11108 KB Output is correct
5 Correct 12 ms 11136 KB Output is correct
6 Correct 11 ms 11136 KB Output is correct
7 Correct 10 ms 11136 KB Output is correct
8 Correct 11 ms 11136 KB Output is correct
9 Correct 11 ms 11180 KB Output is correct
10 Correct 11 ms 11136 KB Output is correct
11 Correct 19 ms 11264 KB Output is correct
12 Correct 20 ms 11384 KB Output is correct
13 Correct 19 ms 11264 KB Output is correct
14 Correct 19 ms 11264 KB Output is correct
15 Correct 19 ms 11264 KB Output is correct
16 Correct 18 ms 11264 KB Output is correct
17 Correct 19 ms 11264 KB Output is correct
18 Correct 20 ms 11264 KB Output is correct
19 Correct 19 ms 11264 KB Output is correct
20 Correct 20 ms 11264 KB Output is correct
21 Correct 19 ms 11264 KB Output is correct
22 Correct 19 ms 11264 KB Output is correct
23 Correct 20 ms 11264 KB Output is correct
24 Correct 19 ms 11264 KB Output is correct
25 Correct 21 ms 11264 KB Output is correct
26 Correct 22 ms 11264 KB Output is correct
27 Correct 21 ms 11264 KB Output is correct
28 Correct 31 ms 11264 KB Output is correct
29 Correct 20 ms 11264 KB Output is correct
30 Correct 21 ms 11264 KB Output is correct
31 Correct 20 ms 11264 KB Output is correct
32 Correct 19 ms 11264 KB Output is correct
33 Correct 19 ms 11264 KB Output is correct
34 Correct 20 ms 11264 KB Output is correct
35 Correct 20 ms 11264 KB Output is correct
36 Correct 20 ms 11236 KB Output is correct
37 Correct 19 ms 11264 KB Output is correct
38 Correct 19 ms 11264 KB Output is correct
39 Correct 20 ms 11264 KB Output is correct
40 Correct 20 ms 11264 KB Output is correct
41 Correct 11 ms 11008 KB Output is correct
42 Correct 2092 ms 56740 KB Output is correct
43 Correct 1968 ms 54836 KB Output is correct
44 Correct 1966 ms 55004 KB Output is correct
45 Correct 1900 ms 54136 KB Output is correct
46 Correct 2048 ms 56588 KB Output is correct
47 Correct 2025 ms 56056 KB Output is correct
48 Correct 2222 ms 61148 KB Output is correct
49 Correct 2384 ms 62648 KB Output is correct
50 Correct 2119 ms 57936 KB Output is correct
51 Correct 1975 ms 57136 KB Output is correct
52 Correct 1991 ms 56696 KB Output is correct
53 Correct 2132 ms 59256 KB Output is correct
54 Correct 2120 ms 59692 KB Output is correct
55 Correct 11 ms 11008 KB Output is correct
56 Execution timed out 3058 ms 52188 KB Time limit exceeded
57 Halted 0 ms 0 KB -