QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#293896 | #7682. Redundant Towers | ucup-team087 | RE | 38ms | 3672kb | C++20 | 15.1kb | 2023-12-29 22:25:51 | 2023-12-29 22:25:52 |
Judging History
answer
#ifndef LOCAL
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#endif
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
//#define int ll
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
using pi=pair<int,int>;
using vi=vc<int>;
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
return os<<"{"<<p.a<<","<<p.b<<"}";
}
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ";
dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif
using uint=unsigned;
using ull=unsigned long long;
template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
return os<<vc<t>(all(a));
}
template<int i,class T>
void print_tuple(ostream&,const T&){
}
template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
if(i)os<<",";
os<<get<i>(t);
print_tuple<i+1,T,Args...>(os,t);
}
template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
os<<"{";
print_tuple<0,tuple<Args...>,Args...>(os,t);
return os<<"}";
}
ll read(){
ll i;
cin>>i;
return i;
}
vi readvi(int n,int off=0){
vi v(n);
rep(i,n)v[i]=read()+off;
return v;
}
pi readpi(int off=0){
int a,b;cin>>a>>b;
return pi(a+off,b+off);
}
template<class t>
void print_single(t x,int suc=1){
cout<<x;
if(suc==1)
cout<<"\n";
if(suc==2)
cout<<" ";
}
template<class t,class u>
void print_single(const pair<t,u>&p,int suc=1){
print_single(p.a,2);
print_single(p.b,suc);
}
template<class T>
void print_single(const vector<T>&v,int suc=1){
rep(i,v.size())
print_single(v[i],i==int(v.size())-1?suc:2);
}
template<class T>
void print_offset(const vector<T>&v,ll off,int suc=1){
rep(i,v.size())
print_single(v[i]+off,i==int(v.size())-1?suc:2);
}
template<class T,size_t N>
void print_single(const array<T,N>&v,int suc=1){
rep(i,N)
print_single(v[i],i==int(N)-1?suc:2);
}
template<class T>
void print(const T&t){
print_single(t);
}
template<class T,class ...Args>
void print(const T&t,const Args&...args){
print_single(t,2);
print(args...);
}
string readString(){
string s;
cin>>s;
return s;
}
template<class T>
T sq(const T& t){
return t*t;
}
void YES(bool ex=true){
cout<<"YES\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void NO(bool ex=true){
cout<<"NO\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void Yes(bool ex=true){
cout<<"Yes\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void No(bool ex=true){
cout<<"No\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
//#define CAPITAL
/*
void yes(bool ex=true){
#ifdef CAPITAL
cout<<"YES"<<"\n";
#else
cout<<"Yes"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void no(bool ex=true){
#ifdef CAPITAL
cout<<"NO"<<"\n";
#else
cout<<"No"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}*/
void possible(bool ex=true){
#ifdef CAPITAL
cout<<"POSSIBLE"<<"\n";
#else
cout<<"Possible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void impossible(bool ex=true){
#ifdef CAPITAL
cout<<"IMPOSSIBLE"<<"\n";
#else
cout<<"Impossible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
constexpr ll ten(int n){
return n==0?1:ten(n-1)*10;
}
const ll infLL=LLONG_MAX/3;
#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif
int topbit(signed t){
return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
return t==0?-1:63-__builtin_clzll(t);
}
int topbit(ull t){
return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
return a==0?64:__builtin_ctzll(a);
}
int botbit(ull a){
return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
return __builtin_popcount(t);
}
int popcount(ll t){
return __builtin_popcountll(t);
}
int popcount(ull t){
return __builtin_popcountll(t);
}
int bitparity(ll t){
return __builtin_parityll(t);
}
bool ispow2(int i){
return i&&(i&-i)==i;
}
ll mask(int i){
return (ll(1)<<i)-1;
}
ull umask(int i){
return (ull(1)<<i)-1;
}
ll minp2(ll n){
if(n<=1)return 1;
else return ll(1)<<(topbit(n-1)+1);
}
bool inc(int a,int b,int c){
return a<=b&&b<=c;
}
template<class t> void mkuni(vc<t>&v){
sort(all(v));
v.erase(unique(all(v)),v.ed);
}
ll rand_int(ll l, ll r) { //[l, r]
//#ifdef LOCAL
static mt19937_64 gen;
/*#else
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
#endif*/
return uniform_int_distribution<ll>(l, r)(gen);
}
ll rand_int(ll k){ //[0,k)
return rand_int(0,k-1);
}
template<class t>
void myshuffle(vc<t>&a){
rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}
template<class t,class u>
int lwb(const vc<t>&v,const u&a){
return lower_bound(all(v),a)-v.bg;
}
template<class t,class u>
bool bis(const vc<t>&v,const u&a){
return binary_search(all(v),a);
}
vvc<int> readGraph(int n,int m){
vvc<int> g(n);
rep(i,m){
int a,b;
cin>>a>>b;
//sc.read(a,b);
a--;b--;
g[a].pb(b);
g[b].pb(a);
}
return g;
}
vvc<int> readTree(int n){
return readGraph(n,n-1);
}
template<class t>
vc<t> presum(const vc<t>&a){
vc<t> s(si(a)+1);
rep(i,si(a))s[i+1]=s[i]+a[i];
return s;
}
vc<ll> presum(const vi&a){
vc<ll> s(si(a)+1);
rep(i,si(a))s[i+1]=s[i]+a[i];
return s;
}
//BIT で数列を管理するときに使う (CF850C)
template<class t>
vc<t> predif(vc<t> a){
gnr(i,1,si(a))a[i]-=a[i-1];
return a;
}
template<class t>
vvc<ll> imos(const vvc<t>&a){
int n=si(a),m=si(a[0]);
vvc<ll> b(n+1,vc<ll>(m+1));
rep(i,n)rep(j,m)
b[i+1][j+1]=b[i+1][j]+b[i][j+1]-b[i][j]+a[i][j];
return b;
}
//verify してないや
void transvvc(int&n,int&m){
swap(n,m);
}
template<class t,class... Args>
void transvvc(int&n,int&m,vvc<t>&a,Args&...args){
assert(si(a)==n);
vvc<t> b(m,vi(n));
rep(i,n){
assert(si(a[i])==m);
rep(j,m)b[j][i]=a[i][j];
}
a.swap(b);
transvvc(n,m,args...);
}
//CF854E
void rotvvc(int&n,int&m){
swap(n,m);
}
template<class t,class... Args>
void rotvvc(int&n,int&m,vvc<t>&a,Args&...args){
assert(si(a)==n);
vvc<t> b(m,vi(n));
rep(i,n){
assert(si(a[i])==m);
rep(j,m)b[m-1-j][i]=a[i][j];
}
a.swap(b);
rotvvc(n,m,args...);
}
//ソートして i 番目が idx[i]
//CF850C
template<class t>
vi sortidx(const vc<t>&a){
int n=si(a);
vi idx(n);iota(all(idx),0);
sort(all(idx),[&](int i,int j){return a[i]<a[j];});
return idx;
}
//vs[i]=a[idx[i]]
//例えば sortidx で得た idx を使えば単にソート列になって返ってくる
//CF850C
template<class t>
vc<t> a_idx(const vc<t>&a,const vi&idx){
int n=si(a);
assert(si(idx)==n);
vc<t> vs(n);
rep(i,n)vs[i]=a[idx[i]];
return vs;
}
//CF850C
vi invperm(const vi&p){
int n=si(p);
vi q(n);
rep(i,n)q[p[i]]=i;
return q;
}
template<class t,class s=t>
s SUM(const vc<t>&a){
return accumulate(all(a),s(0));
}
template<class t,size_t K,class s=t>
s SUM(const array<t,K>&a){
return accumulate(all(a),s(0));
}
template<class t>
t MAX(const vc<t>&a){
return *max_element(all(a));
}
template<class t>
pair<t,int> MAXi(const vc<t>&a){
auto itr=max_element(all(a));
return mp(*itr,itr-a.bg);
}
template<class A>
auto MIN(const A&a){
return *min_element(all(a));
}
template<class t>
pair<t,int> MINi(const vc<t>&a){
auto itr=min_element(all(a));
return mp(*itr,itr-a.bg);
}
vi vid(int n){
vi res(n);iota(all(res),0);
return res;
}
template<class S>
void soin(S&s){
sort(all(s));
}
template<class S,class F>
void soin(S&s,F&&f){
sort(all(s),forward<F>(f));
}
template<class S>
S soout(S s){
soin(s);
return s;
}
template<class S>
void rein(S&s){
reverse(all(s));
}
template<class S>
S reout(S s){
rein(s);
return s;
}
template<class t,class u>
pair<t,u>&operator+=(pair<t,u>&a,pair<t,u> b){
a.a+=b.a;a.b+=b.b;return a;}
template<class t,class u>
pair<t,u>&operator-=(pair<t,u>&a,pair<t,u> b){
a.a-=b.a;a.b-=b.b;return a;}
template<class t,class u>
pair<t,u> operator+(pair<t,u> a,pair<t,u> b){return mp(a.a+b.a,a.b+b.b);}
template<class t,class u>
pair<t,u> operator-(pair<t,u> a,pair<t,u> b){return mp(a.a-b.a,a.b-b.b);}
template<class t,class u>
istream&operator>>(istream&is,pair<t,u>&a){
return is>>a.a>>a.b;
}
template<class t>
t gpp(vc<t>&vs){
assert(si(vs));
t res=move(vs.back());
vs.pop_back();
return res;
}
template<class t,class u>
void pb(vc<t>&a,const vc<u>&b){
a.insert(a.ed,all(b));
}
template<class t,class...Args>
vc<t> cat(vc<t> a,Args&&...b){
(pb(a,forward<Args>(b)),...);
return a;
}
template<class t,class u>
vc<t>& operator+=(vc<t>&a,u x){
for(auto&v:a)v+=x;
return a;
}
template<class t,class u>
vc<t> operator+(vc<t> a,u x){
return a+=x;
}
template<class t>
vc<t> operator+(const vc<t>&a,const vc<t>&b){
vc<t> c(max(si(a),si(b)));
rep(i,si(a))c[i]+=a[i];
rep(i,si(b))c[i]+=b[i];
return c;
}
template<class t,class u>
vc<t>& operator-=(vc<t>&a,u x){
for(auto&v:a)v-=x;
return a;
}
template<class t,class u>
vc<t>& operator-(vc<t> a,u x){
return a-=x;
}
template<class t,class u>
void remval(vc<t>&a,const u&v){
a.erase(remove(all(a),v),a.ed);
}
template<class t,class F>
void remif(vc<t>&a,F f){
a.erase(remove_if(all(a),f),a.ed);
}
template<class VS,class u>
void fila(VS&vs,const u&a){
fill(all(vs),a);
}
template<class t,class u>
int findid(const vc<t>&vs,const u&a){
auto itr=find(all(vs),a);
if(itr==vs.ed)return -1;
else return itr-vs.bg;
}
template<class t>
void rtt(vc<t>&vs,int i){
rotate(vs.bg,vs.bg+i,vs.ed);
}
bool dbg=false;
int R;
int sqR(int v){
chmax(v,-(R+1));
chmin(v,(R+1));
return sq(v);
}
bool connnected(pi a,pi b){
return sqR(a.a-b.a)+sqR(a.b-b.b)<=sq(R);
}
const int K=4;
const int S=K*8;
struct N{
int n=0,lcnt=0,ans=0;
pi pos[S];
vc<pi> g[S];
void init(pi v){
pos[0]=v;
n=1;
ans=1;
}
void toggle(){
n^=1;
ans^=1;
}
void ae(int a,int b,int c){
g[a].eb(b,c);
g[b].eb(a,c);
}
void copy_from(const N&x){
n=x.n;
lcnt=x.lcnt;
ans=x.ans;
rep(i,n)pos[i]=x.pos[i];
rep(i,n){
g[i].clear();
g[i].insert(g[i].ed,all(x.g[i]));
}
}
};
void mg(const N&l,const N&r,N&dst){
if(l.n==0)return dst.copy_from(r);
if(r.n==0)return dst.copy_from(l);
static pi pos[S*2];
static vc<pi> g[S*2];
int n=l.n+r.n;
rep(i,l.n){
pos[i]=l.pos[i];
g[i].clear();
for(auto [j,c]:l.g[i])
g[i].eb(j,c);
}
rep(i,r.n){
pos[l.n+i]=r.pos[i];
g[l.n+i].clear();
for(auto [j,c]:r.g[i])
g[l.n+i].eb(l.n+j,c);
}
rng(i,max(l.n-(R-1),0),l.n)rng(j,l.n,min(l.n+(R-1),n)){
if(connnected(pos[i],pos[j])){
g[i].eb(j,0);
g[j].eb(i,0);
}
}
dmp(n);
rep(i,n)dmp2(i,g[i]);
static int vis[S*2],ord[S*2],low[S*2];
int head,s;
static vi tr[S*4];
static int es[S*4],wei[S*4];
auto ae=[&](int a,int b){
if(a==-1||b==-1)return;
tr[a].pb(b);
tr[b].pb(a);
};
auto dfs=[&](auto self,int v)->void{
assert(vis[v]==0);
vis[v]=1;
ord[v]=low[v]=head++;
for(auto [to,num]:g[v]){
if(vis[to]==0){
self(self,to);
chmin(low[v],low[to]);
}else{
chmin(low[v],ord[to]);
}
}
};
auto dfs2=[&](auto self,int v,int z)->void{
assert(vis[v]==1);
vis[v]=2;
ae(v,z);
for(auto [to,num]:g[v])if(vis[to]==1){
if(low[to]<ord[v]){
self(self,to,z);
}else{
int w=s++;
tr[w].clear();
es[w]=0;
wei[w]=0;
ae(v,w);
self(self,to,w);
}
}else if(vis[to]==2){
es[z]++;
wei[z]+=num;
}
vis[v]=3;
};
rep(i,n)vis[i]=0;
rep(i,n)ord[i]=-1;
rep(i,n)low[i]=-1;
head=0;
s=n;
rep(i,n)tr[i].clear();
rep(i,n)if(vis[i]==0){
dfs(dfs,i);
dfs2(dfs2,i,-1);
}
dst.lcnt=l.lcnt+r.lcnt;
rng(i,n,s)if(es[i]>=2){
dst.lcnt+=wei[i];
wei[i]=0;
}
int xmin=inf,xmax=-inf;
rep(i,n){
chmin(xmin,pos[i].a);
chmax(xmax,pos[i].a);
}
static bool use[S*4];
rep(i,s)use[i]=false;
auto good=[&](int i){
return pos[i].a<=xmin+(R-2)||xmax-(R-2)<=pos[i].a;
};
auto dfs3=[&](auto self,int v,int p)->bool{
use[v]=good(v);
for(auto to:tr[v])if(to!=p)
use[v]|=self(self,to,v);
return use[v];
};
rep(i,n)if(!use[i]&&good(i)){
dfs3(dfs3,i,-1);
}
dst.ans=dst.lcnt;
rep(i,n)if(si(tr[i])<=1&&!use[i])dst.lcnt++;
rep(i,n)if(si(tr[i])<=1)dst.ans++;
rep(i,s)if(use[i])remif(tr[i],[&](int v){return !use[v];});
static bool alive[S*2];
rep(i,n)alive[i]=use[i]&&(good(i)||si(tr[i])>=3);
rng(i,n,s)if(use[i]&&si(tr[i])>=3){
for(auto j:tr[i])alive[j]=true;
}
static int idx[S*2];
dst.n=0;
rep(i,n)if(alive[i]){
int j=dst.n++;
idx[i]=j;
dst.pos[j]=pos[i];
dst.g[j].clear();
}
rng(i,n,s)if(si(tr[i])>=3){
int pre=tr[i].back();
for(auto j:tr[i]){
assert(alive[pre]);
assert(alive[j]);
dst.ae(idx[pre],idx[j],0);
pre=j;
}
}
auto getnext=[&](int u,int v){
if(tr[v][0]==u)return tr[v][1];
else return tr[v][0];
};
rep(i,n)if(alive[i]){
for(auto to:tr[i])if(si(tr[to])==2){
int u=i,v=to,sum=0;
while(1){
if(v<n){
if(alive[v])break;
}else{
sum+=wei[v-n];
}
assert(si(tr[v])==2);
int nx=getnext(u,v);
u=v;
v=nx;
}
assert(alive[i]);
assert(alive[v]);
dst.ae(idx[i],idx[v],sum);
}
}
}
void slv(){
int n;cin>>n;
cin>>R;
vc<pi> raw(n);
rep(i,n)cin>>raw[i];
vi idx=sortidx(raw);
vi xdi=invperm(idx);
int s=minp2(n);
vc<N> buf(s*2);
rep(i,n){
buf[s+i].init(raw[idx[i]]);
}
auto upd=[&](int i){
mg(buf[i*2],buf[i*2+1],buf[i]);
};
gnr(i,1,s)upd(i);
int q;cin>>q;
int last=0;
rep(_,q){
int k;cin>>k;
k^=last;
k--;
assert(inc(0,k,n-1));
dmp(k);
k=xdi[k];
k+=s;
buf[k].toggle();
while(k>>=1)upd(k);
print(buf[1].ans);
last=buf[1].ans;
}
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout<<fixed<<setprecision(20);
if(dbg){
while(1)slv();
}else{
//int t;cin>>t;rep(_,t)
slv();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3576kb
input:
5 3 3 2 4 1 5 4 1 3 2 5 6 1 6 0 3 0 1
output:
4 3 2 2 2 3
result:
ok 6 numbers
Test #2:
score: 0
Accepted
time: 38ms
memory: 3672kb
input:
20 2 13 9 7 15 10 6 15 12 12 11 14 18 18 19 6 4 2 16 3 10 1 8 4 14 8 3 9 17 19 1 11 13 16 2 20 5 17 20 5 7 100000 1 18 23 3 29 1 19 31 19 22 26 4 26 9 3 11 5 7 12 2 30 26 6 25 1 0 24 29 6 28 12 4 29 5 13 8 14 25 8 29 13 7 10 15 2 12 1 13 4 14 5 10 12 25 30 0 7 1 28 1 4 10 14 14 12 13 11 9 4 22 15 23...
output:
19 20 19 18 17 18 17 16 17 16 15 14 13 12 13 12 11 10 11 12 11 10 11 12 11 12 13 12 13 14 13 12 11 12 13 12 13 12 13 14 13 12 11 10 9 10 9 10 9 10 9 10 11 10 11 12 11 12 11 10 11 10 9 8 9 8 7 6 5 4 5 6 5 6 7 8 9 10 11 12 11 12 11 10 9 10 11 10 11 12 11 12 13 12 13 14 13 14 13 12 11 10 11 10 9 10 11 ...
result:
ok 100000 numbers
Test #3:
score: -100
Runtime Error
input:
20 3 19 1 17 19 14 15 8 13 7 2 11 20 13 10 20 7 10 14 4 8 2 12 5 9 18 16 6 3 16 6 1 4 3 17 12 18 9 11 15 5 100000 1 19 1 22 0 3 2 29 21 23 24 7 21 11 11 31 1 11 30 7 10 15 15 4 7 30 1 10 2 4 13 9 31 12 8 12 24 1 11 4 1 11 7 8 26 26 9 27 4 0 8 21 28 24 13 7 12 0 4 13 14 1 10 5 13 0 3 11 10 0 28 13 10...