답안 #367870

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
367870 2021-02-18T16:14:51 Z denkendoemeer Cats or Dogs (JOI18_catdog) C++14
100 / 100
304 ms 22636 KB
#include<bits/stdc++.h>
#include "catdog.h"
#define ll long long
using namespace std;
const int inf=1e9;
int min(int a,int b)
{
    if (a<b)
        return a;
    return b;
}
struct mat
{
    int a[2][2];
    void init()
    {
        a[0][0]=a[1][1]=0;
        a[1][0]=a[0][1]=inf;
    }
    mat()
    {
        init();
    }
    mat operator * (mat b) const
    {
        mat c;
        int i,j,k,e;
        for(i=0;i<2;i++)
            for(j=0;j<2;j++){
                c.a[i][j]=inf;
                for(k=0;k<2;k++)
                    for(e=0;e<2;e++)
                        c.a[i][j]=min(c.a[i][j],a[i][k]+b.a[e][j]+(k^e));
            }
        return c;
    }
}aint[200005];
int l[200005],r[200005],root[100005],u;
void update(int &nod,int st,int dr,int poz,int a,int b)
{
    if (nod==0)
        nod=++u;
    if (st==dr){
        aint[nod].a[0][0]+=a;
        aint[nod].a[1][1]+=b;
        return ;
    }
    int mij=(st+dr)/2;
    if (poz<=mij)
        update(l[nod],st,mij,poz,a,b);
    else
        update(r[nod],mij+1,dr,poz,a,b);
    aint[nod]=aint[l[nod]]*aint[r[nod]];
}
pair<int,int> query(int nod)
{
    int a=min(aint[nod].a[0][0],aint[nod].a[0][1]),b=min(aint[nod].a[1][0],aint[nod].a[1][1]);
    a=min(a,b+1);
    b=min(b,a+1);
    return make_pair(a,b);
}
int head[100005],sz[100005],dep[100005],t[100005],sz2[100005];
vector<int>g[100005];
void dfs(int nod,int tat)
{
    t[nod]=tat;
    dep[nod]=dep[tat]+1;
    sz[nod]=1;
    for(auto it:g[nod])
        if (it!=tat)
            dfs(it,nod),sz[nod]+=sz[it];
}
void hld(int nod,int tat)
{
    if (head[nod]==0)
        head[nod]=nod;
    sz2[head[nod]]++;
    int ok=0;
    for(auto it:g[nod])
        if (it!=tat && sz[ok]<sz[it])
            ok=it;
    if (ok)
        head[ok]=head[nod];
    for(auto it:g[nod])
        if (it!=tat)
            hld(it,nod);
}
int nr[100005];
void initialize(int n,vector<int>a,vector<int>b)
{
    int i;
    for(i=0;i<n-1;i++)
        g[a[i]].push_back(b[i]),g[b[i]].push_back(a[i]);
    dfs(1,0);
    hld(1,0);
}
int calc()
{
    pair<int,int>aux=query(root[1]);
    return min(aux.first,aux.second);
}
void mark(int nod,int a,int b)
{
    while(nod){
        int xa,xb,ya,yb;
        pair<int,int>aux=query(root[head[nod]]);
        xa=aux.first;
        xb=aux.second;
        update(root[head[nod]],1,sz2[head[nod]],dep[nod]-dep[head[nod]]+1,a,b);
        aux=query(root[head[nod]]);
        ya=aux.first;
        yb=aux.second;
        a=ya-xa;
        b=yb-xb;
        nod=t[head[nod]];
    }
}
int cat(int nod)
{
    nr[nod]=1;
    mark(nod,100000,0);
    return calc();
}
int dog(int nod)
{
    nr[nod]=2;
    mark(nod,0,100000);
    return calc();
}
int neighbor(int nod)
{
    if (nr[nod]==1)
        mark(nod,-100000,0);
    if (nr[nod]==2)
        mark(nod,0,-100000);
    return calc();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5868 KB Output is correct
2 Correct 4 ms 5868 KB Output is correct
3 Correct 4 ms 5868 KB Output is correct
4 Correct 4 ms 5888 KB Output is correct
5 Correct 5 ms 5868 KB Output is correct
6 Correct 5 ms 5892 KB Output is correct
7 Correct 4 ms 5868 KB Output is correct
8 Correct 4 ms 5868 KB Output is correct
9 Correct 4 ms 5868 KB Output is correct
10 Correct 4 ms 5868 KB Output is correct
11 Correct 4 ms 5868 KB Output is correct
12 Correct 4 ms 5868 KB Output is correct
13 Correct 4 ms 5868 KB Output is correct
14 Correct 4 ms 5868 KB Output is correct
15 Correct 4 ms 5868 KB Output is correct
16 Correct 4 ms 5868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5868 KB Output is correct
2 Correct 4 ms 5868 KB Output is correct
3 Correct 4 ms 5868 KB Output is correct
4 Correct 4 ms 5888 KB Output is correct
5 Correct 5 ms 5868 KB Output is correct
6 Correct 5 ms 5892 KB Output is correct
7 Correct 4 ms 5868 KB Output is correct
8 Correct 4 ms 5868 KB Output is correct
9 Correct 4 ms 5868 KB Output is correct
10 Correct 4 ms 5868 KB Output is correct
11 Correct 4 ms 5868 KB Output is correct
12 Correct 4 ms 5868 KB Output is correct
13 Correct 4 ms 5868 KB Output is correct
14 Correct 4 ms 5868 KB Output is correct
15 Correct 4 ms 5868 KB Output is correct
16 Correct 4 ms 5868 KB Output is correct
17 Correct 5 ms 5888 KB Output is correct
18 Correct 5 ms 5996 KB Output is correct
19 Correct 5 ms 5868 KB Output is correct
20 Correct 5 ms 5868 KB Output is correct
21 Correct 5 ms 5868 KB Output is correct
22 Correct 5 ms 5996 KB Output is correct
23 Correct 5 ms 5868 KB Output is correct
24 Correct 5 ms 5868 KB Output is correct
25 Correct 6 ms 5868 KB Output is correct
26 Correct 4 ms 5868 KB Output is correct
27 Correct 5 ms 5868 KB Output is correct
28 Correct 6 ms 5996 KB Output is correct
29 Correct 6 ms 6012 KB Output is correct
30 Correct 4 ms 5868 KB Output is correct
31 Correct 4 ms 5868 KB Output is correct
32 Correct 4 ms 5868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5868 KB Output is correct
2 Correct 4 ms 5868 KB Output is correct
3 Correct 4 ms 5868 KB Output is correct
4 Correct 4 ms 5888 KB Output is correct
5 Correct 5 ms 5868 KB Output is correct
6 Correct 5 ms 5892 KB Output is correct
7 Correct 4 ms 5868 KB Output is correct
8 Correct 4 ms 5868 KB Output is correct
9 Correct 4 ms 5868 KB Output is correct
10 Correct 4 ms 5868 KB Output is correct
11 Correct 4 ms 5868 KB Output is correct
12 Correct 4 ms 5868 KB Output is correct
13 Correct 4 ms 5868 KB Output is correct
14 Correct 4 ms 5868 KB Output is correct
15 Correct 4 ms 5868 KB Output is correct
16 Correct 4 ms 5868 KB Output is correct
17 Correct 5 ms 5888 KB Output is correct
18 Correct 5 ms 5996 KB Output is correct
19 Correct 5 ms 5868 KB Output is correct
20 Correct 5 ms 5868 KB Output is correct
21 Correct 5 ms 5868 KB Output is correct
22 Correct 5 ms 5996 KB Output is correct
23 Correct 5 ms 5868 KB Output is correct
24 Correct 5 ms 5868 KB Output is correct
25 Correct 6 ms 5868 KB Output is correct
26 Correct 4 ms 5868 KB Output is correct
27 Correct 5 ms 5868 KB Output is correct
28 Correct 6 ms 5996 KB Output is correct
29 Correct 6 ms 6012 KB Output is correct
30 Correct 4 ms 5868 KB Output is correct
31 Correct 4 ms 5868 KB Output is correct
32 Correct 4 ms 5868 KB Output is correct
33 Correct 135 ms 12120 KB Output is correct
34 Correct 80 ms 11756 KB Output is correct
35 Correct 153 ms 10852 KB Output is correct
36 Correct 242 ms 16308 KB Output is correct
37 Correct 22 ms 8428 KB Output is correct
38 Correct 274 ms 17244 KB Output is correct
39 Correct 281 ms 17244 KB Output is correct
40 Correct 290 ms 17372 KB Output is correct
41 Correct 283 ms 17376 KB Output is correct
42 Correct 271 ms 17372 KB Output is correct
43 Correct 299 ms 17244 KB Output is correct
44 Correct 297 ms 17244 KB Output is correct
45 Correct 257 ms 17372 KB Output is correct
46 Correct 303 ms 17372 KB Output is correct
47 Correct 304 ms 17244 KB Output is correct
48 Correct 76 ms 13364 KB Output is correct
49 Correct 91 ms 15128 KB Output is correct
50 Correct 29 ms 8044 KB Output is correct
51 Correct 37 ms 9552 KB Output is correct
52 Correct 18 ms 7788 KB Output is correct
53 Correct 139 ms 14688 KB Output is correct
54 Correct 81 ms 10232 KB Output is correct
55 Correct 219 ms 14048 KB Output is correct
56 Correct 112 ms 11248 KB Output is correct
57 Correct 165 ms 14632 KB Output is correct
58 Correct 31 ms 9504 KB Output is correct
59 Correct 39 ms 9324 KB Output is correct
60 Correct 83 ms 14188 KB Output is correct
61 Correct 88 ms 14548 KB Output is correct
62 Correct 60 ms 12484 KB Output is correct
63 Correct 55 ms 12908 KB Output is correct
64 Correct 56 ms 14316 KB Output is correct
65 Correct 79 ms 18924 KB Output is correct
66 Correct 63 ms 9452 KB Output is correct
67 Correct 77 ms 15212 KB Output is correct
68 Correct 139 ms 19308 KB Output is correct
69 Correct 33 ms 7404 KB Output is correct
70 Correct 12 ms 6124 KB Output is correct
71 Correct 72 ms 12340 KB Output is correct
72 Correct 94 ms 17516 KB Output is correct
73 Correct 183 ms 22636 KB Output is correct
74 Correct 190 ms 19052 KB Output is correct
75 Correct 160 ms 22508 KB Output is correct
76 Correct 154 ms 21228 KB Output is correct
77 Correct 206 ms 19308 KB Output is correct