Submission #1084296

# Submission time Handle Problem Language Result Execution time Memory
1084296 2024-09-05T18:45:16 Z kokoue Birthday gift (IZhO18_treearray) C++14
12 / 100
4000 ms 11600 KB
#include<bits/stdc++.h>
#define maxn 100010
#define f first
#define s second
using namespace std;
int n,m,q;
vector<int> edges[maxn];
int st[25][maxn];
int timer=0;
int tin[maxn],tout[maxn],depth[maxn];
int seq[maxn];
int tree[4*maxn];
void dfs(int start,int par)
{
    st[0][start]=par;
    tin[start]=timer++;
    depth[start]=depth[par]+1;
    for(auto e:edges[start])
    {
        if(e!=par)
        {
            dfs(e,start);
        }
    }
    tout[start]=timer++;
}
bool is_ancestor(int u,int v)
{
    if(tin[u]<tin[v] && tout[u]>tout[v]) return true;
    return false;
}
int lca(int u,int v)
{
    if(u==v) return u;
    if(is_ancestor(u,v)) return u;
    if(is_ancestor(v,u)) return v;
    for(int i=24;i>=0;i--)
    {
        if(!is_ancestor(st[i][u],v))
        {
            u=st[i][u];
        }
    }
    u=st[0][u];
    return u;
}
void build(int node=1,int l=1,int r=m)
{
    if(l==r)
    {
        tree[node]=seq[l];
        return;
    }
    int mid=(l+r)/2;
    int left=node<<1,right=left+1;
    build(left,l,mid);
    build(right,mid+1,r);
    tree[node]=lca(tree[left],tree[right]);
}
void update(int idx,int val,int node=1,int l=1,int r=m)
{
    if(idx<l || r<idx) return;
    if(l==r)
    {
        tree[node]=val;
        return;
    }
    int mid=(l+r)/2;
    int left=node<<1,right=left+1;
    update(idx,val,left,l,mid);
    update(idx,val,right,mid+1,r);
    tree[node]=lca(tree[left],tree[right]);
}
int query(int ql,int qr,int node=1,int l=1,int r=m)
{
    if(qr<l || r<ql) return 0;
    if(ql<=l && r<=qr) return tree[node];
    int mid=(l+r)/2;
    int left=node<<1,right=left+1;
    int ans=query(ql,qr,left,l,mid);
    int rans=query(ql,qr,right,mid+1,r);
    if(ans==0) return rans;
    if(rans==0) return ans;
    return lca(ans,rans);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m>>q;
    for(int i=0;i<n-1;i++)
    {
        int a,b;
        cin>>a>>b;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }
    dfs(1,1);
    for(int i=1;i<25;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>>seq[i];
    }
    build();
    //printf("done building\n");
    //printf("query %d\n",query(1,4));
   // printf("query %d\n",query(2,2));
    for(int i=0;i<q;i++)
    {
        int t;
        cin>>t;
        if(t==1)
        {
            int idx,val;
            cin>>idx>>val;
            update(idx,val);
            //printf("done update\n");
        }
        if(t==2)
        {
            int l,r,hole;
            cin>>l>>r>>hole;
           // printf("query %d\n",query(2,2));
            int idxl=-1,idxr=-1;
            for(int j=l;j<=r;j++)
            {
                for(int h=j;h<=r;h++)
                {
                    if(query(j,h)==hole)
                    {
                        if(idxl==-1) {idxl=j;idxr=h;continue;}
                        if(idxr-idxl<h-j) {idxl=j;idxr=h;}
                    }
                }
            }
            cout<<idxl<<" "<<idxr<<"\n";
        }
    }
}
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
*/
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11352 KB n=5
2 Correct 10 ms 11352 KB n=100
3 Correct 12 ms 11356 KB n=100
4 Correct 10 ms 11352 KB n=100
5 Correct 10 ms 11352 KB n=100
6 Correct 52 ms 11356 KB n=100
7 Correct 51 ms 11456 KB n=100
8 Correct 6 ms 11356 KB n=100
9 Correct 8 ms 11356 KB n=100
10 Correct 28 ms 11356 KB n=100
11 Correct 27 ms 11356 KB n=100
12 Correct 7 ms 11356 KB n=100
13 Correct 10 ms 11352 KB n=100
14 Correct 7 ms 11352 KB n=100
15 Correct 6 ms 11352 KB n=100
16 Correct 9 ms 11484 KB n=100
17 Correct 9 ms 11356 KB n=100
18 Correct 7 ms 11356 KB n=100
19 Correct 10 ms 11356 KB n=100
20 Correct 6 ms 11352 KB n=100
21 Correct 7 ms 11356 KB n=100
22 Correct 8 ms 11356 KB n=100
23 Correct 8 ms 11352 KB n=100
24 Correct 9 ms 11356 KB n=100
25 Correct 14 ms 11356 KB n=100
26 Correct 2 ms 11352 KB n=12
27 Correct 23 ms 11356 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11352 KB n=5
2 Correct 10 ms 11352 KB n=100
3 Correct 12 ms 11356 KB n=100
4 Correct 10 ms 11352 KB n=100
5 Correct 10 ms 11352 KB n=100
6 Correct 52 ms 11356 KB n=100
7 Correct 51 ms 11456 KB n=100
8 Correct 6 ms 11356 KB n=100
9 Correct 8 ms 11356 KB n=100
10 Correct 28 ms 11356 KB n=100
11 Correct 27 ms 11356 KB n=100
12 Correct 7 ms 11356 KB n=100
13 Correct 10 ms 11352 KB n=100
14 Correct 7 ms 11352 KB n=100
15 Correct 6 ms 11352 KB n=100
16 Correct 9 ms 11484 KB n=100
17 Correct 9 ms 11356 KB n=100
18 Correct 7 ms 11356 KB n=100
19 Correct 10 ms 11356 KB n=100
20 Correct 6 ms 11352 KB n=100
21 Correct 7 ms 11356 KB n=100
22 Correct 8 ms 11356 KB n=100
23 Correct 8 ms 11352 KB n=100
24 Correct 9 ms 11356 KB n=100
25 Correct 14 ms 11356 KB n=100
26 Correct 2 ms 11352 KB n=12
27 Correct 23 ms 11356 KB n=100
28 Correct 690 ms 11356 KB n=500
29 Correct 729 ms 11356 KB n=500
30 Correct 639 ms 11504 KB n=500
31 Correct 663 ms 11512 KB n=500
32 Correct 774 ms 11352 KB n=500
33 Correct 714 ms 11352 KB n=500
34 Correct 766 ms 11500 KB n=500
35 Correct 668 ms 11600 KB n=500
36 Execution timed out 4051 ms 11496 KB Time limit exceeded
37 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11352 KB n=5
2 Correct 10 ms 11352 KB n=100
3 Correct 12 ms 11356 KB n=100
4 Correct 10 ms 11352 KB n=100
5 Correct 10 ms 11352 KB n=100
6 Correct 52 ms 11356 KB n=100
7 Correct 51 ms 11456 KB n=100
8 Correct 6 ms 11356 KB n=100
9 Correct 8 ms 11356 KB n=100
10 Correct 28 ms 11356 KB n=100
11 Correct 27 ms 11356 KB n=100
12 Correct 7 ms 11356 KB n=100
13 Correct 10 ms 11352 KB n=100
14 Correct 7 ms 11352 KB n=100
15 Correct 6 ms 11352 KB n=100
16 Correct 9 ms 11484 KB n=100
17 Correct 9 ms 11356 KB n=100
18 Correct 7 ms 11356 KB n=100
19 Correct 10 ms 11356 KB n=100
20 Correct 6 ms 11352 KB n=100
21 Correct 7 ms 11356 KB n=100
22 Correct 8 ms 11356 KB n=100
23 Correct 8 ms 11352 KB n=100
24 Correct 9 ms 11356 KB n=100
25 Correct 14 ms 11356 KB n=100
26 Correct 2 ms 11352 KB n=12
27 Correct 23 ms 11356 KB n=100
28 Correct 690 ms 11356 KB n=500
29 Correct 729 ms 11356 KB n=500
30 Correct 639 ms 11504 KB n=500
31 Correct 663 ms 11512 KB n=500
32 Correct 774 ms 11352 KB n=500
33 Correct 714 ms 11352 KB n=500
34 Correct 766 ms 11500 KB n=500
35 Correct 668 ms 11600 KB n=500
36 Execution timed out 4051 ms 11496 KB Time limit exceeded
37 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11352 KB n=5
2 Correct 10 ms 11352 KB n=100
3 Correct 12 ms 11356 KB n=100
4 Correct 10 ms 11352 KB n=100
5 Correct 10 ms 11352 KB n=100
6 Correct 52 ms 11356 KB n=100
7 Correct 51 ms 11456 KB n=100
8 Correct 6 ms 11356 KB n=100
9 Correct 8 ms 11356 KB n=100
10 Correct 28 ms 11356 KB n=100
11 Correct 27 ms 11356 KB n=100
12 Correct 7 ms 11356 KB n=100
13 Correct 10 ms 11352 KB n=100
14 Correct 7 ms 11352 KB n=100
15 Correct 6 ms 11352 KB n=100
16 Correct 9 ms 11484 KB n=100
17 Correct 9 ms 11356 KB n=100
18 Correct 7 ms 11356 KB n=100
19 Correct 10 ms 11356 KB n=100
20 Correct 6 ms 11352 KB n=100
21 Correct 7 ms 11356 KB n=100
22 Correct 8 ms 11356 KB n=100
23 Correct 8 ms 11352 KB n=100
24 Correct 9 ms 11356 KB n=100
25 Correct 14 ms 11356 KB n=100
26 Correct 2 ms 11352 KB n=12
27 Correct 23 ms 11356 KB n=100
28 Correct 690 ms 11356 KB n=500
29 Correct 729 ms 11356 KB n=500
30 Correct 639 ms 11504 KB n=500
31 Correct 663 ms 11512 KB n=500
32 Correct 774 ms 11352 KB n=500
33 Correct 714 ms 11352 KB n=500
34 Correct 766 ms 11500 KB n=500
35 Correct 668 ms 11600 KB n=500
36 Execution timed out 4051 ms 11496 KB Time limit exceeded
37 Halted 0 ms 0 KB -