답안 #763201

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
763201 2023-06-22T06:36:55 Z fanwen Plahte (COCI17_plahte) C++17
160 / 160
251 ms 38048 KB
/**
 *      author : pham van sam
 *      created : 22 June 2023 (Thursday)
 **/

#include <bits/stdc++.h>

using namespace std;
using namespace chrono;

#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(it, s) for (__typeof(s.begin()) it = (s).begin(); it != (s).end(); ++it)

template <typename U, typename V> bool maximize(U &A, const V &B) { return (A < B) ? (A = B, true) : false; }
template <typename U, typename V> bool minimize(U &A, const V &B) { return (A > B) ? (A = B, true) : false; }

struct lazy_segment_tree {
	vector <int> it, lazy;

	lazy_segment_tree(int n = 0) : it(4 * n + 5), lazy(4 * n + 5, -1) {}

	void push(int idx) {
		if(lazy[idx] == -1) return;
		FOR(i, idx << 1, idx << 1 | 1) it[i] = lazy[i] = lazy[idx];
		lazy[idx] = -1; 
	}

	void update(int idx, int l, int r, int u, int v, int val) {
		if(l > v || r < u) return;
		if(l >= u && r <= v) return void(it[idx] = lazy[idx] = val);
		push(idx);
		int mid = (l + r) >> 1;
		update(idx << 1, l, mid, u, v, val);
		update(idx << 1 | 1, mid + 1, r, u, v, val);
		it[idx] = max(it[idx << 1], it[idx << 1 | 1]);
	}

	int get(int idx, int l, int r, int u, int v) {
		if(l > v || r < u) return 0;
		if(l >= u && r <= v) return it[idx];
		push(idx);
		int mid = (l + r) >> 1;
		return max(get(idx << 1, l, mid, u, v), get(idx << 1 | 1, mid + 1, r, u, v));
	}
};

struct element {
	int x, l, r, type, id;
	element(int x = 0, int l = 0, int r = 0, int type = 0, int id = 0) : x(x), l(l), r(r), type(type), id(id) {}

	bool operator < (const element &other) const {
		return (x < other.x || (x == other.x && type > other.type));
	}
};

const int MAXN = 1e5 + 5;

int N, M, par[MAXN], ans[MAXN];
set <int> s[MAXN];
vector <int> adj[MAXN];

void DFS(int u) {
	for (auto v : adj[u]) {
		DFS(v);
		if((int) s[u].size() < (int) s[v].size()) s[u].swap(s[v]);
		for (auto it : s[v]) s[u].insert(it);
	}
	ans[u] = (int) s[u].size();
}

void process(void) {
	cin >> N >> M;
	vector <element> events;
	vector <int> compress;
	FOR(i, 1, N) {
		int a, b, c, d; cin >> a >> b >> c >> d;
		events.push_back(element(a, b, d, 1, i));
		events.push_back(element(c, b, d, -1, i));
		compress.push_back(b);
		compress.push_back(d);
	}
	FOR(i, 1, M) {
		int x, y, k; cin >> x >> y >> k;
		events.push_back(element(x, y, y, 0, k));
		compress.push_back(y);
	}
	sort(ALL(events));
	sort(ALL(compress)); compress.erase(unique(ALL(compress)), compress.end());
	M = (int) compress.size();
	lazy_segment_tree it(M);
	for (auto &[_, l, r, t, c] : events) {
		// cout << _ << " " << l << " " << r << " " << t << " " << c << '\n';
		l = lower_bound(ALL(compress), l) - compress.begin() + 1;
		r = lower_bound(ALL(compress), r) - compress.begin() + 1;
		if(t == 1) {
			par[c] = it.get(1, 1, M, l, r);
			it.update(1, 1, M, l, r, c);
			adj[par[c]].push_back(c);
			// cout << par[c] << " " << c << '\n';
		} else if(t == -1) {
			it.update(1, 1, M, l, r, par[c]);
		} else {
			int id = it.get(1, 1, M, l, r);
			s[id].insert(c);
			// cout << id << " " << c << '\n';
			// cout << l << " " << r << '\n';
		}
	}
	DFS(0);
	FOR(i, 1, N) cout << ans[i] << '\n';
}

signed main() {

    #define TASK "plahte"
    if(fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }

    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    auto start_time = steady_clock::now();
    int test = 1;
    // cin >> test;
    for (int i = 1; i <= test; ++i) {
        process();
        // cout << '\n';
    }

    auto end_time = steady_clock::now();

    cerr << "\nExecution time : " << duration_cast<milliseconds> (end_time - start_time).count() << "[ms]" << endl;

    return (0 ^ 0);
}

Compilation message

plahte.cpp: In function 'int main()':
plahte.cpp:122:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
plahte.cpp:123:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  123 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 15928 KB Output is correct
2 Correct 73 ms 16580 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 80 ms 18108 KB Output is correct
2 Correct 89 ms 17332 KB Output is correct
3 Correct 5 ms 7376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 144 ms 26632 KB Output is correct
2 Correct 145 ms 23068 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 250 ms 38048 KB Output is correct
2 Correct 251 ms 33624 KB Output is correct
3 Correct 3 ms 7376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 236 ms 36140 KB Output is correct
2 Correct 230 ms 32104 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct