Submission #1076667

# Submission time Handle Problem Language Result Execution time Memory
1076667 2024-08-26T15:25:20 Z modwwe Jail (JOI22_jail) C++17
60 / 100
5000 ms 286936 KB
//#pragma GCC optimize("Ofast,unroll-loops")
#include<bits/stdc++.h>
//#define int long long
//#define ll long long
#define down cout<<'\n';
#define debug cout<<" cucuucucuuu",down
#define NHP     ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define modwwe  int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".ans","w",stdout)
#define pb push_back
#define checktime   cerr << (double)clock() / CLOCKS_PER_SEC * 1000  << " ms";
using namespace std;
void phongbeo();
const int inf=1e9;
const int mod2=1e9+7;
const int  mod1=998244353;
struct icd
{
    long double a;
    int b;
};
struct ib
{
    int a;
    int b;
};
struct ic
{
    int a,b,c;
};
struct id
{
    int a,b,c,d;
};
struct ie
{
    int a,b,c,d,e;

};

int n,m,s1,s2,s4,s3,sf,k,s5,s6,mx,s7,s8,s9,mx2,res,dem2=0,dem=0,s33,dem3,dem4,l,r,mid;
int  i,s10,s12;
int kk;
int el=29;

main()
{
#ifndef ONLINE_JUDGE
     // fin(task),fou(task);
#endif
    NHP
    /// cin>>s1;
    modwwe
    phongbeo(),down
//checktime
}
int st[17][120001];
int depth[120001];
int head[120001];
int pos[120001];
vector<int> v[120001];
int heavy[120001];
ib a[120001];
int b[120001];
int c[120001];
stack<int> s;
int vitri[120001];
vector<int> v2[480001];
int f[120001];
int sz[120001];
int mxz[120001];
void dfs(int x,int y)
{
     sz[x]=1;
     mxz[x]=0;
    st[0][x]=y;
    depth[x]=depth[y]+1;
    for(auto f:v[x])
    {
        if(f!=y)
        {
            dfs(f,x);
            sz[x]+=sz[f];
            if(sz[f]>sz[x])
                heavy[x]=f,mxz[x]=sz[f];
        }
    }
}
int lca2(int x,int y)
{
    if(depth[x]<depth[y]) swap(x,y);
    int ss=depth[x]-depth[y]-1;
    if(ss>=0)
        for(int j=0; j<17; ++j)
            if(bit(ss,j))
                x=st[j][x];
    if(st[0][x]==y) return x;
    if(ss!=-1)  x=st[0][x];
    for(int j=16; j>=0; --j)
        if(st[j][x]!=st[j][y])
            x=st[j][x],y=st[j][y];
    return x;
}
int lca(int x,int y)
{
    if(depth[x]<depth[y]) swap(x,y);
    int ss=depth[x]-depth[y];
    for(int j=0; j<17; ++j)
        if(bit(ss,j))
            x=st[j][x];
    if(x==y) return x;
    for(int j=16; j>=0; --j)
        if(st[j][x]!=st[j][y])
            x=st[j][x],y=st[j][y];
    return st[0][x];
}
void des(int x,int y)
{
    head[x]=y;
    pos[x]=++dem;
    if(heavy[x]!=0)
        des(heavy[x],y);
    for(auto f:v[x])
        if(pos[f]==0)
            des(f,f);
}
struct IT
{
    int t[480001];
    int t2[480001];
    void ff(int x)
    {
        for(int i=x*2; i<=x*2+1; i++)
            t2[i]+=t2[x];
        t2[x]=0;
    }
    void upd_in(int node,int l,int r,int l1,int r1)
    {
        if(l>r1||r<l1) return;
        if(l>=l1&&r<=r1)
        {
            t[node]=1;
            return;
        }
        int mid=l+r>>1;
        upd_in(node<<1,l,mid,l1,r1);
        upd_in(node<<1|1,mid+1,r,l1,r1);
        t[node]=t[node<<1]+t[node<<1|1];
    }
    int get(int node,int l,int r,int l1,int r1,int x)
    {
        if(l>r1||r<l1) return 0;
        if(l>=l1&&r<=r1)
        {
            v2[node].pb(x);
            t2[node]++;
            //   cout<<t2[node]<<" "<<node<<" "<<l<<" "<<r<<" "<<l1<<" "<<r1,down
            return t[node];
        }
        int mid=l+r>>1;
        return get(node<<1,l,mid,l1,r1,x)+get(node<<1|1,mid+1,r,l1,r1,x);
    }
    int get2(int node,int l,int r,int l1,int r1)
    {
        if(l>r1||r<l1) return 0;
        if(l>=l1&&r<=r1) return t2[node];
        ff(node);
        int mid=l+r>>1;
        return get2(node<<1,l,mid,l1,r1)+get2(node<<1|1,mid+1,r,l1,r1);

    }
    void setup(int node,int l,int r)
    {
        t[node]=0;
        if(l==r)
        {
            t2[node]--;
            if(t2[node]<=0)t2[node]=inf;
            f[l]=node;
            return;
        }
        ff(node);
        int mid=l+r>>1;
        setup(node<<1,l,mid);
        setup(node<<1|1,mid+1,r);
        t2[node]=min(t2[node<<1],t2[node<<1|1]);
    }
    void ff2(int x)
    {
        for(int i=x*2; i<=x*2+1; i++)
            t[i]+=t[x],
                  t2[i]-=t[x];
        t[x]=0;
    }
    void upd_out2(int node,int l,int r)
    {
        if(t2[node]!=0)return;
        if(l==r)
        {
            t2[node]=inf;
            c[vitri[l]]=0;
            if(b[vitri[l]]==1) s.push(vitri[l]);
            return;
        }
        ff2(node);
        int mid=l+r>>1;
        upd_out2(node<<1,l,mid);
        upd_out2(node<<1|1,mid+1,r);
        t2[node]=min(t2[node<<1],t2[node<<1|1]);
    }
    void upd_out(int node,int l,int r,int l1,int r1)
    {
        if(l>r1||r<l1) return;
        if(l>=l1&&r<=r1)
        {
            t2[node]--;
            t[node]++;
            if(t2[node]==0)ff2(node),upd_out2(node,l,r);
            return;
        }
        ff2(node);
        int mid=l+r>>1;
        upd_out(node<<1,l,mid,l1,r1);
        upd_out(node<<1|1,mid+1,r,l1,r1);
        t2[node]=min(t2[node<<1],t2[node<<1|1]);
    }
    int get_hld(int x,int y,int z)
    {
        if(pos[head[x]]<=pos[y]) return get(1,1,n,pos[y],pos[x],z);
        return get(1,1,n,pos[head[x]],pos[x],z)+get_hld(st[0][head[x]],y,z);
    }
    int get_namgiua(int x,int y,int z)
    {
        if(depth[x]>depth[y]) swap(x,y);
        s2=lca(x,y);
        s3=lca2(y,s2);
        return get_hld(x,s2,z)+get_hld(y,s3,z);
    }
    void ou(int x,int y)
    {
        if(pos[head[x]]<=pos[y])  upd_out(1,1,n,pos[y],pos[x]);
        else    upd_out(1,1,n,pos[head[x]],pos[x]),ou(st[0][head[x]],y);
    }
    void upd_out_out(int x,int y)
    {
        if(depth[x]>depth[y]) swap(x,y);
        s2=lca(x,y);
        s3=lca2(y,s2);
        ou(x,s2);
        ou(y,s3);
    }
    void upd_out_in(int x)
    {
        x=f[pos[x]];
        while(x!=0)
        {
            for(auto f:v2[x])
            {
                b[f]--;
                if(b[f]==1&&c[f]==0)s.push(f);
            }
            x/=2;
        }
    }
} stt;
void out(int x)
{
    stt.upd_out_out(a[x].a,a[x].b);
    stt.upd_out_in(a[x].a);
}
void phongbeo()
{
    cin>>n;
    /// cach 1
    dem=0;
    for(int i=1; i<=n; i++)
        v[i].clear(),b[i]=0,c[i]=0,f[i]=0,heavy[i]=0,pos[i]=0,head[i]=0,depth[i]=0,vitri[i]=0;
    for(int i=1; i<=n*4; i++)
        v2[i].clear(),stt.t[i]=0,stt.t2[i]=0;
    for(int i=1; i<n; i++)
        cin>>l>>r,v[l].pb(r),v[r].pb(l);
dfs(1,0);
  des(1,0);
    for(int i=1; i<17; i++)
        for(int j=1; j<=n; j++)
            st[i][j]=st[i-1][st[i-1][j]];

    cin>>m;
    for(int i=1; i<=m; i++)
        cin>>a[i].a>>a[i].b,stt.upd_in(1,1,n,pos[a[i].a],pos[a[i].a]);
    for(int i=1; i<=m; i++)
        b[i]=stt.get_namgiua(a[i].a,a[i].b,i);
    for(int i=1; i<=m; i++)
    {
        c[i]=stt.get2(1,1,n,pos[a[i].b],pos[a[i].b])-1;
        vitri[pos[a[i].b]]=i;
        if(b[i]==1&&c[i]==0)
        {
            s.push(i);
        }
    }
    b[0]=inf;
    stt.setup(1,1,n);
    dem4=0;
    while(!s.empty())
    {
        int  x=s.top();
        s.pop();
        out(x);
        dem4++;
    }
    if(dem4!=m) cout<<"No";
    else cout<<"Yes";
}

/**
7
1 2
2 3
3 4
4 5
3 6
6 7
2
4 1
5 7
*/

Compilation message

jail.cpp:50:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   50 | main()
      | ^~~~
jail.cpp: In member function 'void IT::upd_in(int, int, int, int, int)':
jail.cpp:149:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  149 |         int mid=l+r>>1;
      |                 ~^~
jail.cpp: In member function 'int IT::get(int, int, int, int, int, int)':
jail.cpp:164:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  164 |         int mid=l+r>>1;
      |                 ~^~
jail.cpp: In member function 'int IT::get2(int, int, int, int, int)':
jail.cpp:172:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  172 |         int mid=l+r>>1;
      |                 ~^~
jail.cpp: In member function 'void IT::setup(int, int, int)':
jail.cpp:187:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  187 |         int mid=l+r>>1;
      |                 ~^~
jail.cpp: In member function 'void IT::upd_out2(int, int, int)':
jail.cpp:210:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  210 |         int mid=l+r>>1;
      |                 ~^~
jail.cpp: In member function 'void IT::upd_out(int, int, int, int, int)':
jail.cpp:226:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  226 |         int mid=l+r>>1;
      |                 ~^~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 20824 KB Output is correct
2 Correct 3 ms 20828 KB Output is correct
3 Correct 2 ms 19036 KB Output is correct
4 Correct 11 ms 21096 KB Output is correct
5 Correct 20 ms 19064 KB Output is correct
6 Correct 4 ms 21080 KB Output is correct
7 Correct 4 ms 21080 KB Output is correct
8 Correct 8 ms 19036 KB Output is correct
9 Correct 1091 ms 34460 KB Output is correct
10 Execution timed out 5052 ms 286936 KB Time limit exceeded
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 19036 KB Output is correct
2 Correct 3 ms 19032 KB Output is correct
3 Correct 4 ms 19036 KB Output is correct
4 Correct 4 ms 19036 KB Output is correct
5 Correct 3 ms 21084 KB Output is correct
6 Correct 3 ms 19032 KB Output is correct
7 Correct 4 ms 19032 KB Output is correct
8 Correct 4 ms 20932 KB Output is correct
9 Correct 4 ms 21084 KB Output is correct
10 Correct 4 ms 21084 KB Output is correct
11 Correct 4 ms 19036 KB Output is correct
12 Correct 3 ms 19036 KB Output is correct
13 Correct 3 ms 21084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 19036 KB Output is correct
2 Correct 3 ms 19032 KB Output is correct
3 Correct 4 ms 19036 KB Output is correct
4 Correct 4 ms 19036 KB Output is correct
5 Correct 3 ms 21084 KB Output is correct
6 Correct 3 ms 19032 KB Output is correct
7 Correct 4 ms 19032 KB Output is correct
8 Correct 4 ms 20932 KB Output is correct
9 Correct 4 ms 21084 KB Output is correct
10 Correct 4 ms 21084 KB Output is correct
11 Correct 4 ms 19036 KB Output is correct
12 Correct 3 ms 19036 KB Output is correct
13 Correct 3 ms 21084 KB Output is correct
14 Correct 2 ms 19032 KB Output is correct
15 Correct 3 ms 20828 KB Output is correct
16 Correct 4 ms 19032 KB Output is correct
17 Correct 4 ms 21080 KB Output is correct
18 Correct 4 ms 21080 KB Output is correct
19 Correct 4 ms 20828 KB Output is correct
20 Correct 4 ms 21080 KB Output is correct
21 Correct 4 ms 21084 KB Output is correct
22 Correct 4 ms 19036 KB Output is correct
23 Correct 3 ms 19036 KB Output is correct
24 Correct 3 ms 21084 KB Output is correct
25 Correct 4 ms 19092 KB Output is correct
26 Correct 3 ms 19036 KB Output is correct
27 Correct 3 ms 19036 KB Output is correct
28 Correct 3 ms 19036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 19036 KB Output is correct
2 Correct 3 ms 19032 KB Output is correct
3 Correct 4 ms 19036 KB Output is correct
4 Correct 4 ms 19036 KB Output is correct
5 Correct 3 ms 21084 KB Output is correct
6 Correct 3 ms 19032 KB Output is correct
7 Correct 4 ms 19032 KB Output is correct
8 Correct 4 ms 20932 KB Output is correct
9 Correct 4 ms 21084 KB Output is correct
10 Correct 4 ms 21084 KB Output is correct
11 Correct 4 ms 19036 KB Output is correct
12 Correct 3 ms 19036 KB Output is correct
13 Correct 3 ms 21084 KB Output is correct
14 Correct 2 ms 19032 KB Output is correct
15 Correct 3 ms 20828 KB Output is correct
16 Correct 4 ms 19032 KB Output is correct
17 Correct 4 ms 21080 KB Output is correct
18 Correct 4 ms 21080 KB Output is correct
19 Correct 4 ms 20828 KB Output is correct
20 Correct 4 ms 21080 KB Output is correct
21 Correct 4 ms 21084 KB Output is correct
22 Correct 4 ms 19036 KB Output is correct
23 Correct 3 ms 19036 KB Output is correct
24 Correct 3 ms 21084 KB Output is correct
25 Correct 4 ms 19092 KB Output is correct
26 Correct 3 ms 19036 KB Output is correct
27 Correct 3 ms 19036 KB Output is correct
28 Correct 3 ms 19036 KB Output is correct
29 Correct 9 ms 19032 KB Output is correct
30 Correct 5 ms 21084 KB Output is correct
31 Correct 6 ms 21128 KB Output is correct
32 Correct 6 ms 19288 KB Output is correct
33 Correct 4 ms 19088 KB Output is correct
34 Correct 6 ms 20924 KB Output is correct
35 Correct 9 ms 19032 KB Output is correct
36 Correct 6 ms 21084 KB Output is correct
37 Correct 5 ms 21084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 19036 KB Output is correct
2 Correct 3 ms 19032 KB Output is correct
3 Correct 4 ms 19036 KB Output is correct
4 Correct 4 ms 19036 KB Output is correct
5 Correct 3 ms 21084 KB Output is correct
6 Correct 3 ms 19032 KB Output is correct
7 Correct 4 ms 19032 KB Output is correct
8 Correct 4 ms 20932 KB Output is correct
9 Correct 4 ms 21084 KB Output is correct
10 Correct 4 ms 21084 KB Output is correct
11 Correct 4 ms 19036 KB Output is correct
12 Correct 3 ms 19036 KB Output is correct
13 Correct 3 ms 21084 KB Output is correct
14 Correct 2 ms 19032 KB Output is correct
15 Correct 3 ms 20828 KB Output is correct
16 Correct 4 ms 19032 KB Output is correct
17 Correct 4 ms 21080 KB Output is correct
18 Correct 4 ms 21080 KB Output is correct
19 Correct 4 ms 20828 KB Output is correct
20 Correct 4 ms 21080 KB Output is correct
21 Correct 4 ms 21084 KB Output is correct
22 Correct 4 ms 19036 KB Output is correct
23 Correct 3 ms 19036 KB Output is correct
24 Correct 3 ms 21084 KB Output is correct
25 Correct 4 ms 19092 KB Output is correct
26 Correct 3 ms 19036 KB Output is correct
27 Correct 3 ms 19036 KB Output is correct
28 Correct 3 ms 19036 KB Output is correct
29 Correct 9 ms 19032 KB Output is correct
30 Correct 5 ms 21084 KB Output is correct
31 Correct 6 ms 21128 KB Output is correct
32 Correct 6 ms 19288 KB Output is correct
33 Correct 4 ms 19088 KB Output is correct
34 Correct 6 ms 20924 KB Output is correct
35 Correct 9 ms 19032 KB Output is correct
36 Correct 6 ms 21084 KB Output is correct
37 Correct 5 ms 21084 KB Output is correct
38 Correct 1113 ms 32612 KB Output is correct
39 Execution timed out 5065 ms 286936 KB Time limit exceeded
40 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 20828 KB Output is correct
2 Correct 4 ms 20828 KB Output is correct
3 Correct 3 ms 19032 KB Output is correct
4 Correct 3 ms 20828 KB Output is correct
5 Correct 10 ms 19232 KB Output is correct
6 Correct 3 ms 19036 KB Output is correct
7 Correct 3 ms 19036 KB Output is correct
8 Correct 3 ms 20828 KB Output is correct
9 Correct 3 ms 20828 KB Output is correct
10 Correct 3 ms 19036 KB Output is correct
11 Correct 3 ms 20828 KB Output is correct
12 Correct 5 ms 19092 KB Output is correct
13 Correct 25 ms 21084 KB Output is correct
14 Correct 49 ms 19032 KB Output is correct
15 Correct 36 ms 21084 KB Output is correct
16 Correct 102 ms 37628 KB Output is correct
17 Correct 207 ms 42832 KB Output is correct
18 Correct 393 ms 48316 KB Output is correct
19 Correct 194 ms 39184 KB Output is correct
20 Correct 158 ms 40788 KB Output is correct
21 Correct 187 ms 40984 KB Output is correct
22 Correct 221 ms 45860 KB Output is correct
23 Correct 153 ms 45636 KB Output is correct
24 Correct 316 ms 45900 KB Output is correct
25 Correct 334 ms 46040 KB Output is correct
26 Correct 352 ms 45892 KB Output is correct
27 Correct 296 ms 43108 KB Output is correct
28 Correct 184 ms 43184 KB Output is correct
29 Correct 162 ms 43180 KB Output is correct
30 Correct 185 ms 41936 KB Output is correct
31 Correct 138 ms 41788 KB Output is correct
32 Correct 191 ms 41936 KB Output is correct
33 Correct 120 ms 41936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 20824 KB Output is correct
2 Correct 3 ms 20828 KB Output is correct
3 Correct 2 ms 19036 KB Output is correct
4 Correct 11 ms 21096 KB Output is correct
5 Correct 20 ms 19064 KB Output is correct
6 Correct 4 ms 21080 KB Output is correct
7 Correct 4 ms 21080 KB Output is correct
8 Correct 8 ms 19036 KB Output is correct
9 Correct 1091 ms 34460 KB Output is correct
10 Execution timed out 5052 ms 286936 KB Time limit exceeded
11 Halted 0 ms 0 KB -