#include <bits/stdc++.h> #include <unistd.h> using namespace std; class Input { public: Input() { bufpos = bufend = buffer; eof = false; } bool Eof() { return eof; } char Peek() { if(bufpos == bufend) Grab(); return *bufpos; } unsigned char UPeek() { return static_cast<unsigned char>(Peek()); } void SkipWS(); template<class T> T Get(); void operator()() {} template<class Arg, class... Args> void operator()(Arg &arg, Args &... args) { arg = Get<Arg>(); operator()(args...); } private: static const int BUFSIZE = 1<<16; char buffer[BUFSIZE]; char *bufpos; char *bufend; bool eof; void Grab(); }; void Input::Grab() { if(eof) return; bufpos = buffer; bufend = buffer + read(0, buffer, BUFSIZE); if(bufend==bufpos) { eof=true; *bufpos=0; } } template<> inline char Input::Get<char>() { char res = Peek(); ++bufpos; return res; } void Input::SkipWS() { while(isspace(UPeek())) Get<char>(); } template<> unsigned Input::Get<unsigned>() { SkipWS(); unsigned x = 0; while(isdigit(UPeek())) { x = 10u * x + (Get<char>()-'0'); } return x; } template<> int Input::Get<int>() { SkipWS(); bool neg = false; if(Peek()=='-') { neg=true; Get<char>(); } unsigned x = Get<unsigned>(); if (neg) x = -x; return static_cast<int>(x); } class Output { public: void Flush(); Output():bufpos(buffer),BUFLIMIT(buffer+BUFSIZE-100) {} ~Output() { Flush(); } void Put(char c); void Put(unsigned x); void Put(int x); void Put(const char*s); void operator()() {} template<class Arg, class... Args> void operator()(const Arg &arg, const Args &... args) { Put(arg); operator()(args...); } private: static const int BUFSIZE = 1<<16; char buffer[BUFSIZE]; char *bufpos; char *BUFLIMIT; }; void Output::Flush() { char *p = buffer; while(p < bufpos) { p += write(1, p, bufpos-p); } bufpos = buffer; } inline void Output::Put(char c) { *bufpos = c; ++bufpos; if(bufpos >= BUFLIMIT) Flush(); } void Output::Put(unsigned x) { char *old = bufpos; do { *bufpos = char('0' + x % 10u); x /= 10u; ++bufpos; } while(x); reverse(old, bufpos); if(bufpos >= BUFLIMIT) Flush(); } void Output::Put(int x) { if(x<0) { Put('-'); Put(-static_cast<unsigned>(x)); } else { Put(static_cast<unsigned>(x)); } } void Output::Put(const char*s) { while(*s) Put(*s++); } Input IN; Output OUT; // --- struct Request { int a, b; int p; int res; int id; }; struct Range { int a,b; }; struct Set { vector<Range> ranges; }; int numP; vector<Request> requests; Set global; vector<Set> local; void ReadInput() { int n,k; IN(n,k); mt19937 rng; requests.resize(n); int nextId=0; for(Request &r : requests) { IN(r.a, r.b, r.p); r.id = nextId++; } shuffle(requests.begin(), requests.end(), rng); } void ReduceP() { vector<int> uniqueP; uniqueP.reserve(requests.size()); for (const Request &r : requests) { uniqueP.push_back(r.p); } sort(uniqueP.begin(), uniqueP.end()); numP = unique(uniqueP.begin(), uniqueP.end()) - uniqueP.begin(); uniqueP.resize(numP); for (Request &r : requests) { r.p = lower_bound(uniqueP.begin(), uniqueP.end(), r.p) - uniqueP.begin(); } } struct CmpEndpoint { bool operator()(const Request &a, const Request &b) const { return a.b < b.b; } }; struct CmpPLeft { bool operator()(const Request &a, const Request &b) const { if(a.p != b.p) return a.p<b.p; return a.a < b.a; } }; int FindLastGlobalNotLocal(const Set &ss) { auto itG = global.ranges.end(); auto itL = ss.ranges.end(); while(itG != global.ranges.begin() && itL != ss.ranges.begin()) { --itG; --itL; if(itG->b > itL->b) return itG->b; if(itG->a < itL->a) return itL->a - 1; } if(itG == global.ranges.begin()) return -1; --itG; return itG->b; } int FindLastUnusedLe(const Set &ss, int limit) { if(ss.ranges.empty()) return limit; if(ss.ranges.back().b < limit) return limit; return ss.ranges.back().a - 1; } void Add(Set &ss, int x) { int n = ss.ranges.size(); int i = n; while(i>0 && ss.ranges[i-1].a > x) --i; if (i < n && ss.ranges[i].a == x+1) { ss.ranges[i].a = x; if (i>0 && ss.ranges[i-1].b == x-1) { ss.ranges[i-1].b = ss.ranges[i].b; ss.ranges.erase(ss.ranges.begin() + i); } } else if (i > 0 && ss.ranges[i-1].a == x-1) { ss.ranges[i-1].a = x; } else { Range r; r.a = x; r.b=x; ss.ranges.insert(ss.ranges.begin() + i, r); } } int Size(const Set &ss) { int res = 0; for (const Range &r : ss.ranges) { res += r.b - r.a + 1; } return res; } vector<int> Expand(const Set &ss) { vector<int> res; res.reserve(Size(ss)); for (const Range &r : ss.ranges) { for(int i=r.a; i<=r.b; ++i) res.push_back(i); } return res; } void ProcessRequest(const Request &r) { int t = FindLastGlobalNotLocal(local[r.p]); if(t >= r.a) { Add(local[r.p], t); return; } int t2 = FindLastUnusedLe(local[r.p], r.b); if(t2>t) Add(global, t2); Add(local[r.p], t2); } void Solve() { local.resize(numP); sort(requests.begin(), requests.end(), CmpEndpoint()); for (const Request &r : requests) { ProcessRequest(r); } } void Verify() { sort(requests.begin(), requests.end(), CmpPLeft()); int p = -1; vector<int> selected; int next = 0; for(Request &r : requests) { if(r.p != p) { p = r.p; selected = Expand(local[p]); next = 0; } if(selected[next] < r.a || selected[next] > r.b) throw 0; r.res = selected[next]; ++next; } } void Print() { vector<int> sol(requests.size()); for (const Request &r : requests) { sol[r.id] = r.res; } int total = Size(global); OUT(total, '\n'); for(int s : sol) OUT(s, '\n'); } int main() { ReadInput(); ReduceP(); try { Solve(); Verify(); Print(); } catch(int) { OUT("NIE\n"); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | #include <bits/stdc++.h> #include <unistd.h> using namespace std; class Input { public: Input() { bufpos = bufend = buffer; eof = false; } bool Eof() { return eof; } char Peek() { if(bufpos == bufend) Grab(); return *bufpos; } unsigned char UPeek() { return static_cast<unsigned char>(Peek()); } void SkipWS(); template<class T> T Get(); void operator()() {} template<class Arg, class... Args> void operator()(Arg &arg, Args &... args) { arg = Get<Arg>(); operator()(args...); } private: static const int BUFSIZE = 1<<16; char buffer[BUFSIZE]; char *bufpos; char *bufend; bool eof; void Grab(); }; void Input::Grab() { if(eof) return; bufpos = buffer; bufend = buffer + read(0, buffer, BUFSIZE); if(bufend==bufpos) { eof=true; *bufpos=0; } } template<> inline char Input::Get<char>() { char res = Peek(); ++bufpos; return res; } void Input::SkipWS() { while(isspace(UPeek())) Get<char>(); } template<> unsigned Input::Get<unsigned>() { SkipWS(); unsigned x = 0; while(isdigit(UPeek())) { x = 10u * x + (Get<char>()-'0'); } return x; } template<> int Input::Get<int>() { SkipWS(); bool neg = false; if(Peek()=='-') { neg=true; Get<char>(); } unsigned x = Get<unsigned>(); if (neg) x = -x; return static_cast<int>(x); } class Output { public: void Flush(); Output():bufpos(buffer),BUFLIMIT(buffer+BUFSIZE-100) {} ~Output() { Flush(); } void Put(char c); void Put(unsigned x); void Put(int x); void Put(const char*s); void operator()() {} template<class Arg, class... Args> void operator()(const Arg &arg, const Args &... args) { Put(arg); operator()(args...); } private: static const int BUFSIZE = 1<<16; char buffer[BUFSIZE]; char *bufpos; char *BUFLIMIT; }; void Output::Flush() { char *p = buffer; while(p < bufpos) { p += write(1, p, bufpos-p); } bufpos = buffer; } inline void Output::Put(char c) { *bufpos = c; ++bufpos; if(bufpos >= BUFLIMIT) Flush(); } void Output::Put(unsigned x) { char *old = bufpos; do { *bufpos = char('0' + x % 10u); x /= 10u; ++bufpos; } while(x); reverse(old, bufpos); if(bufpos >= BUFLIMIT) Flush(); } void Output::Put(int x) { if(x<0) { Put('-'); Put(-static_cast<unsigned>(x)); } else { Put(static_cast<unsigned>(x)); } } void Output::Put(const char*s) { while(*s) Put(*s++); } Input IN; Output OUT; // --- struct Request { int a, b; int p; int res; int id; }; struct Range { int a,b; }; struct Set { vector<Range> ranges; }; int numP; vector<Request> requests; Set global; vector<Set> local; void ReadInput() { int n,k; IN(n,k); mt19937 rng; requests.resize(n); int nextId=0; for(Request &r : requests) { IN(r.a, r.b, r.p); r.id = nextId++; } shuffle(requests.begin(), requests.end(), rng); } void ReduceP() { vector<int> uniqueP; uniqueP.reserve(requests.size()); for (const Request &r : requests) { uniqueP.push_back(r.p); } sort(uniqueP.begin(), uniqueP.end()); numP = unique(uniqueP.begin(), uniqueP.end()) - uniqueP.begin(); uniqueP.resize(numP); for (Request &r : requests) { r.p = lower_bound(uniqueP.begin(), uniqueP.end(), r.p) - uniqueP.begin(); } } struct CmpEndpoint { bool operator()(const Request &a, const Request &b) const { return a.b < b.b; } }; struct CmpPLeft { bool operator()(const Request &a, const Request &b) const { if(a.p != b.p) return a.p<b.p; return a.a < b.a; } }; int FindLastGlobalNotLocal(const Set &ss) { auto itG = global.ranges.end(); auto itL = ss.ranges.end(); while(itG != global.ranges.begin() && itL != ss.ranges.begin()) { --itG; --itL; if(itG->b > itL->b) return itG->b; if(itG->a < itL->a) return itL->a - 1; } if(itG == global.ranges.begin()) return -1; --itG; return itG->b; } int FindLastUnusedLe(const Set &ss, int limit) { if(ss.ranges.empty()) return limit; if(ss.ranges.back().b < limit) return limit; return ss.ranges.back().a - 1; } void Add(Set &ss, int x) { int n = ss.ranges.size(); int i = n; while(i>0 && ss.ranges[i-1].a > x) --i; if (i < n && ss.ranges[i].a == x+1) { ss.ranges[i].a = x; if (i>0 && ss.ranges[i-1].b == x-1) { ss.ranges[i-1].b = ss.ranges[i].b; ss.ranges.erase(ss.ranges.begin() + i); } } else if (i > 0 && ss.ranges[i-1].a == x-1) { ss.ranges[i-1].a = x; } else { Range r; r.a = x; r.b=x; ss.ranges.insert(ss.ranges.begin() + i, r); } } int Size(const Set &ss) { int res = 0; for (const Range &r : ss.ranges) { res += r.b - r.a + 1; } return res; } vector<int> Expand(const Set &ss) { vector<int> res; res.reserve(Size(ss)); for (const Range &r : ss.ranges) { for(int i=r.a; i<=r.b; ++i) res.push_back(i); } return res; } void ProcessRequest(const Request &r) { int t = FindLastGlobalNotLocal(local[r.p]); if(t >= r.a) { Add(local[r.p], t); return; } int t2 = FindLastUnusedLe(local[r.p], r.b); if(t2>t) Add(global, t2); Add(local[r.p], t2); } void Solve() { local.resize(numP); sort(requests.begin(), requests.end(), CmpEndpoint()); for (const Request &r : requests) { ProcessRequest(r); } } void Verify() { sort(requests.begin(), requests.end(), CmpPLeft()); int p = -1; vector<int> selected; int next = 0; for(Request &r : requests) { if(r.p != p) { p = r.p; selected = Expand(local[p]); next = 0; } if(selected[next] < r.a || selected[next] > r.b) throw 0; r.res = selected[next]; ++next; } } void Print() { vector<int> sol(requests.size()); for (const Request &r : requests) { sol[r.id] = r.res; } int total = Size(global); OUT(total, '\n'); for(int s : sol) OUT(s, '\n'); } int main() { ReadInput(); ReduceP(); try { Solve(); Verify(); Print(); } catch(int) { OUT("NIE\n"); } } |