Beispiel #1
0
	void solve2() {
		typedef set<int, cmp_height> set_t;
		static vector<Event> event;
		static set_t box;
		static stack<int> stk;
		static vector<int> seq;
		static int dgr[N_MAX];
		static vector<int> adj[N_MAX];

		for ( int i = 0; i < n_stuff; i ++ ) {
			event.push_back(Event(stuff[i].x1, i, ENTER));
			event.push_back(Event(stuff[i].x2, i, LEAVE));
		}
		sort(event.begin(), event.end(), cmp_event);
		for ( vector<Event>::iterator ev = event.begin();
				ev != event.end(); ev ++ ) {
			if ( ev->typ == ENTER ) {
				pair<set_t::iterator, bool> res = box.insert(ev->id);
				set_t::iterator it = res.first;
				it ++;
				if ( it != box.end() ) {
					dgr[*it] ++;
					adj[ev->id].push_back(*it);
					//fprintf(stderr, "%d->%d\n", ev->id, *it);
				}
				it --;
				if ( it != box.begin() ) {
					it --;		
					dgr[ev->id] ++;
					adj[*it].push_back(ev->id);
					//fprintf(stderr, "%d->%d\n", *it, ev->id);
				}
			} else 
				box.erase(box.lower_bound(ev->id));
		}

		for ( int i = 0; i < n_stuff; i ++ )
			if ( dgr[i] == 0 )
				stk.push(i);
		while ( !stk.empty() ) {
			int u = stk.top(); stk.pop();
			seq.push_back(u);
			for ( vector<int>::iterator it = adj[u].begin();
					it != adj[u].end(); it ++ ) {
				int v = *it;
				dgr[v] --;
				if ( dgr[v] == 0 )
					stk.push(v);
			}
		}

		assert((int)seq.size() == n_stuff);
		for ( int i = 0; i < n_stuff; i ++ )
			printf("%d %d\n", seq[i] + 1, 3);
	}
Beispiel #2
0
	void build_graph() {
		for ( int i = 0; i < n_stuff; i ++ ) {
			event.push_back(Event(stuff[i].x1, i, ENTER));
			event.push_back(Event(stuff[i].x2, i, LEAVE));
		}
		sort(event.begin(), event.end(), cmp_event);
		axis = 0;
		for ( int i = 0; i < 2 * n_stuff; i ++ ) {
			int st = i;
			while ( i + 1 < 2 * n_stuff &&
					event[i + 1].x == event[i].x )
				i ++;
			for ( int k = st; k <= i; k ++ ) {
				int t = event[k].id;
				if ( event[k].typ == ENTER )
					stuff[t].l = axis;
				else
					stuff[t].r = axis;
			}
			axis ++;
		}

		for ( vector<Event>::iterator ev = event.begin();
				ev != event.end(); ev ++ ) {
			if ( ev->typ == ENTER ) {
				pair<set_t::iterator, bool> res = box.insert(ev->id);
				set_t::iterator it = res.first;
				it ++;
				if ( it != box.end() ) {
					dgr[*it] ++;
					adj[ev->id].push_back(*it);
				}
				it --;
				if ( it != box.begin() ) {
					it --;		
					dgr[ev->id] ++;
					adj[*it].push_back(ev->id);
				}
			} else {
				set_t::iterator it = box.lower_bound(ev->id);
				box.erase(it);
			}
		}
	}