QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#55233 | #1955. Double Rainbow | KING_UT# | RE | 0ms | 0kb | C++20 | 3.7kb | 2022-10-12 19:55:38 | 2022-10-12 19:55:40 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#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 bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define pb push_back
#define eb emplace_back
#define si(x) int(x.size())
template<class t>using vc=vector<t>;
template<class t>using vvc=vc<vc<t>>;
using vi=vc<int>;
#define a first
#define b second
#define mp make_pair
using pi=pair<int,int>;
template<class t,class u>bool chmin(t&a,u b){
if(a>b){
a=b;
return true;
}else return false;
}
template<class t,class u>bool chmax(t&a,u b){
if(a<b){
a=b;
return true;
}else return false;
}
template<class t>void mkuni(vc<t>&vs){
sort(all(vs));
vs.erase(unique(all(vs)),vs.ed);
}
template<class t>int lwb(const vc<t>&vs,t v){
return lower_bound(all(vs),v)-vs.bg;
}
const int inf=INT_MAX/2-100;
template<class E>
struct HLD{
vvc<E> g;
int n,rt,cnt;
vi sub,in,out,par,head,dep,hei,ni;
vc<E> pe;
int dfs1(int v,int p,int d){
par[v]=p;
dep[v]=d;
auto itr=find(all(g[v]),p);
if(itr!=g[v].ed){
pe[v]=*itr;
g[v].erase(itr);
}
for(auto&e:g[v]){
pe[e]=e;
sub[v]+=dfs1(e,v,d+1);
if(sub[g[v][0]]<sub[e])
swap(g[v][0],e);
}
return sub[v];
}
void dfs2(int v,int h){
in[v]=cnt++;
head[v]=h;
for(int to:g[v])
dfs2(to,to==g[v][0]?h:to);
out[v]=cnt;
if(si(g[v]))hei[v]=hei[g[v][0]]+1;
}
HLD(){}
HLD(const vvc<E>&gg,int rr):g(gg),n(si(g)),rt(rr),cnt(0),
sub(n,1),in(n),out(n),par(n,-1),head(n),dep(n),hei(n,1),ni(n),
pe(n){
dfs1(rt,-1,0);
dfs2(rt,rt);
rep(i,n)ni[in[i]]=i;
}
int lca(int a,int b){
while(head[a]!=head[b]){
if(dep[head[a]]>dep[head[b]])
swap(a,b);
b=par[head[b]];
}
if(dep[a]>dep[b])swap(a,b);
return a;
}
vi index;
pair<vi,vc<pi>> tree_compress(vi vs){
if(si(index)==0)index.resize(n);
auto cmp=[&](int x,int y){
return in[x]<in[y];
};
sort(all(vs),cmp);
vs.erase(unique(all(vs)),vs.ed);
int k=si(vs);
rep(i,k-1)vs.pb(lca(vs[i],vs[i+1]));
sort(all(vs),cmp);
vs.erase(unique(all(vs)),vs.ed);
k=si(vs);
rep(i,k)index[vs[i]]=i;
vc<pi> es;
rng(i,1,k){
int p=lca(vs[i-1],vs[i]);
es.eb(i,index[p]);
}
return mp(vs,es);
}
};
struct Q{
int a,b,c;
void readinit(){
string s;cin>>s;
if(s=="U"){
cin>>a;a--;
b=-1;
cin>>c;c--;
}else{
cin>>a>>b>>c;
a--;b--;c--;
}
}
};
struct segtree{
struct N{
int mn,len,l,r;
N(){single(0,0);}
void single(int v,int w){
mn=inf;
len=w;
if(v==0){
l=inf;
r=inf;
}else{
l=0;
r=w;
}
}
static N merge(const N&a,const N&b){
N res;
res.mn=min({a.mn,b.mn,a.r+b.l});
res.len=a.len+b.len;
res.l=min(a.l,a.len+b.l);
res.r=min(a.r+b.len,b.r);
return res;
}
void rev(){
swap(l,r);
}
};
int s;
vc<N> x;
void init(int n){
s=1;while(s<n)s*=2;
x.resize(s*2,N());
}
void upd(int i){
x[i]=N::merge(x[i*2],x[i*2+1]);
}
void setsingle(int i,int v,int w){
i+=s;
x[i].single(v,w);
while(i>>=1)upd(i);
}
N composite(int i,int l,int r,int b,int e){
if(r<=e||b<=l)return N();
if(b<=l&&r<=e)return x[i];
return N::merge(
composite(i*2,l,(l+r)/2,b,e),
composite(i*2+1,(l+r)/2,r,b,e));
}
N composite(int b,int e){
assert(b<=e);
return composite(1,0,s,b,e);
}
};
void slv(){
int n,q;cin>>n>>q;
vvc<int> t(n);
vi col(n);
vvc<int> vs(n);
rep(i,n){
cin>>col[i];col[i]--;
vs[col[i]].pb(i);
}
rep(i,n-1){
int a,b;cin>>a>>b;
a--;b--;
t[a].pb(b);
t[b].pb(a);
}
vc<Q> qs(q);
rep(i,q){
qs[i].readinit();
if(qs[i].b==-1)
vs[qs[i].c].pb(qs[i].a);
}
HLD<int> hld(t,0);
vc<HLD<int>> hs(n);
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
slv();
}
詳細信息
Test #1:
score: 0
Runtime Error
input:
1 1 1