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 <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <fstream>
#include <unordered_map>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define UseOFF ios_base::sync_with_stdio(0);cin.tie(0), cout.tie(0);
#define sz size
#define ins insert
#define new junkedseduneon
#define prev usfdjik
const ll N = 2e6 + 111;
ll a[N];
vector <ll> v[N];
bool used[N];
bool used2[N];
ll sum[N];
ll r[N];
bool ans[N];
void dfs(ll x, ll p) {
used[x] = 1;
sum[x] += sum[p];
if(p != 0) sum[x] += a[x];
// if(x == ) {
// cout << sum[x] << " " << sum[p] << " " << p << '\n';
// }
for(auto to : v[x]) {
if(to > x || v[to].sz() > 2) {
continue;
}
dfs(to, x);
}
}
void dfs2(ll x, ll p) {
used[x] = 1;
sum[x] = a[x] + sum[p];
for(auto to : v[x]) {
if(used[to]) continue;
if(to > x) {
return;
}
dfs(to, x);
}
}
pair <ll, ll> t[N + N + N + N];
void build(ll v, ll tl, ll tr) {
if(tl == tr) {
t[v].ff = a[tl];
t[v].ss = tl;
return;
}
ll L = v + v, R = v + v + 1, tm = (tl + tr) / 2;
build(L, tl, tm);
build(R, tm + 1, tr);
t[v].ff = max(t[L].ff, t[R].ff);
if(t[L].ff > t[R].ff) {
t[v].ss = t[L].ss;
}
else t[v].ss = t[R].ss;
}
ll mx = 0, pos = 0;
void get(ll v, ll tl, ll tr, ll l, ll r) {
if(l > tr || r < tl) return;
if(l <= tl && tr <= r) {
mx = max(mx, t[v].ff);
if(t[v].ff == mx) pos = t[v].ss;
return;
}
ll L = v + v, R = v + v + 1, tm = (tl + tr) / 2;
get(L, tl, tm, l, r);
get(R, tm + 1, tr, l, r);
}
ll p[N];
ll n;
void f(ll l, ll r) {
//cout << l << " " << r << " " << mx << " "<< pos << '\n';
if(l > r) return;
if(l == r) {
ans[l] = 1;
return;
}
mx = 0;
get(1, 1, n, l, r);
ans[pos] = 1;
if(p[pos - 1] - p[l - 1] >= mx) {
f(l, pos - 1);
}
if(p[r] - p[pos] >= mx) {
f(pos + 1, r);
}
}
void solve() {
ll m;
cin >> n >> m;
for(int i = 1; i <= n; i++) {
cin >> a[i];
p[i] = p[i - 1] + a[i];
}
bool subtask3 = 1;
for(int i = 1; i <= m; i++) {
ll x, y;
cin >> x >> y;
if(abs(x - y) > 1) subtask3 = 0;
if(x < y) swap(x, y);
v[x].pb(y);
v[y].pb(x);
}
if(subtask3 == 1) {
build(1, 1, n);
f(1, n);
for(int i = 1; i <= n; i++) cout << ans[i];
return;
}
if(n <= 2000 && m <= 2000) {
for(int i = 1; i <= n; i++) {
multiset < pair <ll, ll> > st;
st.ins({a[i], i});
//cout << x.ff << " " << x.ss << '\n';
ll cur = 0;
bool bb = 0;
for(int j = 1; j <= n; j++) used[j] = 0;
used[i] = 1;
while(st.sz() > 0) {
vector <ll> g;
pair <ll, ll> x = *st.begin();
if(cur < x.ff && x.ss != i) {
cout << '0';
bb = 1;
break;
}
cur += x.ff;
for(auto to : v[x.ss]) {
if(used[to]) continue;
//cout << i << " " << cur << " " << to << " " << x.ss << endl;
st.ins({a[to], to});
used[to] = 1;
}
st.erase(x);
}
if(st.sz() == 0) {
cout << '1';
}
}
return;
}
for(int i = n; i >= 1; i--) {
if(sum[i] == 0) {
sum[i] = a[i];
for(auto to : v[i]) {
if(to > i) sum[i] += sum[to];
// if(i == 4) {
// cout << i << " " << to << " " << sum[to] << " " << sum[i] << '\n';
// }
}
dfs(i, 0);
}
//cout << sum[i] << " " << i << " || ";
}
ans[1] = 1;
for(int i = 2; i <= n; i++) {
for(auto to : v[i]) {
if(to > i) continue;
if(ans[to] == 1) {
if(sum[i] >= a[to]) {
ans[i] = 1;
}
}
}
}
for(int i = 1; i <= n; i++) {
cout << ans[i];
}
}
signed main()
{
UseOFF
ll ttt = 1;
//cin >> ttt;
while(ttt--)
{
solve();
}
}
Compilation message (stderr)
island.cpp: In function 'void solve()':
island.cpp:146:12: warning: variable 'bb' set but not used [-Wunused-but-set-variable]
146 | bool bb = 0;
| ^~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |