Submission #497603

# Submission time Handle Problem Language Result Execution time Memory
497603 2021-12-23T10:35:39 Z maomao90 New Home (APIO18_new_home) C++17
0 / 100
327 ms 73044 KB
// When they saw the star, they were overjoyed. On coming to the house,
// they saw the child with his mother Mary, and they bowed down and
// worhipped him. Then they opened their treasures and presented him with 
// gifts of gold, frankincense and myrrh.
// Matthew 2:10-11
#include <bits/stdc++.h> 
using namespace std;

template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;

#ifdef DEBUG
#define debug(args...) printf(args)
#else
#define debug(args...)
#endif

#define INF 1500000005
#define LINF 1000000000000000005
#define MOD 1000000007
#define MAXN 400005

struct store {
	int x, t, a, b;
	bool operator< (const store& o) const {
		return x < o.x;
	}
};
struct qry {
	int l, y, id;
	bool operator< (const qry& o) const {
		return l < o.l;
	}
};
struct ev {
	int t, x, v;
	bool operator< (const ev& o) const {
		return t < o.t;
	}
};
struct ray {
	int x; 
	mutable int h, t;
	bool operator< (const ray& o) const {
		return x < o.x;
	}
	bool operator> (const ray& o) const {
		return x > o.x;
	}
};
struct upd {
	int x, h, s, e;
	bool operator< (const upd& o) const {
		return x < o.x;
	}
};

int n, k, q;
store stores[MAXN];
qry qrys[MAXN];
vector<ev> evs[MAXN];
int cnt[MAXN];
vii dis;
int m;
int ans[MAXN];

vector<upd> zigu, zagu;
void insert(bool b, ray r, int s, int e) {
	if (s > e) return;
	debug("%d %d %d: %d %d\n", b, s, e, r.x, r.h);
	if (!b) {
		zigu.pb((upd) {r.x, r.h, s, e});
	} else {
		zagu.pb((upd) {r.x, r.h, s, e});
	}
}
void dnc(int s, int e, vector<qry> &qrys,
	   	vector<upd> &zigu, vector<upd> &zagu) {
	if (qrys.empty() || zigu.empty()) {
		return;
	}
	debug("%d %d\n", s, e);
	int m = s + e >> 1;
	vector<upd> lzagu, lzigu, rzagu, rzigu, nzagu, nzigu;
	for (upd u : zagu) {
		if (u.s <= s && e <= u.e) {
			debug(" %d %d: %d %d\n", u.s, u.e, u.x, u.h);
			nzagu.pb(u);
			continue;
		}
		if (u.s <= m) {
			lzagu.pb(u);
		}
		if (u.e > m) {
			rzagu.pb(u);
		}
	}
	for (upd u : zigu) {
		if (u.s <= s && e <= u.e) {
			nzigu.pb(u);
			continue;
		}
		if (u.s <= m) {
			lzigu.pb(u);
		}
		if (u.e > m) {
			rzigu.pb(u);
		}
	}
	int ptr = 0;
	for (qry q : qrys) {
		while (ptr < nzagu.size() && nzagu[ptr].x - nzagu[ptr].h < q.l) {
			ptr++;
		}
		if (ptr < nzagu.size()) {
			mxto(ans[q.id], q.l - nzagu[ptr].x);
			debug(" %d: %d\n", q.id, ans[q.id]);
		}
	}
	ptr = 0;
	reverse(ALL(qrys));
	for (qry q : qrys) {
		while (ptr < nzigu.size() && nzigu[ptr].x - nzigu[ptr].h > q.l) {
			ptr++;
		}
		if (ptr < nzigu.size()) {
			mxto(ans[q.id], nzigu[ptr].x - q.l);
			debug(" %d: %d\n", q.id, ans[q.id]);
		}
	}
	reverse(ALL(qrys));
	vector<qry> lqrys, rqrys;
	for (qry q : qrys) {
		if (q.y <= m) {
			lqrys.pb(q);
		} else {
			rqrys.pb(q);
		}
	}
	dnc(s, m, lqrys, lzigu, lzagu);
	dnc(m + 1, e, rqrys, rzigu, rzagu);
}

int main() {
	scanf("%d%d%d", &n, &k, &q);
	REP (i, 0, n) {
		int x, t, a, b; scanf("%d%d%d%d", &x, &t, &a, &b);
		x *= 2;
		stores[i] = (store) {x, t, a, b};
		dis.pb(MP(a, i));
		dis.pb(MP(b, i + n));
	}
	REP (i, 0, q) {
		int l, y; scanf("%d%d", &l, &y);
		l *= 2;
		qrys[i] = (qry) {l, y, i};
		dis.pb(MP(y, i + 2 * n));
	}
	sort(ALL(dis));
	int prv = -1;
	for (auto [a, b] : dis) {
		if (a != prv) {
			m++;
			prv = a;
		}
		if (b < n) {
			stores[b].a = m - 1;
		} else if (b < 2 * n) {
			stores[b - n].b = m - 1;
		} else {
			qrys[b - 2 * n].y = m - 1;
		}
	}
	REP (i, 0, n) {
		debug("%d %d %d %d\n", stores[i].x, stores[i].t, stores[i].a, stores[i].b);
	}
	REP (i, 0, q) {
		debug("%d %d\n", qrys[i].l, qrys[i].y);
	}
	REP (i, 0, n) {
		evs[stores[i].t].pb((ev) {stores[i].a, stores[i].x, 1});
		evs[stores[i].t].pb((ev) {stores[i].b + 1, stores[i].x, -1});
	}
	REP (i, 1, k + 1) {
		evs[i].pb((ev) {0, INF, 1});
		evs[i].pb((ev) {0, -INF, 1});
		sort(ALL(evs[i]));
		{
			set<ray> rays;
			for (ev e : evs[i]) {
				//debug(" %d %d %d\n", e.t, e.x, e.v);
				if (e.v == 1) {
					if (e.x != INF && e.x != -INF && cnt[e.x]++ > 0) {
						continue;
					}
					auto it = rays.upper_bound((ray) {e.x, INF, INF});
					if (it != rays.end()) {
						insert(0, *it, it -> t, e.t - 1);
						it -> t = e.t;
						int isect = it -> x - e.x >> 1;
						it -> h = isect;
					}
					if (it == rays.begin()) {
						rays.insert((ray) {e.x, INF, e.t});
					} else {
						it = prev(it);
						assert(it -> x != e.x);
						int isect = e.x - it -> x >> 1;
						rays.insert((ray) {e.x, isect, e.t});
					}
				} else {
					if (--cnt[e.x] > 0) {
						continue;
					}
					auto it = rays.lower_bound((ray) {e.x, -1, -1});
					assert(it -> x == e.x);
					insert(0, *it, it -> t, e.t - 1);
					if (next(it) != rays.end()) {
						auto nxt = next(it);
						insert(0, *nxt, nxt -> t, e.t - 1);
						nxt -> t = e.t;
						nxt -> h += it -> h;
						mnto(nxt -> h, INF);
					}
					rays.erase(it);
				}
			}
			while (!rays.empty()) {
				auto it = rays.begin();
				insert(0, *it, it -> t, m);
				rays.erase(it);
			}
			for (ev e : evs[i]) {
				if (e.x != INF && e.x != -INF) {
					cnt[e.x] = 0;
				}
			}
		}
		{
			set<ray, greater<ray>> rays;
			for (ev e : evs[i]) {
				//debug(" %d %d %d\n", e.t, e.x, e.v);
				if (e.v == 1) {
					if (e.x != INF && e.x != -INF && cnt[e.x]++ > 0) {
						continue;
					}
					auto it = rays.upper_bound((ray) {e.x, INF, INF});
					if (it != rays.end()) {
						insert(1, *it, it -> t, e.t - 1);
						it -> t = e.t;
						int isect = it -> x - e.x >> 1;
						it -> h = isect;
					}
					if (it == rays.begin()) {
						rays.insert((ray) {e.x, INF, e.t});
					} else {
						it = prev(it);
						assert(it -> x != e.x);
						int isect = e.x - it -> x >> 1;
						rays.insert((ray) {e.x, isect, e.t});
					}
				} else {
					if (--cnt[e.x] > 0) {
						continue;
					}
					auto it = rays.lower_bound((ray) {e.x, -1, -1});
					assert(it -> x == e.x);
					insert(1, *it, it -> t, e.t - 1);
					if (next(it) != rays.end()) {
						auto nxt = next(it);
						insert(1, *nxt, nxt -> t, e.t - 1);
						nxt -> t = e.t;
						nxt -> h += it -> h;
						mnto(nxt -> h, INF);
					}
					rays.erase(it);
				}
			}
			while (!rays.empty()) {
				auto it = rays.begin();
				insert(1, *it, it -> t, m);
				rays.erase(it);
			}
			for (ev e : evs[i]) {
				if (e.x != INF && e.x != -INF) {
					cnt[e.x] = 0;
				}
			}
		}
	}
	sort(ALL(zigu));
	reverse(ALL(zigu));
	sort(ALL(zagu));
	sort(qrys, qrys + q);
	vector<qry> tmp;
	REP (i, 0, q) {
		tmp.pb(qrys[i]);
	}
	dnc(0, m - 1, tmp, zigu, zagu);
	REP (i, 0, q) {
		ans[i] /= 2;
		if (ans[i] > 1e8) {
			ans[i] = -1;
		}
		printf("%d\n", ans[i]);
	}
}

/*
4 2 4
3 1 1 10
9 2 2 4
7 2 5 7
4 1 8 10
5 3
5 6
5 9
1 10
*/

Compilation message

new_home.cpp: In function 'void dnc(int, int, std::vector<qry>&, std::vector<upd>&, std::vector<upd>&)':
new_home.cpp:102:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  102 |  int m = s + e >> 1;
      |          ~~^~~
new_home.cpp:131:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<upd>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |   while (ptr < nzagu.size() && nzagu[ptr].x - nzagu[ptr].h < q.l) {
      |          ~~~~^~~~~~~~~~~~~~
new_home.cpp:134:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<upd>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  134 |   if (ptr < nzagu.size()) {
      |       ~~~~^~~~~~~~~~~~~~
new_home.cpp:142:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<upd>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  142 |   while (ptr < nzigu.size() && nzigu[ptr].x - nzigu[ptr].h > q.l) {
      |          ~~~~^~~~~~~~~~~~~~
new_home.cpp:145:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<upd>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |   if (ptr < nzigu.size()) {
      |       ~~~~^~~~~~~~~~~~~~
new_home.cpp: In function 'int main()':
new_home.cpp:219:27: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
  219 |       int isect = it -> x - e.x >> 1;
      |                   ~~~~~~~~^~~~~
new_home.cpp:227:23: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
  227 |       int isect = e.x - it -> x >> 1;
      |                   ~~~~^~~~~~~~~
new_home.cpp:270:27: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
  270 |       int isect = it -> x - e.x >> 1;
      |                   ~~~~~~~~^~~~~
new_home.cpp:278:23: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
  278 |       int isect = e.x - it -> x >> 1;
      |                   ~~~~^~~~~~~~~
new_home.cpp:164:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  164 |  scanf("%d%d%d", &n, &k, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:166:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  166 |   int x, t, a, b; scanf("%d%d%d%d", &x, &t, &a, &b);
      |                   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:173:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  173 |   int l, y; scanf("%d%d", &l, &y);
      |             ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Runtime error 13 ms 19420 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Runtime error 13 ms 19420 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 305 ms 73044 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 327 ms 71264 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Runtime error 13 ms 19420 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Runtime error 13 ms 19420 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -