This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define f first
#define s second
#define m_p make_pair
#define vec vector
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define fast_rmi ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef pair<int,int> pii;
signed main(){
fast_rmi;
int n,m;
cin>>n>>m;
vec<array<int,4>>arr(m);
vec<array<int,4>>arrt(m);
for(auto &z : arr){
cin>>z[0]>>z[1]>>z[2]>>z[3];
if(z[3]==1)
z[2]=z[2]-1;
// cout<<"L R "<<z[0]<<' '<<z[1]<<' '<<z[2]<<' '<<"TYPE "<<z[3]<<endl;
}
auto brute=[&](){
for(int mask=0;mask<(1<<n);mask++){
vec<int>a(n);
for(int i=0;i<n;i++){
if(mask&(1<<i))
a[i]=1;
else
a[i]=0;
}
auto check=[&](){
int ok=1;
for(auto &z : arr){
int cnt=0;
for(int i=z[0];i<=z[1];i++)
cnt+=(!a[i]);
if(z[3])
ok&=(cnt<=z[2]);
else{
ok&=(cnt>=z[2]);
}
}
return ok;
};
if(check()){
return a;
}
}
return vec<int>();
};
// if(n<=18){
// vec<int> ans=brute();
// if(!sz(ans)){
// cout<<-1;
// return 0;
// }
// for(auto &z : ans)
// cout<<z<<' ';
// return 0;
// }
vec<int> pref(n+1,-2e9);
vec<vec<pii>>g(n+1,vec<pii>());
for(int i=0;i<m;i++){
int l=arr[i][0];
int r=arr[i][1];
++l;++r;
int t=arr[i][2];
int lt=0,rt=r-l+1;
if(arr[i][3])
rt=t;
else
lt=t;
for(int x=lt;x<=rt;x++){
g[l-1].pb({r,x});
g[r].pb({l-1,-x});
}
}
for(int i=0;i<n;i++){
g[i].pb({i+1,1});
g[i+1].pb({i,0});
}
pref[0]=0;
queue<int>q;
q.push(0);
vec<int>last(n+1,-1000);
while(!q.empty()){
int v=q.front();q.pop();
if(last[v]==pref[v]){
continue;
}
// cout<<"V "<<v<<' '<<pref[v]<<endl;
last[v]=pref[v];
for(auto &z : g[v]){
int u=z.f;
if(pref[u]<pref[v]+z.s){
// cout<<"G "<<u<<' '<<v<<' '<<z.s<<endl;
pref[u]=pref[v]+z.s;
q.push(u);
}
}
}
for(int i=0;i<=n;i++){
if(pref[i]>i || pref[i]<0){
cout<<-1;
return 0;
}
}
vec<int>a;
for(int i=0;i<n;i++){
a[i]=(pref[i+1]-pref[i]?0:1);
}
arrt=arr;
for(int i=0;i<n;i++){
int ok=1;
int nd=0;
for(auto &z : arr){
if(z[0]<=i&&z[1]>=i){
// cout<<"ALOGG "<<z[0]<<' '<<z[1]<<endl;
if(z[3]==0 && z[2]){
nd=1;
// cout<<"NEED "<<z[0]<<' '<<z[1]<<' '<<z[2]<<endl;
}
if(z[3]==1 && !z[2]){
ok=0;
// cout<<"CHE "<<z[0]<<' '<<z[1]<<endl;
}
}
}
if(nd && ok){
a[i]=0;
// cout<<"Y "<<i<<endl;
for(auto &z : arr){
if(z[0]<=i&&z[1]>=i){
z[2]=max(0,z[2]-1);
}
}
}
}
auto check=[&](){
int ok=1;
for(auto &z : arrt){
int cnt=0;
for(int i=z[0];i<=z[1];i++)
cnt+=(!a[i]);
if(z[3])
assert(cnt<=z[2]);
else{
ok&=(cnt>=z[2]);
}
}
return ok;
};
if(!check()){
cout<<-1;
}
else
for(auto &z : a)
cout<<z<<' ';
return 0;
}
Compilation message (stderr)
restore.cpp: In function 'int main()':
restore.cpp:28:11: warning: variable 'brute' set but not used [-Wunused-but-set-variable]
28 | auto brute=[&](){
| ^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |