Submission #1004943

# Submission time Handle Problem Language Result Execution time Memory
1004943 2024-06-22T04:44:08 Z modwwe Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 27228 KB
///https://www.instagram.com/_modwwe/
#pragma GCC optimize("O3")
#pragma GCC optimize("O1")
#pragma GCC optimize("O1")
#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx")
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,mmx,abm")
#include<bits/stdc++.h>
//#define int long long
//#define ll long long
#define down cout<<'\n';
#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".out","w",stdout)
#define pb push_back
#define checktime   cerr << (double)clock() / CLOCKS_PER_SEC * 1000  << " ms";
using namespace std;
void phongbeo();
const int mod2=1e9+7;
const int  mod1=998244353;
struct icd
{
    int a,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,f;

};
int n,m,s1,s2,s4,s3,sf,k,r,mid,s5,s6,mx,s7,s8,s9,mx2,res,dem2=0,dem=0,l;
int  i,s10,s12;
int el=29;
main()
{
#ifndef ONLINE_JUDGE
   // fin(task),fou(task);
#endif
    NHP
    /// cin>>s1;
    // modwwe
    phongbeo();
}
ic t[400007];
int depth[100007];
ic a[100007];
int c[100007];
ib euler[100007];
int d[100007];
int st[17][100007];
int ans[1000007];
int S;
int b[100007];
vector<int> v[100007];
int e[100007];
int lz[400007];
bool cmp(ic a,ic b)
{
    if(a.a/S==b.a/S) return a.b<b.b;
    return        a.a/S<b.a/S;
}
void build(int node,int l,int r)
{
    t[node]= {0,n+1,0};
    if(l==r)return;
    int mid=l+r>>1;
    build(node<<1,l,mid);
    build(node<<1|1,mid+1,r);
}

void upd(int node,int l,int r,int l1,int r1,int cc)
{
    if(l>r1||r<l1) return;
    if(l>=l1&&r<=r1)
    {
        t[node].c+=cc;
        if(t[node].c>0) t[node]= {l,r,t[node].c};
        else t[node]= {0,n+1,0};
        return;
    }
    int mid=l+r>>1;
    upd(node<<1,l,mid,l1,r1,cc);
    upd(node<<1|1,mid+1,r,l1,r1,cc);
    t[node].a=max(t[node<<1].a,t[node<<1|1].a);
    t[node].b=min(t[node<<1].b,t[node<<1|1].b);
}
int get1(int node,int l,int r,int l1,int r1)
{
    if(l>r1||r<l1) return 0;
    if(l>=l1&&r<=r1) return t[node].a;

    int mid=l+r>>1;
    return max(get1(node<<1,l,mid,l1,r1),get1(node<<1|1,mid+1,r,l1,r1));
}
int get2(int node, int l,int r,int l1,int r1)
{
    if(l>r1||r<l1) return n+1;
    if(l>=l1&&r<=r1) return t[node].b;
    int mid=l+r>>1;
    return min(get2(node<<1,l,mid,l1,r1),get2(node<<1|1,mid+1,r,l1,r1));
}
void dfs(int x,int y)
{
    st[0][x]=y;
    depth[x]=depth[y]+1;
    euler[x].a=++dem;
    c[dem]=x;
    for(auto f:v[x])
        if(f!=y)
            dfs(f,x);
    euler[x].b=dem;
}
int lca(int x,int y,int cc)
{
    if(x==0||x==n+1||y==0||y==n+1) return 1;
    if(depth[x]<depth[y]) swap(x,y),cc=0;
    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];
    if(cc==0) return y;
    else
        return x;
}
int lca2(int x,int y)
{
    if(x==0 )return y;
    if(y==0 )return x;
    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 build2(int node,int l,int r)
{
    if(l==r)
    {
        lz[node]=b[l];
        return;
    }
    int mid=l+r>>1;
    build2(node<<1,l,mid);
    build2(node<<1|1,mid+1,r);
    lz[node]=lca2(lz[node<<1],lz[node<<1|1]);
}
int get_lca(int node,int l,int r,int l1,int r1)
{
    if(l>r1||r<l1) return 0;
    if(l>=l1&&r<=r1) return lz[node];
    int mid=l+r>>1;
    return lca2(get_lca(node<<1,l,mid,l1,r1),get_lca(node<<1|1,mid+1,r,l1,r1));
}
void add(int x)
{
    s2=get1(1,1,n,1,euler[x].a);
    s3=get2(1,1,n,euler[x].a,n);
    upd(1,1,n,euler[x].a,euler[x].a,1);
    s2=c[s2];
    s3=c[s3];
    if(s2==x||s3==x) return;
    s2=lca(x,s2,1);
    s3=lca(x,s3,1);
    if(depth[s2]<depth[s3]) swap(s2,s3);
    if(depth[x]>=depth[s2])
    {
        s4+=depth[x]-depth[s2]+1;
        d[x]=s2;
        e[s2]=x;
    }
}
void del(int x)
{
    upd(1,1,n,euler[x].a,euler[x].a,-1);
    s5=d[x];
    if(d[x]==0) return;
    s2=get1(1,1,n,euler[s5].a,euler[x].a);
    s3=get2(1,1,n,euler[x].a,euler[s5].b);
    s2=c[s2];
    s3=c[s3];
    if(s2==x||s3==x) return;
    s4-=(depth[x]-depth[s5]+1);
    e[s5]=0;
    d[x]=0;
    if(s2==0&&s3==0) return;
    s9=s2;
    s2=lca(s2,x,1);
    s3=lca(s3,x,1);
    if(st[0][s2]==s9&&depth[s2]>=depth[s3]&&d[s2]==0)
    {
        s2=s9;
        s4+=depth[s2]-depth[s5]+1;
        d[s2]=s5;
        e[s5]=s2;
        return;
    }
    if(depth[s2]<depth[s3])swap(s2,s3);
    if(depth[s5]<depth[s2])
    {
        s3=e[s2];
        e[s2]=0;
        e[s5]=s3;
        d[s3]=s5;
        s4+=(depth[s2]-depth[s5]);
    }
}
void go(int x,int y)
{
    while(r<y)
    {
        r++;
        add(b[r]);
    }
    while(l>x)
    {
        l--;
        add(b[l]);
    }
    while(l<x)
    {
        del(b[l]);
        //   cout<<l<<" "<<s4,down
        l++;
    }
    while(r>y)
    {
        del(b[r]);
        r--;
    }

}
void phongbeo()
{
    cin>>n>>m>>k;
    S=sqrt(m);
    for(int i=1; i<n; i++)
        cin>>l>>r,v[l].pb(r),v[r].pb(l);
    dfs(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]];
    for(int i=1; i<=m; i++)
        cin>>b[i];
    for(int i=1; i<=k; i++)
        cin>>a[i].a>>a[i].b,a[i].c=i;
    sort(a+1,a+1+k,cmp);
    l=1;
    r=0;
    build(1,1,n);
    build2(1,1,m);
    for(int i=1; i<=k; i++)
    {
        go(a[i].a,a[i].b);
        s3=get_lca(1,1,m,a[i].a,a[i].b);
        ans[a[i].c]=s4+depth[1]-depth[s3];
    }
    for(int i=1; i<=k; i++)
        cout<<ans[i],down
    }
/*
20 20 3
1 2
1 3
1 4
3 5
5 6
1 7
5 8
8 9
1 10
6 11
10 12
10 13
5 14
8 15
1 16
5 17
11 18
7 19
15 20
17 5 11 11 10 5 18 6 5 13 3 8 4 19 14 10 17 4 11 1
16 18
8 19
20 20

*/

Compilation message

tourism.cpp:91:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   91 | main()
      | ^~~~
tourism.cpp: In function 'void build(int, int, int)':
tourism.cpp:123:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  123 |     int mid=l+r>>1;
      |             ~^~
tourism.cpp: In function 'void upd(int, int, int, int, int, int)':
tourism.cpp:138:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  138 |     int mid=l+r>>1;
      |             ~^~
tourism.cpp: In function 'int get1(int, int, int, int, int)':
tourism.cpp:149:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  149 |     int mid=l+r>>1;
      |             ~^~
tourism.cpp: In function 'int get2(int, int, int, int, int)':
tourism.cpp:156:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  156 |     int mid=l+r>>1;
      |             ~^~
tourism.cpp: In function 'void build2(int, int, int)':
tourism.cpp:210:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  210 |     int mid=l+r>>1;
      |             ~^~
tourism.cpp: In function 'int get_lca(int, int, int, int, int)':
tourism.cpp:219:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  219 |     int mid=l+r>>1;
      |             ~^~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 3 ms 14684 KB Output is correct
5 Correct 2 ms 14684 KB Output is correct
6 Correct 3 ms 14684 KB Output is correct
7 Correct 3 ms 14684 KB Output is correct
8 Correct 2 ms 14684 KB Output is correct
9 Correct 3 ms 14684 KB Output is correct
10 Correct 3 ms 14768 KB Output is correct
11 Correct 3 ms 14684 KB Output is correct
12 Correct 2 ms 14680 KB Output is correct
13 Correct 3 ms 14680 KB Output is correct
14 Correct 3 ms 14684 KB Output is correct
15 Correct 3 ms 14684 KB Output is correct
16 Correct 3 ms 14812 KB Output is correct
17 Correct 3 ms 14684 KB Output is correct
18 Correct 3 ms 14684 KB Output is correct
19 Correct 3 ms 14680 KB Output is correct
20 Correct 3 ms 14684 KB Output is correct
21 Correct 3 ms 14684 KB Output is correct
22 Correct 3 ms 14680 KB Output is correct
23 Correct 3 ms 14836 KB Output is correct
24 Correct 3 ms 14864 KB Output is correct
25 Correct 3 ms 14684 KB Output is correct
26 Correct 3 ms 14684 KB Output is correct
27 Correct 2 ms 14684 KB Output is correct
28 Correct 2 ms 14684 KB Output is correct
29 Correct 2 ms 14684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 3 ms 14684 KB Output is correct
5 Correct 2 ms 14684 KB Output is correct
6 Correct 3 ms 14684 KB Output is correct
7 Correct 3 ms 14684 KB Output is correct
8 Correct 2 ms 14684 KB Output is correct
9 Correct 3 ms 14684 KB Output is correct
10 Correct 3 ms 14768 KB Output is correct
11 Correct 3 ms 14684 KB Output is correct
12 Correct 2 ms 14680 KB Output is correct
13 Correct 3 ms 14680 KB Output is correct
14 Correct 3 ms 14684 KB Output is correct
15 Correct 3 ms 14684 KB Output is correct
16 Correct 3 ms 14812 KB Output is correct
17 Correct 3 ms 14684 KB Output is correct
18 Correct 3 ms 14684 KB Output is correct
19 Correct 3 ms 14680 KB Output is correct
20 Correct 3 ms 14684 KB Output is correct
21 Correct 3 ms 14684 KB Output is correct
22 Correct 3 ms 14680 KB Output is correct
23 Correct 3 ms 14836 KB Output is correct
24 Correct 3 ms 14864 KB Output is correct
25 Correct 3 ms 14684 KB Output is correct
26 Correct 3 ms 14684 KB Output is correct
27 Correct 2 ms 14684 KB Output is correct
28 Correct 2 ms 14684 KB Output is correct
29 Correct 2 ms 14684 KB Output is correct
30 Correct 20 ms 14940 KB Output is correct
31 Correct 23 ms 14936 KB Output is correct
32 Correct 33 ms 14820 KB Output is correct
33 Correct 32 ms 15044 KB Output is correct
34 Correct 32 ms 14936 KB Output is correct
35 Correct 7 ms 15052 KB Output is correct
36 Correct 10 ms 14960 KB Output is correct
37 Correct 8 ms 14940 KB Output is correct
38 Correct 24 ms 15092 KB Output is correct
39 Correct 24 ms 14940 KB Output is correct
40 Correct 23 ms 14940 KB Output is correct
41 Correct 6 ms 14940 KB Output is correct
42 Correct 7 ms 15104 KB Output is correct
43 Correct 7 ms 14940 KB Output is correct
44 Correct 33 ms 14940 KB Output is correct
45 Correct 25 ms 14940 KB Output is correct
46 Correct 25 ms 15056 KB Output is correct
47 Correct 7 ms 15060 KB Output is correct
48 Correct 7 ms 14916 KB Output is correct
49 Correct 7 ms 15076 KB Output is correct
50 Correct 29 ms 14940 KB Output is correct
51 Correct 29 ms 14940 KB Output is correct
52 Correct 39 ms 14936 KB Output is correct
53 Correct 39 ms 14940 KB Output is correct
54 Correct 29 ms 14936 KB Output is correct
55 Correct 29 ms 14940 KB Output is correct
56 Correct 4 ms 14684 KB Output is correct
57 Correct 3 ms 14940 KB Output is correct
58 Correct 5 ms 14940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 2 ms 14812 KB Output is correct
3 Correct 4 ms 14684 KB Output is correct
4 Execution timed out 5084 ms 23784 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 82 ms 20288 KB Output is correct
3 Correct 122 ms 20568 KB Output is correct
4 Correct 100 ms 22864 KB Output is correct
5 Correct 86 ms 24924 KB Output is correct
6 Correct 109 ms 24920 KB Output is correct
7 Correct 137 ms 24788 KB Output is correct
8 Correct 163 ms 24916 KB Output is correct
9 Correct 142 ms 24912 KB Output is correct
10 Correct 149 ms 24912 KB Output is correct
11 Correct 153 ms 24944 KB Output is correct
12 Correct 158 ms 24908 KB Output is correct
13 Correct 157 ms 25172 KB Output is correct
14 Correct 156 ms 25680 KB Output is correct
15 Correct 168 ms 27220 KB Output is correct
16 Correct 153 ms 25168 KB Output is correct
17 Correct 144 ms 25168 KB Output is correct
18 Correct 145 ms 25160 KB Output is correct
19 Correct 88 ms 24664 KB Output is correct
20 Correct 100 ms 24796 KB Output is correct
21 Correct 103 ms 24860 KB Output is correct
22 Correct 107 ms 24656 KB Output is correct
23 Correct 122 ms 24668 KB Output is correct
24 Correct 134 ms 24788 KB Output is correct
25 Correct 151 ms 24668 KB Output is correct
26 Correct 156 ms 24668 KB Output is correct
27 Correct 181 ms 24884 KB Output is correct
28 Correct 181 ms 24880 KB Output is correct
29 Correct 192 ms 24920 KB Output is correct
30 Correct 189 ms 24912 KB Output is correct
31 Correct 211 ms 24916 KB Output is correct
32 Correct 184 ms 25424 KB Output is correct
33 Correct 200 ms 26192 KB Output is correct
34 Correct 184 ms 27228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 4 ms 14684 KB Output is correct
4 Execution timed out 5094 ms 22624 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 3 ms 14684 KB Output is correct
5 Correct 2 ms 14684 KB Output is correct
6 Correct 3 ms 14684 KB Output is correct
7 Correct 3 ms 14684 KB Output is correct
8 Correct 2 ms 14684 KB Output is correct
9 Correct 3 ms 14684 KB Output is correct
10 Correct 3 ms 14768 KB Output is correct
11 Correct 3 ms 14684 KB Output is correct
12 Correct 2 ms 14680 KB Output is correct
13 Correct 3 ms 14680 KB Output is correct
14 Correct 3 ms 14684 KB Output is correct
15 Correct 3 ms 14684 KB Output is correct
16 Correct 3 ms 14812 KB Output is correct
17 Correct 3 ms 14684 KB Output is correct
18 Correct 3 ms 14684 KB Output is correct
19 Correct 3 ms 14680 KB Output is correct
20 Correct 3 ms 14684 KB Output is correct
21 Correct 3 ms 14684 KB Output is correct
22 Correct 3 ms 14680 KB Output is correct
23 Correct 3 ms 14836 KB Output is correct
24 Correct 3 ms 14864 KB Output is correct
25 Correct 3 ms 14684 KB Output is correct
26 Correct 3 ms 14684 KB Output is correct
27 Correct 2 ms 14684 KB Output is correct
28 Correct 2 ms 14684 KB Output is correct
29 Correct 2 ms 14684 KB Output is correct
30 Correct 20 ms 14940 KB Output is correct
31 Correct 23 ms 14936 KB Output is correct
32 Correct 33 ms 14820 KB Output is correct
33 Correct 32 ms 15044 KB Output is correct
34 Correct 32 ms 14936 KB Output is correct
35 Correct 7 ms 15052 KB Output is correct
36 Correct 10 ms 14960 KB Output is correct
37 Correct 8 ms 14940 KB Output is correct
38 Correct 24 ms 15092 KB Output is correct
39 Correct 24 ms 14940 KB Output is correct
40 Correct 23 ms 14940 KB Output is correct
41 Correct 6 ms 14940 KB Output is correct
42 Correct 7 ms 15104 KB Output is correct
43 Correct 7 ms 14940 KB Output is correct
44 Correct 33 ms 14940 KB Output is correct
45 Correct 25 ms 14940 KB Output is correct
46 Correct 25 ms 15056 KB Output is correct
47 Correct 7 ms 15060 KB Output is correct
48 Correct 7 ms 14916 KB Output is correct
49 Correct 7 ms 15076 KB Output is correct
50 Correct 29 ms 14940 KB Output is correct
51 Correct 29 ms 14940 KB Output is correct
52 Correct 39 ms 14936 KB Output is correct
53 Correct 39 ms 14940 KB Output is correct
54 Correct 29 ms 14936 KB Output is correct
55 Correct 29 ms 14940 KB Output is correct
56 Correct 4 ms 14684 KB Output is correct
57 Correct 3 ms 14940 KB Output is correct
58 Correct 5 ms 14940 KB Output is correct
59 Correct 2 ms 14684 KB Output is correct
60 Correct 2 ms 14812 KB Output is correct
61 Correct 4 ms 14684 KB Output is correct
62 Execution timed out 5084 ms 23784 KB Time limit exceeded
63 Halted 0 ms 0 KB -