QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#340029 | #7398. Triangle Tiling | Kevin5307 | RE | 0ms | 0kb | C++20 | 4.2kb | 2024-02-28 12:39:21 | 2024-02-28 12:39:21 |
answer
//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define ls (x<<1)
#define rs (ls|1)
void die(string S){puts(S.c_str());exit(0);}
using ll=long long;
using ull=unsigned ll;
using longer=__int128_t;
const int maxn=5005;
int ans[maxn][maxn];
int val[maxn][maxn];
char grid[maxn][maxn];
bool token[maxn];
int segt[maxn][maxn<<2],tag[maxn][maxn<<2];
bool used[maxn][maxn];
void build(int ind,int x,int l,int r)
{
if(l==r)
{
segt[ind][x]=val[l][ind];
return ;
}
int mid=(l+r)/2;
build(ind,ls,l,mid);
build(ind,rs,mid+1,r);
segt[ind][x]=max(segt[ind][ls],segt[ind][rs]);
}
int query(int ind,int x,int l,int r,int qr)
{
if(qr<l) return -inf;
if(qr==r) return segt[ind][x];
int mid=(l+r)/2;
if(qr<=mid) return query(ind,ls,l,mid,qr)+tag[ind][x];
return max(segt[ind][ls],query(ind,rs,mid+1,r,qr))+tag[ind][x];
}
void update(int ind,int x,int l,int r,int qr,int v)
{
if(qr<l) return ;
if(r==qr)
{
tag[ind][x]+=v;
segt[ind][x]+=v;
return ;
}
int mid=(l+r)/2;
if(qr<=mid)
update(ind,ls,l,mid,qr,v);
else
{
update(ind,ls,l,mid,mid,v);
update(ind,rs,mid+1,r,qr,v);
}
segt[ind][x]=max(segt[ind][ls],segt[ind][rs])+tag[ind][x];
}
int Main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
string s;
cin>>s;
for(int j=1;j<=i;j++)
grid[i][j]=s[j-1];
}
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
val[i][j]=used[i][j]=0;
int cnt=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
if(grid[i][j]=='0')
{
cnt++;
val[j][j+n-i]++;
}
if(cnt!=n)
{
cout<<"Impossible!";
return 0;
}
for(int i=1;i<=n;i++)
val[i][i]--;
for(int len=1;len<=n;len++)
for(int i=1,j=len;j<=n;i++,j++)
val[i][j]+=val[i+1][j]+val[i][j-1]-val[i+1][j-1];
int mx=0;
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
mx=max(mx,val[i][j]);
if(mx)
{
cout<<"Impossible!";
return 0;
}
for(int i=1;i<=n;i++)
build(i,1,1,i);
for(int i=1;i<=n;i++)
token[i]=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
if(grid[i][j]=='0')
ans[i][j]=0;
else
ans[i][j]=2;
for(int i=n;i>=1;i--)
{
for(int j=1;j<=i;j++)
if(grid[i][j]=='0')
{
if(token[j]) token[j]=0;
else if(!used[i][j])
{
cout<<"Impossible!";
return 0;
}
}
static bool ntoken[maxn];
memset(ntoken,0,sizeof(ntoken));
int cl=inf,cr=inf;
for(int j=i;j>=1;j--)
{
if(token[j])
{
int mn=query(j+n-i,1,1,j+n-i,j);
if(j!=i&&mn<0&&!used[i-1][j]&&!ntoken[j])
{
ntoken[j]=1;
update(j+n-i,1,1,j+n-i,j,1);
ans[i][j]=3;
}
else if(mn>=0||j==i)
{
int p=i;
while(p>=1&&j>(i-p)&&grid[p][j-(i-p)]!='0')
{
ans[p][j-(i-p)]=1;
p--;
used[p][j-(i-p)]=1;
}
if(!p||j==i-p)
{
cout<<"Impossible!";
return 0;
}
for(int k=j+n-i;k<=n;k++)
{
update(k,1,1,k,j,1);
update(k,1,1,k,j-(i-p),-1);
}
}
else
{
ans[i][j]=1;
ntoken[j-1]=1;
if(cl==inf)
cl=cr=j;
else
cl=j;
}
}
if(cl>j&&cl!=inf)
{
for(int k=cl+n-i;k<=n;k++)
{
update(k,1,1,k,min(cr,k-n+i),1);
update(k,1,1,k,cl-1,-1);
}
cl=cr=inf;
}
}
if(cl==1)
for(int k=cl+n-i;k<=n;k++)
update(k,1,1,k,min(cr,k-n+i),1);
memcpy(token,ntoken,sizeof(token));
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
cout<<"-123"[ans[i][j]];
cout<<'\n';
}
return 0;
}
int main()
{
freopen("triangle.in","r",stdin);
freopen("triangle.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--)
Main();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
1 4 0 11 010 1101