Submission #1005101

# Submission time Handle Problem Language Result Execution time Memory
1005101 2024-06-22T07:11:53 Z modwwe Tourism (JOI23_tourism) C++17
35 / 100
5000 ms 29524 KB
///https://www.instagram.com/_modwwe/
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2")
#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,c;
};
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();
}
id 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];
int pre[100007];
int nxt[100007];
bool cmp(ic a,ic b)
{
    if(a.a / S != b.a / S) return (a.a < b.a);
	else if(!((a.a / S) & 1)) return (a.b < b.b);
	else return (a.b > b.b);
}
void upd(int node,int l,int r,int l1,int r1,int cc)
{
    if(l>=l1&&r<=r1)
    {
        t[node].c+=cc;
        s10=t[node].c;
        if(t[node].c>0) t[node]= {l,t[node].c};
        else t[node]= {0,0};
        return;
    }
    int mid=l+r>>1;
    if(mid>=r1)
    upd(node<<1,l,mid,l1,r1,cc);
    else
    upd(node<<1|1,mid+1,r,l1,r1,cc);
    t[node].a=max(t[node<<1].a,t[node<<1|1].a);
}
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));
}
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);
    if(d[x]!=0||s10>1) return;
    s2=c[s2];
    s3=nxt[s2];
    pre[x]=s2;
    nxt[x]=s3;
    nxt[s2]=x;
    pre[s3]=x;
//if(l==3&&x==1) cout<<s2<<" "<<s3<<" "<<x,down
    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];
    //s2=get1(1,1,n,euler[s5].a,euler[x].a);
    //s3=get2(1,1,n,euler[x].a,euler[s5].b);
    if(s10>=1) return;
    s2=pre[x];
    s3=nxt[x];
    nxt[s2]=s3;
    if(s3!=0) pre[s3]=s2;
    pre[x]=0;
    nxt[x]=0;
    s9=s2;
    if(d[x]==0)return;
    d[x]=0;
    e[s5]=0;
    s4-=(depth[x]-depth[s5]+1);
    s2=lca(s2,x,1);
    s3=lca(s3,x,1);
    if(max(depth[s2],depth[s3])<=depth[s5]) return;
    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]);
        //cout<<l<<" "<<s4,down

    }
    //cout<<s4<<" "<<x<<" "<<y,down
    while(l<x)
    {
        del(b[l]);
        l++;
    }
    while(r>y)
    {
        del(b[r]);

        r--;
    }

}
void phongbeo()
{
    cin>>n>>m>>k;
    S=400;
    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;
    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
    }
/*
8 8 2
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 6 4 3 5 2 4 7
2 8
1 2

*/

Compilation message

tourism.cpp:46:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   46 | main()
      | ^~~~
tourism.cpp: In function 'void upd(int, int, int, int, int, int)':
tourism.cpp:87:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   87 |     int mid=l+r>>1;
      |             ~^~
tourism.cpp: In function 'int get1(int, int, int, int, int)':
tourism.cpp:99:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   99 |     int mid=l+r>>1;
      |             ~^~
tourism.cpp: In function 'void build2(int, int, int)':
tourism.cpp:153:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  153 |     int mid=l+r>>1;
      |             ~^~
tourism.cpp: In function 'int get_lca(int, int, int, int, int)':
tourism.cpp:162:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  162 |     int mid=l+r>>1;
      |             ~^~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16732 KB Output is correct
2 Correct 2 ms 16732 KB Output is correct
3 Correct 2 ms 16732 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16900 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 3 ms 16732 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 4 ms 16732 KB Output is correct
10 Correct 4 ms 16732 KB Output is correct
11 Correct 4 ms 16732 KB Output is correct
12 Correct 2 ms 16732 KB Output is correct
13 Correct 2 ms 16728 KB Output is correct
14 Correct 2 ms 16732 KB Output is correct
15 Correct 3 ms 16732 KB Output is correct
16 Correct 3 ms 16732 KB Output is correct
17 Correct 3 ms 16732 KB Output is correct
18 Correct 3 ms 16732 KB Output is correct
19 Correct 3 ms 16732 KB Output is correct
20 Correct 3 ms 16732 KB Output is correct
21 Correct 3 ms 16732 KB Output is correct
22 Correct 3 ms 16732 KB Output is correct
23 Correct 4 ms 16732 KB Output is correct
24 Correct 5 ms 16732 KB Output is correct
25 Correct 4 ms 16732 KB Output is correct
26 Correct 4 ms 16732 KB Output is correct
27 Correct 2 ms 16732 KB Output is correct
28 Correct 2 ms 16732 KB Output is correct
29 Correct 2 ms 16732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16732 KB Output is correct
2 Correct 2 ms 16732 KB Output is correct
3 Correct 2 ms 16732 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16900 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 3 ms 16732 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 4 ms 16732 KB Output is correct
10 Correct 4 ms 16732 KB Output is correct
11 Correct 4 ms 16732 KB Output is correct
12 Correct 2 ms 16732 KB Output is correct
13 Correct 2 ms 16728 KB Output is correct
14 Correct 2 ms 16732 KB Output is correct
15 Correct 3 ms 16732 KB Output is correct
16 Correct 3 ms 16732 KB Output is correct
17 Correct 3 ms 16732 KB Output is correct
18 Correct 3 ms 16732 KB Output is correct
19 Correct 3 ms 16732 KB Output is correct
20 Correct 3 ms 16732 KB Output is correct
21 Correct 3 ms 16732 KB Output is correct
22 Correct 3 ms 16732 KB Output is correct
23 Correct 4 ms 16732 KB Output is correct
24 Correct 5 ms 16732 KB Output is correct
25 Correct 4 ms 16732 KB Output is correct
26 Correct 4 ms 16732 KB Output is correct
27 Correct 2 ms 16732 KB Output is correct
28 Correct 2 ms 16732 KB Output is correct
29 Correct 2 ms 16732 KB Output is correct
30 Correct 20 ms 16988 KB Output is correct
31 Correct 27 ms 16732 KB Output is correct
32 Correct 31 ms 16988 KB Output is correct
33 Correct 32 ms 16988 KB Output is correct
34 Correct 30 ms 16988 KB Output is correct
35 Correct 6 ms 16984 KB Output is correct
36 Correct 6 ms 16904 KB Output is correct
37 Correct 6 ms 16988 KB Output is correct
38 Correct 23 ms 16988 KB Output is correct
39 Correct 23 ms 16988 KB Output is correct
40 Correct 24 ms 16984 KB Output is correct
41 Correct 6 ms 16988 KB Output is correct
42 Correct 6 ms 16988 KB Output is correct
43 Correct 6 ms 16988 KB Output is correct
44 Correct 24 ms 16988 KB Output is correct
45 Correct 32 ms 17020 KB Output is correct
46 Correct 25 ms 17016 KB Output is correct
47 Correct 6 ms 16988 KB Output is correct
48 Correct 6 ms 17012 KB Output is correct
49 Correct 6 ms 16988 KB Output is correct
50 Correct 29 ms 16984 KB Output is correct
51 Correct 27 ms 16988 KB Output is correct
52 Correct 28 ms 16988 KB Output is correct
53 Correct 28 ms 16988 KB Output is correct
54 Correct 28 ms 16988 KB Output is correct
55 Correct 28 ms 16984 KB Output is correct
56 Correct 4 ms 16732 KB Output is correct
57 Correct 2 ms 16988 KB Output is correct
58 Correct 5 ms 16956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16732 KB Output is correct
2 Correct 2 ms 16732 KB Output is correct
3 Correct 4 ms 16892 KB Output is correct
4 Correct 3286 ms 22080 KB Output is correct
5 Correct 2069 ms 26964 KB Output is correct
6 Correct 2811 ms 27764 KB Output is correct
7 Correct 4693 ms 29524 KB Output is correct
8 Correct 4688 ms 29436 KB Output is correct
9 Correct 4827 ms 29452 KB Output is correct
10 Correct 4513 ms 29444 KB Output is correct
11 Correct 4432 ms 29520 KB Output is correct
12 Correct 992 ms 29224 KB Output is correct
13 Correct 988 ms 29228 KB Output is correct
14 Correct 1014 ms 29268 KB Output is correct
15 Correct 37 ms 25424 KB Output is correct
16 Correct 167 ms 29012 KB Output is correct
17 Correct 159 ms 18524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16728 KB Output is correct
2 Correct 92 ms 20044 KB Output is correct
3 Correct 132 ms 20204 KB Output is correct
4 Correct 111 ms 21364 KB Output is correct
5 Correct 70 ms 22608 KB Output is correct
6 Correct 81 ms 22608 KB Output is correct
7 Correct 94 ms 22612 KB Output is correct
8 Correct 93 ms 22556 KB Output is correct
9 Correct 102 ms 22612 KB Output is correct
10 Correct 114 ms 22552 KB Output is correct
11 Correct 164 ms 22608 KB Output is correct
12 Correct 164 ms 22700 KB Output is correct
13 Correct 187 ms 22612 KB Output is correct
14 Correct 161 ms 22724 KB Output is correct
15 Correct 178 ms 22856 KB Output is correct
16 Correct 123 ms 22616 KB Output is correct
17 Correct 126 ms 22608 KB Output is correct
18 Correct 123 ms 22704 KB Output is correct
19 Correct 65 ms 22620 KB Output is correct
20 Correct 69 ms 22496 KB Output is correct
21 Correct 76 ms 22608 KB Output is correct
22 Correct 83 ms 22608 KB Output is correct
23 Correct 88 ms 22612 KB Output is correct
24 Correct 100 ms 22644 KB Output is correct
25 Correct 113 ms 22640 KB Output is correct
26 Correct 139 ms 22664 KB Output is correct
27 Correct 186 ms 22612 KB Output is correct
28 Correct 223 ms 22516 KB Output is correct
29 Correct 236 ms 22652 KB Output is correct
30 Correct 247 ms 22660 KB Output is correct
31 Correct 255 ms 22608 KB Output is correct
32 Correct 252 ms 22612 KB Output is correct
33 Correct 236 ms 22612 KB Output is correct
34 Correct 224 ms 22864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16732 KB Output is correct
2 Correct 2 ms 16732 KB Output is correct
3 Correct 4 ms 16732 KB Output is correct
4 Execution timed out 5088 ms 20636 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16732 KB Output is correct
2 Correct 2 ms 16732 KB Output is correct
3 Correct 2 ms 16732 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16900 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 3 ms 16732 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 4 ms 16732 KB Output is correct
10 Correct 4 ms 16732 KB Output is correct
11 Correct 4 ms 16732 KB Output is correct
12 Correct 2 ms 16732 KB Output is correct
13 Correct 2 ms 16728 KB Output is correct
14 Correct 2 ms 16732 KB Output is correct
15 Correct 3 ms 16732 KB Output is correct
16 Correct 3 ms 16732 KB Output is correct
17 Correct 3 ms 16732 KB Output is correct
18 Correct 3 ms 16732 KB Output is correct
19 Correct 3 ms 16732 KB Output is correct
20 Correct 3 ms 16732 KB Output is correct
21 Correct 3 ms 16732 KB Output is correct
22 Correct 3 ms 16732 KB Output is correct
23 Correct 4 ms 16732 KB Output is correct
24 Correct 5 ms 16732 KB Output is correct
25 Correct 4 ms 16732 KB Output is correct
26 Correct 4 ms 16732 KB Output is correct
27 Correct 2 ms 16732 KB Output is correct
28 Correct 2 ms 16732 KB Output is correct
29 Correct 2 ms 16732 KB Output is correct
30 Correct 20 ms 16988 KB Output is correct
31 Correct 27 ms 16732 KB Output is correct
32 Correct 31 ms 16988 KB Output is correct
33 Correct 32 ms 16988 KB Output is correct
34 Correct 30 ms 16988 KB Output is correct
35 Correct 6 ms 16984 KB Output is correct
36 Correct 6 ms 16904 KB Output is correct
37 Correct 6 ms 16988 KB Output is correct
38 Correct 23 ms 16988 KB Output is correct
39 Correct 23 ms 16988 KB Output is correct
40 Correct 24 ms 16984 KB Output is correct
41 Correct 6 ms 16988 KB Output is correct
42 Correct 6 ms 16988 KB Output is correct
43 Correct 6 ms 16988 KB Output is correct
44 Correct 24 ms 16988 KB Output is correct
45 Correct 32 ms 17020 KB Output is correct
46 Correct 25 ms 17016 KB Output is correct
47 Correct 6 ms 16988 KB Output is correct
48 Correct 6 ms 17012 KB Output is correct
49 Correct 6 ms 16988 KB Output is correct
50 Correct 29 ms 16984 KB Output is correct
51 Correct 27 ms 16988 KB Output is correct
52 Correct 28 ms 16988 KB Output is correct
53 Correct 28 ms 16988 KB Output is correct
54 Correct 28 ms 16988 KB Output is correct
55 Correct 28 ms 16984 KB Output is correct
56 Correct 4 ms 16732 KB Output is correct
57 Correct 2 ms 16988 KB Output is correct
58 Correct 5 ms 16956 KB Output is correct
59 Correct 2 ms 16732 KB Output is correct
60 Correct 2 ms 16732 KB Output is correct
61 Correct 4 ms 16892 KB Output is correct
62 Correct 3286 ms 22080 KB Output is correct
63 Correct 2069 ms 26964 KB Output is correct
64 Correct 2811 ms 27764 KB Output is correct
65 Correct 4693 ms 29524 KB Output is correct
66 Correct 4688 ms 29436 KB Output is correct
67 Correct 4827 ms 29452 KB Output is correct
68 Correct 4513 ms 29444 KB Output is correct
69 Correct 4432 ms 29520 KB Output is correct
70 Correct 992 ms 29224 KB Output is correct
71 Correct 988 ms 29228 KB Output is correct
72 Correct 1014 ms 29268 KB Output is correct
73 Correct 37 ms 25424 KB Output is correct
74 Correct 167 ms 29012 KB Output is correct
75 Correct 159 ms 18524 KB Output is correct
76 Correct 2 ms 16728 KB Output is correct
77 Correct 92 ms 20044 KB Output is correct
78 Correct 132 ms 20204 KB Output is correct
79 Correct 111 ms 21364 KB Output is correct
80 Correct 70 ms 22608 KB Output is correct
81 Correct 81 ms 22608 KB Output is correct
82 Correct 94 ms 22612 KB Output is correct
83 Correct 93 ms 22556 KB Output is correct
84 Correct 102 ms 22612 KB Output is correct
85 Correct 114 ms 22552 KB Output is correct
86 Correct 164 ms 22608 KB Output is correct
87 Correct 164 ms 22700 KB Output is correct
88 Correct 187 ms 22612 KB Output is correct
89 Correct 161 ms 22724 KB Output is correct
90 Correct 178 ms 22856 KB Output is correct
91 Correct 123 ms 22616 KB Output is correct
92 Correct 126 ms 22608 KB Output is correct
93 Correct 123 ms 22704 KB Output is correct
94 Correct 65 ms 22620 KB Output is correct
95 Correct 69 ms 22496 KB Output is correct
96 Correct 76 ms 22608 KB Output is correct
97 Correct 83 ms 22608 KB Output is correct
98 Correct 88 ms 22612 KB Output is correct
99 Correct 100 ms 22644 KB Output is correct
100 Correct 113 ms 22640 KB Output is correct
101 Correct 139 ms 22664 KB Output is correct
102 Correct 186 ms 22612 KB Output is correct
103 Correct 223 ms 22516 KB Output is correct
104 Correct 236 ms 22652 KB Output is correct
105 Correct 247 ms 22660 KB Output is correct
106 Correct 255 ms 22608 KB Output is correct
107 Correct 252 ms 22612 KB Output is correct
108 Correct 236 ms 22612 KB Output is correct
109 Correct 224 ms 22864 KB Output is correct
110 Correct 2 ms 16732 KB Output is correct
111 Correct 2 ms 16732 KB Output is correct
112 Correct 4 ms 16732 KB Output is correct
113 Execution timed out 5088 ms 20636 KB Time limit exceeded
114 Halted 0 ms 0 KB -