QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#424098 | #8106. Mosaic | enterurusername | TL | 0ms | 0kb | C++14 | 3.6kb | 2024-05-28 21:50:25 | 2024-05-28 21:50:26 |
answer
#include <iostream>
#include <cstdio>
#include <set>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef __int128 lll;
const int MAXN = 2010;
const ll INF = 40000000000LL;
struct node {
ll x, y; int id;
} p[MAXN];
struct upd {
ll p, l, r; bool tp;
} h[MAXN<<1];
struct lim {
ll l, r, k;
bool operator <(lim b) const {
if (l!=b.l) return l<b.l;
return r<b.r;
}
};
int res[MAXN]; ll b[MAXN], cp[MAXN<<1];
set<lim> li; lim ins[5];
char buf[131072], *cs, *ct, obuf[131072], *os=obuf, *ot=obuf, ss[20];
inline char gc() {
if (cs==ct) {
ct = (cs=buf)+fread(buf,1,131072,stdin);
if (cs==ct) return 0;
}
return *cs++;
}
inline int read() {
int r=0; char ch=gc();
while ((ch<'0')||(ch>'9')) ch=gc();
while ((ch>='0')&&(ch<='9')) {
r=r*10+ch-'0'; ch=gc();
}
return r;
}
inline void pc(char ch) {
if (ot==os+131072) {
fwrite(obuf,1,131072,stdout); ot=os;
}
(*ot++) = ch;
}
inline void write(int x) {
int tot = 0;
if (!x) ss[++tot]='0';
while (x) {
ss[++tot]=x%10+'0'; x/=10;
}
for (int i=tot; i; --i) pc(ss[i]);
}
bool update(ll l, ll r, ll k, ll ep) {
set<lim>::iterator it=li.lower_bound((lim){l+1,-1LL,0LL}), beg, en; --it;
int dc=0, ic=0; bool fst=false;
while ((it!=li.end())&&(min(r,it->r)>=max(l,it->l))) {
if (it->k!=ep) return false;
if (!fst) {
fst=true; beg=it;
}
en = it;
if (it->l<l) ins[++ic]=(lim){it->l,l-1LL,it->k};
if (it->r>r) ins[++ic]=(lim){r+1LL,it->r,it->k};
++it;
}
li.erase(beg, ++en);
for (int i=1; i<=ic; ++i) li.insert(ins[i]);
li.insert((lim){l,r,k});
return true;
}
ll find(ll x) {
set<lim>::iterator it = li.lower_bound((lim){x+1,-1LL,0LL});
return (--it)->k;
}
bool check(int n, ll m) {
lll sum=lll(0); ll mxx=0LL, mxy=0LL;
li.clear(); li.insert((lim){0LL,INF+712LL,m});
//printf("m=%lld\n",m);
for (int i=n; i>=1; --i) {
ll px=p[i].x, py=p[i].y;
b[i] = find(py)-px;
if (b[i]<1LL) return false;
sum += lll(b[i])*lll(b[i]);
if (!update(py,py+b[i]-1LL,px,px+b[i])) return false;
//for(lim o:li) printf("[%lld,%lld:%lld]",o.l,o.r,o.k);
//printf("(%lld,%lld) : %lld\n",px,py,b[i]);
mxx=max(mxx,px+b[i]); mxy=max(mxy,py+b[i]);
}
if (sum!=lll(mxx)*lll(mxy)) return false;
pc('Y'); pc('E'); pc('S');
for (int i=1; i<=n; ++i) res[p[i].id]=int(b[i]);
for (int i=1; i<=n; ++i) {
pc(' '); write(res[i]);
}
pc('\n');
return true;
}
int main() {
freopen("puzzle.in","r",stdin); //freopen("puzzle.out","w",stdout);
int t = read();
while (t--) {
int n=read(); ll mnx=INF, mny=INF;
for (int i=1; i<=n; ++i) {
p[i].x=ll(read()); p[i].y=ll(read()); p[i].id=i;
mnx=min(mnx,p[i].x); mny=min(mny,p[i].y);
}
if (n==1) {
pc('Y'); pc('E');
pc('S'); pc(' ');
pc('1'); pc('\n');
continue;
}
for (int i=1; i<=n; ++i) {
p[i].x-=mnx; p[i].y-=mny;
}
sort(p+1, p+n+1, [](node x, node y) {
if (x.x!=y.x) return x.x<y.x;
return x.y>y.y;
});
int ctot=0; bool imp=false;
for (int i=1; i<n; ++i) {
if (p[i].y<p[n].y) {
cp[++ctot] = p[n].x+p[n].y-p[i].y-(p[n].x-p[i].x);
if (cp[ctot]<0) {
imp=true; break;
}
if (p[i].y) --ctot;
continue;
}
cp[++ctot] = p[n].x+p[i].y-p[n].y;
if (p[i].x<p[n].x) cp[++ctot]=p[n].x+p[i].y-p[n].y+p[n].x-p[i].x;
}
if (imp) {
pc('N'); pc('O'); pc('\n');
continue;
}
sort(cp+1,cp+ctot+1); bool flag=true;
for (int i=1; i<=ctot; ++i) {
//printf("~%d\n",cp[i]);
if ((cp[i]!=cp[i-1]||1)&&(check(n,cp[i]))) {
flag=false; break;
}
}
if (flag) {
pc('N'); pc('O'); pc('\n');
}
}
fwrite(obuf, 1, ot-os, stdout);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
3 2 0 0 0 1 2 3 2 2 3 4 1 1 2 1 3 1 1 2