QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#123847 | #2644. Cats or Dogs | lmeowdn | 0 | 3ms | 28996kb | C++14 | 4.0kb | 2023-07-13 19:48:33 | 2023-07-13 19:48:36 |
Judging History
answer
#include "catdog.h"
#include<bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define mp make_pair
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
template<typename T,typename U>
T ceil(T x, U y) {return (x>0?(x+y-1)/y:x/y);}
template<typename T,typename U>
T floor(T x, U y) {return (x>0?x/y:(x-y+1)/y);}
template<class T,class S>
bool chmax(T &a,const S b) {return (a<b?a=b,1:0);}
template<class T,class S>
bool chmin(T &a,const S b) {return (a>b?a=b,1:0);}
int popcnt(int x) {return __builtin_popcount(x);}
int popcnt(ll x) {return __builtin_popcountll(x);}
int topbit(int x) {return (x==0?-1:31-__builtin_clz(x));}
int topbit(ll x) {return (x==0?-1:63-__builtin_clzll(x));}
int lowbit(int x) {return (x==0?-1:__builtin_ctz(x));}
int lowbit(ll x) {return (x==0?-1:__builtin_ctzll(x));}
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;
typedef tuple<int,int,int> tiii;
const int N=1e5+5,inf=0x3f3f3f3f;
int n,dfn[N],tick,sz[N],son[N],top[N],dn[N],id[N],f[N][3],g[N][3],anc[N];
vi e[N];
struct Mat {
int a[3][3];
Mat(int x=0) {
a[0][0]=a[1][1]=a[2][2]=x;
a[0][1]=a[1][2]=a[2][0]=inf;
a[0][2]=a[1][0]=a[2][1]=inf;
}
} G[N],A[N],T[N],C,D,E;
Mat operator * (const Mat &a,const Mat &b) {
Mat c(inf);
rep(i,0,2) rep(j,0,2) {
chmin(c.a[i][j],a.a[i][0]+b.a[0][j]);
chmin(c.a[i][j],a.a[i][1]+b.a[1][j]);
chmin(c.a[i][j],a.a[i][2]+b.a[2][j]);
}
return c;
}
namespace SegT {
int ls[N<<1],rs[N<<1],tot=1; Mat s[N<<1];
void build(int p,int l,int r) {
if(l==r) {s[p]=T[id[l]]; return;} int mid=l+r>>1;
build(ls[p]=++tot,l,mid), build(rs[p]=++tot,mid+1,r);
s[p]=s[rs[p]]*s[ls[p]];
}
Mat qry(int p,int l,int r,int x,int y) {
if(l==x&&r==y) return s[p]; int mid=l+r>>1;
if(y<=mid) return qry(ls[p],l,mid,x,y);
else if(x>mid) return qry(rs[p],mid+1,r,x,y);
else return qry(rs[p],mid+1,r,mid+1,y)*qry(ls[p],l,mid,x,mid);
}
void mdf(int p,int l,int r,int x) {
if(l==r) {s[p]=T[id[l]]; return;} int mid=l+r>>1;
if(x<=mid) mdf(ls[p],l,mid,x); else mdf(rs[p],mid+1,r,x);
s[p]=s[rs[p]]*s[ls[p]];
}
}
void make(int u,bool flag=1) {
rep(j,0,2) G[u].a[0][j]=g[u][j];
G[u].a[1][0]=g[u][0]+1, G[u].a[1][1]=g[u][1];
G[u].a[2][0]=g[u][0]+1, G[u].a[2][2]=g[u][2];
G[u].a[1][2]=G[u].a[2][1]=inf;
T[u]=G[u]*A[u];
if(flag) SegT::mdf(1,1,n,dfn[u]);
}
void dfs1(int u,int fa) {
sz[u]=1; f[u][0]=f[u][1]=f[u][2]=0;
for(int v:e[u]) if(v!=fa) {
dfs1(v,u), sz[u]+=sz[v], anc[v]=u;
if(sz[v]>sz[son[u]]) son[u]=v;
}
}
int dfs2(int u,int tp) {
top[u]=tp, dfn[u]=++tick, id[tick]=u;
A[u]=E; make(u,0);
if(son[u]) dn[u]=dfs2(son[u],tp);
else return dn[u]=u;
for(int v:e[u]) if(v!=son[u]&&v!=anc[u]) dfs2(v,v);
return dn[u];
}
void mdf(int u) {
make(u); int x=top[u], y=dn[u];
Mat tran=SegT::qry(1,1,n,dfn[x],dfn[y]);
if(anc[x]) {
g[anc[x]][0]-=min(f[x][0],min(f[x][1],f[x][2])+1);
g[anc[x]][1]-=min(f[x][0],min(f[x][1],f[x][2]+1));
g[anc[x]][2]-=min(f[x][0],min(f[x][2],f[x][1]+1));
}
rep(j,0,2) f[x][j]=tran.a[0][j];
if(anc[x]) {
g[anc[x]][0]+=min(f[x][0],min(f[x][1],f[x][2])+1);
g[anc[x]][1]+=min(f[x][0],min(f[x][1],f[x][2]+1));
g[anc[x]][2]+=min(f[x][0],min(f[x][2],f[x][1]+1));
mdf(anc[x]);
}
}
int qry() {return min(f[1][0],min(f[1][1],f[1][2]));}
void initialize(int N,vi A,vi B) {
E=C=D=Mat(inf);
E.a[0][0]=E.a[0][1]=E.a[0][2]=E.a[1][1]=E.a[2][2]=0;
C.a[0][1]=C.a[1][1]=D.a[0][2]=D.a[2][2]=0;
n=N;
rep(i,0,n-2) {
int u=A[i], v=B[i];
e[u].eb(v), e[v].eb(u);
}
dfs1(1,0), dfs2(1,1);
SegT::build(1,1,n);
}
int cat(int v) {A[v]=C; mdf(v); return qry();}
int dog(int v) {A[v]=D; mdf(v); return qry();}
int neighbor(int v) {A[v]=E; mdf(v); return qry();}
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 8
Accepted
time: 3ms
memory: 28996kb
input:
2 2 1 75 1 2 1 1 3 1 3 2 1 1 2 2 3 1 3 2 2 2 3 2 1 1 3 1 1 2 2 1 3 2 2 2 3 2 1 2 3 1 1 1 3 2 3 1 1 2 3 2 2 1 2 2 3 2 3 1 2 1 1 2 3 1 1 1 3 2 3 1 1 2 3 2 2 1 3 1 1 1 1 2 3 1 3 2 2 1 1 2 3 1 2 1 3 2 2 2 3 1 2 1 3 2 2 2 3 2 2 2 3 1 3 2 1 1 3 1 2 2 1 1 3 1 1 1 3 2 2 2 3 2 3 1 2 2 2 1 3 1 2 1 3 2 1 2 3 1...
output:
0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0
result:
ok 75 lines
Test #2:
score: -8
Wrong Answer
time: 3ms
memory: 27196kb
input:
9 5 3 4 9 5 4 4 1 5 7 7 6 4 8 2 3 32 2 9 2 5 1 1 2 2 1 4 1 3 3 3 2 3 3 1 2 7 3 4 1 4 3 2 3 7 1 6 3 4 2 7 2 8 3 7 3 9 3 5 1 4 3 6 2 1 1 7 3 4 3 8 1 2 3 2 3 3 3 7 1 9
output:
0 0 1 1 2 4 2 2 2 2 0 2 2 2 3 1 1 1 1 1 1 3 2 3 4 1 1 2 1 1 0 1
result:
wrong answer 22nd lines differ - expected: '2', found: '3'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%