답안 #248783

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
248783 2020-07-13T11:44:03 Z user202729 새 집 (APIO18_new_home) C++17
100 / 100
1374 ms 127644 KB
#ifndef LOCAL
#define NDEBUG 1
#endif

#include<cassert>
#include<cstdio>
#include<vector>
#include<cstdint>
#include<climits>
#include<array>
#include<algorithm>
#include<map>
struct Input{
	int get(){return std::getchar();}

	/*
	auto& operator>>(std::string& it){
		it.clear();
		char ch;
		do{
			while((ch=(char)get())>' ') it+=ch;
		}while(it.empty());
		return *this;
	}
	*/

	auto& ignore(std::size_t number){
		while(number--) get();
		return *this;
	}

	auto& operator>>(char& it){
		while((it=(char)get())<=' ') {}
		return *this;
	}

	template<class T, class=std::enable_if<std::is_integral<T>::value>>
	auto& operator>>(T& it){
		while((it=get())<'0') {}
		it-='0';
		int ch; while((ch=get())>='0') it=it*10+ch-'0';
		return *this;
	}
} cin;
struct Output{
	auto& operator<<(char it) {
		std::putchar(it); return *this;
	}
	auto& operator<<(int it) {
		std::printf("%d", it); return *this;
	}
	auto& operator<<(char const* it) {
		for(; *it; ++it) std::putchar(*it);
		return *this;
	}
} cout;

struct Tree{
	std::vector<int> data;
	void reset(int number, int value){
		data.assign(number*2, value);
	}
	void changeMaximum(int left, int right, int value){
		left+=int(data.size()/2);
		right+=int(data.size()/2);
		while(left<right){
			if(left&1) { data[left]=std::max(data[left], value);++left;}
			if(right&1){ --right; data[right]=std::max(data[right], value);}
			left>>=1; right>>=1;
		}
	}
	int get(int index){
		index+=int(data.size()/2);
		int result=INT_MIN;
		for(; index; index>>=1)
			result=std::max(result, data[index]);
		return result;
	}
};

int main(){
	int numStore, numType, numQuery; cin>>numStore>>numType>>numQuery;
	struct Event{
		int position, time, type;
		bool open;
		//bool operator<(Event other) const{return time<other.time;}
	};
	std::vector<Event> events; events.reserve(numStore*2);
	for(int _=0; _<numStore; ++_){ // event before query if same time
		int position, type, open, close; cin>>position>>type>>open>>close;
		--type; assert(0<=type); assert(type<numType);
		events.push_back({position, open, type, true});
		events.push_back({position, close+1, type, false});
	}
	assert((int)events.size()==numStore*2);
	std::sort(begin(events), end(events),[&](auto first, auto sec){return first.time<sec.time;});

	struct Query{
		int position, time, index;
		bool operator<(Query other) const{return position<other.position;}
	};
	std::vector<Query> queries(numQuery);
	int numTime;

	{ // read queries & compress time
		std::vector<int> times; times.reserve(numQuery);
		for(int index=0; index<numQuery; ++index){
			auto& [position, time, index_]=queries[index];
			index_=index;
			cin>>position>>time;
			times.push_back(time);
		}
		std::sort(begin(queries), end(queries));
		std::sort(begin(times), end(times));
		times.resize(std::unique(begin(times), end(times))-begin(times));
		numTime=(int)times.size();
		for(auto& [_, time,__]: queries) time=int(std::lower_bound(begin(times), end(times), time)-begin(times));
		for(auto& event: events) event.time=int(std::lower_bound(begin(times), end(times), event.time)-begin(times));
		times.clear();
	}

	int const INFINITY=(int)4e8; //...

	struct Event1{
		int start; // inclusive (change event before query)
		bool operator<(Event1 other) const{return start<other.start;}

		int value; // or query index
		int left, right; // half inclusive. In this problem they're time. Useless for queries
	};
	std::array<std::vector<Event1>, 2> events1; // [0]: increasing,[1]: decreasing
	auto const addSegment=[&](int last, int time, int left, int right){
		assert(left<right);
		assert(last<=time); if(last==time) return;
		//assert((cout<<last<<' '<<time<<' '<<left<<' '<<right<<'\n', true));
		int const mid=(left+right)>>1;
		events1[0].push_back({mid, -left, last, time});
		events1[1].push_back({mid+1, right, last, time});
	};

	{ // construct events1
		struct Item{
			int nextLastChange;
			int count;
		};
		std::vector<std::map<int, Item>> positions(numType);
		for(auto& it: positions){ it.insert({-INFINITY, {0, 1}}); it.insert({INFINITY, {INT_MAX, 1}}); }
		// ^ I should have figured out this trick earlier...
		for(auto [position, time, type, open]: events){
			auto& map=positions[type];
			if(open){
				auto [iterator, inserted]=map.insert({position, {time, 1}});
				if(not inserted){ iterator->second.count++; continue; }
				auto const prevIterator=std::prev(iterator);
				auto const next=std::next(iterator)->first, previous=prevIterator->first;

				addSegment(prevIterator->second.nextLastChange, time, previous, next);
				// ^ if previous==-INFINITY and next==INFINITY then tree.get will be rather large
				prevIterator->second.nextLastChange=time;
				assert(iterator->second.nextLastChange==time);
			}else{
				auto iterator=map.find(position); --iterator->second.count;
				if(iterator->second.count!=0) continue;
				auto const rightLastChange=iterator->second.nextLastChange;
				auto const nextIterator=map.erase(iterator), prevIterator=std::prev(nextIterator);
				auto const next=nextIterator->first; auto const previous=prevIterator->first;

				addSegment(prevIterator->second.nextLastChange, time, previous, position);
				addSegment(rightLastChange, time, position, next);
				prevIterator->second.nextLastChange=time;
			}
		}
		int tmp=INT_MAX;
		for(auto& it: positions){
			assert(it.size()==2);
			assert(it.begin()->first==-INFINITY);
			assert(it.rbegin()->first==INFINITY);
			tmp=std::min(tmp, it.begin()->second.nextLastChange);
		}
		
		addSegment(tmp, numTime,-INFINITY, INFINITY);
	}

	// will be processed in reverse order
	std::sort(begin(events1[0]), end(events1[0]));
	std::sort(begin(events1[1]), end(events1[1]), [&](Event1 first, Event1 sec){return sec<first;});

	std::vector<int> result(numQuery, -1); // corresponds to the current index of queries

	Tree tree;
	tree.reset(numTime, -INFINITY);
	{ // process events1[0]
		for(auto index=numQuery; index--;){
			Query query=queries[index];

			while(not events1[0].empty()){
				auto [start, value, left, right]=events1[0].back();
				if(start<query.position) break;
				tree.changeMaximum(left, right, value);
				events1[0].pop_back();
			}
			//assert(tree.get(query.time)==-INFINITY or tree.get(query.time)+query.position>=0);
			result[index]=std::max(result[index], tree.get(query.time)+query.position);
		}
	}

	tree.reset(numTime, -INFINITY);
	{ // process events1[1]
		for(int index=0; index<numQuery; ++index){
			Query query=queries[index];

			while(not events1[1].empty()){
				auto [start, value, left, right]=events1[1].back();
				if(start>query.position) break;
				tree.changeMaximum(left, right, value);
				events1[1].pop_back();
			}
			//assert(tree.get(query.time)==-INFINITY or tree.get(query.time)-query.position>=0);
			result[index]=std::max(result[index], tree.get(query.time)-query.position);
		}
	}

	std::vector<int> result2(numQuery);
	for(int index=0; index<numQuery; ++index){
		result2[queries[index].index]=result[index];
	}
	for(auto cur: result2){
		assert(cur!=-1); if(cur>=INFINITY/2) cur=-1;
		cout<<cur<<'\n';
	}
}

Compilation message

new_home.cpp: In function 'int main()':
new_home.cpp:108:33: warning: variable 'index_' set but not used [-Wunused-but-set-variable]
    auto& [position, time, index_]=queries[index];
                                 ^
new_home.cpp:117:24: warning: unused variable '_' [-Wunused-variable]
   for(auto& [_, time,__]: queries) time=int(std::lower_bound(begin(times), end(times), time)-begin(times));
                        ^
new_home.cpp:117:24: warning: unused variable '__' [-Wunused-variable]
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 1 ms 512 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 1 ms 512 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 2 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 1 ms 512 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 1 ms 512 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 2 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 214 ms 15860 KB Output is correct
32 Correct 38 ms 4492 KB Output is correct
33 Correct 195 ms 13300 KB Output is correct
34 Correct 184 ms 13428 KB Output is correct
35 Correct 199 ms 15732 KB Output is correct
36 Correct 207 ms 15732 KB Output is correct
37 Correct 167 ms 12776 KB Output is correct
38 Correct 171 ms 12648 KB Output is correct
39 Correct 147 ms 12600 KB Output is correct
40 Correct 154 ms 12524 KB Output is correct
41 Correct 149 ms 12868 KB Output is correct
42 Correct 150 ms 12776 KB Output is correct
43 Correct 47 ms 6412 KB Output is correct
44 Correct 151 ms 12912 KB Output is correct
45 Correct 141 ms 12904 KB Output is correct
46 Correct 130 ms 12344 KB Output is correct
47 Correct 115 ms 12252 KB Output is correct
48 Correct 109 ms 12148 KB Output is correct
49 Correct 123 ms 12280 KB Output is correct
50 Correct 142 ms 12664 KB Output is correct
51 Correct 122 ms 12148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 747 ms 71772 KB Output is correct
2 Correct 855 ms 56220 KB Output is correct
3 Correct 814 ms 125636 KB Output is correct
4 Correct 763 ms 78828 KB Output is correct
5 Correct 936 ms 56156 KB Output is correct
6 Correct 869 ms 56256 KB Output is correct
7 Correct 695 ms 125936 KB Output is correct
8 Correct 596 ms 75636 KB Output is correct
9 Correct 557 ms 63224 KB Output is correct
10 Correct 583 ms 57596 KB Output is correct
11 Correct 526 ms 52388 KB Output is correct
12 Correct 538 ms 53204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1087 ms 74392 KB Output is correct
2 Correct 167 ms 20448 KB Output is correct
3 Correct 1150 ms 72360 KB Output is correct
4 Correct 895 ms 125392 KB Output is correct
5 Correct 954 ms 83964 KB Output is correct
6 Correct 945 ms 90692 KB Output is correct
7 Correct 1105 ms 72264 KB Output is correct
8 Correct 1141 ms 72228 KB Output is correct
9 Correct 855 ms 125380 KB Output is correct
10 Correct 877 ms 86344 KB Output is correct
11 Correct 928 ms 75900 KB Output is correct
12 Correct 951 ms 72808 KB Output is correct
13 Correct 627 ms 71108 KB Output is correct
14 Correct 618 ms 70672 KB Output is correct
15 Correct 718 ms 71760 KB Output is correct
16 Correct 760 ms 72392 KB Output is correct
17 Correct 772 ms 71236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 1 ms 512 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 1 ms 512 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 2 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 214 ms 15860 KB Output is correct
32 Correct 38 ms 4492 KB Output is correct
33 Correct 195 ms 13300 KB Output is correct
34 Correct 184 ms 13428 KB Output is correct
35 Correct 199 ms 15732 KB Output is correct
36 Correct 207 ms 15732 KB Output is correct
37 Correct 167 ms 12776 KB Output is correct
38 Correct 171 ms 12648 KB Output is correct
39 Correct 147 ms 12600 KB Output is correct
40 Correct 154 ms 12524 KB Output is correct
41 Correct 149 ms 12868 KB Output is correct
42 Correct 150 ms 12776 KB Output is correct
43 Correct 47 ms 6412 KB Output is correct
44 Correct 151 ms 12912 KB Output is correct
45 Correct 141 ms 12904 KB Output is correct
46 Correct 130 ms 12344 KB Output is correct
47 Correct 115 ms 12252 KB Output is correct
48 Correct 109 ms 12148 KB Output is correct
49 Correct 123 ms 12280 KB Output is correct
50 Correct 142 ms 12664 KB Output is correct
51 Correct 122 ms 12148 KB Output is correct
52 Correct 202 ms 26228 KB Output is correct
53 Correct 176 ms 23924 KB Output is correct
54 Correct 181 ms 19316 KB Output is correct
55 Correct 155 ms 16888 KB Output is correct
56 Correct 158 ms 19188 KB Output is correct
57 Correct 156 ms 13940 KB Output is correct
58 Correct 157 ms 16884 KB Output is correct
59 Correct 164 ms 19312 KB Output is correct
60 Correct 154 ms 13860 KB Output is correct
61 Correct 81 ms 23540 KB Output is correct
62 Correct 183 ms 26228 KB Output is correct
63 Correct 174 ms 20468 KB Output is correct
64 Correct 172 ms 18104 KB Output is correct
65 Correct 162 ms 14068 KB Output is correct
66 Correct 154 ms 12904 KB Output is correct
67 Correct 81 ms 12148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 1 ms 512 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 1 ms 512 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 2 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 214 ms 15860 KB Output is correct
32 Correct 38 ms 4492 KB Output is correct
33 Correct 195 ms 13300 KB Output is correct
34 Correct 184 ms 13428 KB Output is correct
35 Correct 199 ms 15732 KB Output is correct
36 Correct 207 ms 15732 KB Output is correct
37 Correct 167 ms 12776 KB Output is correct
38 Correct 171 ms 12648 KB Output is correct
39 Correct 147 ms 12600 KB Output is correct
40 Correct 154 ms 12524 KB Output is correct
41 Correct 149 ms 12868 KB Output is correct
42 Correct 150 ms 12776 KB Output is correct
43 Correct 47 ms 6412 KB Output is correct
44 Correct 151 ms 12912 KB Output is correct
45 Correct 141 ms 12904 KB Output is correct
46 Correct 130 ms 12344 KB Output is correct
47 Correct 115 ms 12252 KB Output is correct
48 Correct 109 ms 12148 KB Output is correct
49 Correct 123 ms 12280 KB Output is correct
50 Correct 142 ms 12664 KB Output is correct
51 Correct 122 ms 12148 KB Output is correct
52 Correct 747 ms 71772 KB Output is correct
53 Correct 855 ms 56220 KB Output is correct
54 Correct 814 ms 125636 KB Output is correct
55 Correct 763 ms 78828 KB Output is correct
56 Correct 936 ms 56156 KB Output is correct
57 Correct 869 ms 56256 KB Output is correct
58 Correct 695 ms 125936 KB Output is correct
59 Correct 596 ms 75636 KB Output is correct
60 Correct 557 ms 63224 KB Output is correct
61 Correct 583 ms 57596 KB Output is correct
62 Correct 526 ms 52388 KB Output is correct
63 Correct 538 ms 53204 KB Output is correct
64 Correct 1087 ms 74392 KB Output is correct
65 Correct 167 ms 20448 KB Output is correct
66 Correct 1150 ms 72360 KB Output is correct
67 Correct 895 ms 125392 KB Output is correct
68 Correct 954 ms 83964 KB Output is correct
69 Correct 945 ms 90692 KB Output is correct
70 Correct 1105 ms 72264 KB Output is correct
71 Correct 1141 ms 72228 KB Output is correct
72 Correct 855 ms 125380 KB Output is correct
73 Correct 877 ms 86344 KB Output is correct
74 Correct 928 ms 75900 KB Output is correct
75 Correct 951 ms 72808 KB Output is correct
76 Correct 627 ms 71108 KB Output is correct
77 Correct 618 ms 70672 KB Output is correct
78 Correct 718 ms 71760 KB Output is correct
79 Correct 760 ms 72392 KB Output is correct
80 Correct 772 ms 71236 KB Output is correct
81 Correct 202 ms 26228 KB Output is correct
82 Correct 176 ms 23924 KB Output is correct
83 Correct 181 ms 19316 KB Output is correct
84 Correct 155 ms 16888 KB Output is correct
85 Correct 158 ms 19188 KB Output is correct
86 Correct 156 ms 13940 KB Output is correct
87 Correct 157 ms 16884 KB Output is correct
88 Correct 164 ms 19312 KB Output is correct
89 Correct 154 ms 13860 KB Output is correct
90 Correct 81 ms 23540 KB Output is correct
91 Correct 183 ms 26228 KB Output is correct
92 Correct 174 ms 20468 KB Output is correct
93 Correct 172 ms 18104 KB Output is correct
94 Correct 162 ms 14068 KB Output is correct
95 Correct 154 ms 12904 KB Output is correct
96 Correct 81 ms 12148 KB Output is correct
97 Correct 1033 ms 127644 KB Output is correct
98 Correct 188 ms 20964 KB Output is correct
99 Correct 1191 ms 64376 KB Output is correct
100 Correct 997 ms 115608 KB Output is correct
101 Correct 1153 ms 92876 KB Output is correct
102 Correct 1374 ms 76348 KB Output is correct
103 Correct 966 ms 62368 KB Output is correct
104 Correct 969 ms 61972 KB Output is correct
105 Correct 816 ms 62032 KB Output is correct
106 Correct 838 ms 62056 KB Output is correct
107 Correct 857 ms 80576 KB Output is correct
108 Correct 911 ms 92188 KB Output is correct
109 Correct 834 ms 68348 KB Output is correct
110 Correct 887 ms 80660 KB Output is correct
111 Correct 931 ms 92384 KB Output is correct
112 Correct 846 ms 64640 KB Output is correct
113 Correct 441 ms 124224 KB Output is correct
114 Correct 1040 ms 127560 KB Output is correct
115 Correct 1102 ms 98480 KB Output is correct
116 Correct 1082 ms 86664 KB Output is correct
117 Correct 1069 ms 68788 KB Output is correct
118 Correct 867 ms 63760 KB Output is correct
119 Correct 510 ms 56220 KB Output is correct
120 Correct 577 ms 55680 KB Output is correct
121 Correct 625 ms 58792 KB Output is correct
122 Correct 591 ms 56824 KB Output is correct
123 Correct 651 ms 60492 KB Output is correct
124 Correct 738 ms 62076 KB Output is correct
125 Correct 696 ms 59972 KB Output is correct
126 Correct 797 ms 62240 KB Output is correct