QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#109102 | #5418. Color the Tree | shihoghmean | WA | 30ms | 23908kb | C++17 | 4.5kb | 2023-05-27 12:46:52 | 2023-05-27 12:46:54 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fr(i,a,b) for(int i=a;i>=b;i--)
#define py puts("Yes")
#define pn puts("No")
#define pt puts("")
#define pb push_back
#define wt(x) write(x),puts("")
#define wr(x) write(x) ,putchar(' ')
#define tx printf("fds")
#define mp make_pair
#define fi first
#define se second
inline int read(){
int x=0,k=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-') k=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+ch-48;
ch=getchar();
}
return x*k;
}
void write(int x){
if(x<0){
x=-x;
putchar('-');
}
if(x>9) write(x/10);
putchar(x%10+'0');
}
int power(int x,int y,int mod){
int num=1;
while(y){
if(y&1) num=(num*x)%mod;
x=x*x%mod;
y>>=1;
}
return num;
}
int mul(int x,int y,int mod){
int num=0;
while(y){
if(y&1) num=(num+x)%mod;
x=(x+x)%mod;
y>>=1;
}
return num;
}
const int N=1e6+7,mod=998244353;
int n,tot,cnt;
int a[N];
int head[N];
struct edge{
int to,next;
}e[N];
void add(int u,int v){
e[++tot]={v,head[u]};
head[u]=tot;
}
struct tree{
int ls,rs,lazy;
int num;
}t[N];
int dep[N],son[N],mdep[N],top[N];
int rt[N];
int rmq[N][20],rmq1[N][20];
int newnode(int x){
t[x]={0,0,0,0};
return x;
}
void pu(int id){
int x=0,y=0;
if(t[id].ls) x=t[t[id].ls].num;
if(t[id].rs) y=t[t[id].rs].num;
t[id].num=x+y;
}
void update(int id,int l,int r,int x,int k,int h){
if(l==r){
t[id].num=k;
t[id].lazy=h;
return ;
}
int mid=(l+r)>>1;
if(x<=mid){
if(!t[id].ls) t[id].ls=newnode(++cnt);
update(t[id].ls,l,mid,x,k,h);
}
else{
if(!t[id].rs) t[id].rs=newnode(++cnt);
update(t[id].rs,mid+1,r,x,k,h);
}
pu(id);
}
int RMQ(int l,int r){
int x=log2(r-l+1);
return min(rmq[l][x],rmq[r-(1<<x)+1][x]);
}
int RMQ1(int l,int r){
int x=log2(r-l+1);
if(rmq[l][x]<rmq[r-(1<<x)+1][x]){
return rmq1[l][x];
}
else{
return rmq1[r-(1<<x)+1][x];
}
}
int Merge(int ddp,int dp,int x,int y,int l,int r){
if(!y||!x) return x+y;
if(l==r){
if(t[x].lazy>dp){
if(RMQ(l-dp,l-ddp)<=t[x].num+t[y].num){
t[x].num=RMQ(l-dp,l-ddp);
t[x].lazy=l-RMQ1(l-dp,l-ddp);
}
else{
t[x].num=t[x].num+t[y].num;
}
}
return x;
}
int mid=(l+r)>>1;
t[x].ls=Merge(ddp,dp,t[x].ls,t[y].ls,l,mid);
t[x].rs=Merge(ddp,dp,t[x].rs,t[y].rs,mid+1,r);
pu(x);
return x;
}
void dfs(int u,int fa){
dep[u]=mdep[u]=dep[fa]+1;
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(v==fa) continue;
dfs(v,u);
mdep[u]=max(mdep[u],mdep[v]);
if(mdep[v]>mdep[son[u]]){
son[u]=v;
}
}
}
void dfs1(int u,int fa,int tp){
top[u]=tp;
if(tp==u) rt[u]=newnode(++cnt);
update(rt[top[u]],1,n,dep[u],RMQ(0,dep[u]-dep[top[u]]),dep[u]-RMQ1(0,dep[u]-dep[top[u]]));
if(son[u]) dfs1(son[u],u,tp);
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(v==fa||v==son[u]) continue;
dfs1(v,u,v);
}
}
void dfs2(int u,int fa){
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(v==fa) continue;
dfs2(v,u);
if(top[u]==top[v]) continue;
rt[top[u]]=Merge(dep[top[u]],dep[u],rt[top[u]],rt[top[v]],1,n);
}
}
signed main(){
int tt=read();
while(tt--){
tot=0;
n=read();
fo(i,0,n) head[i]=0,rt[i]=0,son[i]=0,dep[i]=0,mdep[i]=0,top[i]=0;
fo(i,1,cnt) t[i]={0,0,0,0};
fo(i,0,n-1) a[i]=read();
fo(i,0,n-1)
fo(j,0,19) rmq[i][j]=1e9,rmq1[i][j]=0;
fo(i,0,n-1){
rmq[i][0]=a[i];
rmq1[i][0]=i;
}
fo(i,1,19){
fo(j,0,n-(1<<i)){
if(rmq[j][i-1]<rmq[j+(1<<(i-1))][i-1]){
rmq[j][i]=rmq[j][i-1];
rmq1[j][i]=rmq1[j][i-1];
}
else{
rmq[j][i]=rmq[j+(1<<(i-1))][i-1];
rmq1[j][i]=rmq1[j+(1<<(i-1))][i-1];
}
}
}
fo(i,2,n){
int x=read(),y=read();
add(x,y);
add(y,x);
}
cnt=0;
dfs(1,0);
dfs1(1,0,1);
dfs2(1,0);
wt(t[rt[1]].num);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 23908kb
input:
3 4 10 15 40 1 1 2 2 3 2 4 5 10 5 1 100 1000 1 2 2 3 2 4 4 5 4 1000 200 10 8 1 2 2 3 3 4
output:
35 17 1218
result:
ok 3 number(s): "35 17 1218"
Test #2:
score: -100
Wrong Answer
time: 30ms
memory: 17900kb
input:
3000 54 43 44 11 49 17 14 7 30 35 12 34 14 15 8 29 47 30 31 39 17 26 23 26 45 34 50 49 13 35 18 29 15 13 44 47 5 22 20 19 46 30 22 26 13 47 46 43 27 48 48 13 14 30 44 1 2 1 3 2 4 3 5 4 6 2 7 4 8 1 9 3 10 1 11 8 12 2 13 9 14 1 15 1 16 15 17 15 18 7 19 1 20 19 21 13 22 10 23 17 24 9 25 9 26 24 27 8 28...
output:
180 142 222 230 156 240 225 126 100 81 155 73 154 127 149 122 204 230 132 187 153 170 78 282 195 286 191 211 119 197 211 233 88 252 239 205 173 180 195 121 109 114 180 170 226 210 182 97 199 59 56 31 115 204 203 118 137 208 53 133 189 170 173 137 233 90 163 273 80 350 156 133 146 159 240 269 137 217...
result:
wrong answer 2nd numbers differ - expected: '168', found: '142'