Submission #334865

# Submission time Handle Problem Language Result Execution time Memory
334865 2020-12-10T06:02:34 Z beksultan04 Birthday gift (IZhO18_treearray) C++14
0 / 100
10 ms 5100 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define OK puts("OK");
#define fr first
#define sc second
#define ret return
#define scan1(a) scanf("%lld",&a);
#define scan2(a,b) scanf("%lld %lld",&a, &b);
#define scan3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c);
#define all(s) s.begin(),s.end()
#define pb push_back
#define endi puts("");
const int N = 2e5+12,INF=1e18+7;
int q[N],up[N][40],tin[N],tout[N],cnt,der[4*N];
vector <int> g[N];

void dfs(int x,int p){
    int i;
    tin[x]=++cnt;
    up[x][0]=p;
    for (i=1;i<20;++i)
        up[x][i]= up[up[x][i-1]][i-1];

    for (i=0;i<g[x].size();++i){
        if (g[x][i] != p){
            dfs(g[x][i],x);
        }
    }
    tout[x] = ++cnt;
}

bool upper(int a,int b){
    ret (tin[a] <= tin[b] && tout[a] >= tout[b]);
}

int lca(int a,int b){
    int i;
    if (upper(a,b))ret a;
    if (upper(b,a))ret b;
    for(i=19;i>=0;--i){
        if (!upper(up[a][i],b)){
            a = up[a][i];
        }
    }
    ret up[a][0];
}

void build(int v,int l,int r){
    if (l == r){
        der[v] = q[l];
    }
    else {
        int m = l+r>>1;
        build(v<<1,l,m);
        build((v<<1)+1,m+1,r);
        der[v] = lca(der[v<<1],der[(v<<1)+1]);
    }
}
void update(int v,int l,int r,int x,int y){
    if (l == r){
        der[v]=y;
    }
    else {
        int m = l+r>>1;
        if (m < x){
            update((v<<1)+1,m+1,r,x,y);
        }
        else update(v<<1,l,m,x,y);

        der[v] = lca(der[v<<1],der[(v<<1)+1]);
    }
}

int get_ans(int v,int l,int r,int ql,int qr){
    if (ql <= l && r <= qr)ret der[v];
    if (ql > r || qr < l)ret 0;
    else {
        int m = l+r>>1;
        int x = get_ans(v<<1,l,m,ql,qr);
        int y = get_ans((v<<1)+1,m+1,r,ql,qr);
        if (x == 0)ret y;
        if (y == 0)ret x;
        ret lca(x,y);
    }
}

main(){
    int n,m,t,i,j,mm;
    scan3(n,m,t)
    for (i=1;i<n;++i){
        int x,y;
        scan2(x,y)
        g[x].pb(y);
        g[y].pb(x);
    }

    dfs(1,1);

    for (i=1;i<=m;++i)
        scan1(q[i])
    build(1,1,m);
    while (t--){
        int type;
        scan1(type)
        if (type==1){
            int l,r;
            scan2(l,r)
            update(1,1,m,l,r);
            q[l]=r;
        }
        else {
            int l,r,v,ll=-1,rr=-1;
            scan3(l,r,v)
            for (i=l;i<=r;++i){
                int lc = q[i];
                for (j=i;j<=r;++j){
                    int x =lca(lc,q[j]);
                    if (x == v){
                        ll = i;
                        rr = j;
                        break;
                    }
                }
            }
            cout <<ll<<" "<<rr<<"\n";
        }
    }
}






Compilation message

treearray.cpp: In function 'void dfs(long long int, long long int)':
treearray.cpp:26:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |     for (i=0;i<g[x].size();++i){
      |              ~^~~~~~~~~~~~
treearray.cpp: In function 'void build(long long int, long long int, long long int)':
treearray.cpp:55:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   55 |         int m = l+r>>1;
      |                 ~^~
treearray.cpp: In function 'void update(long long int, long long int, long long int, long long int, long long int)':
treearray.cpp:66:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   66 |         int m = l+r>>1;
      |                 ~^~
treearray.cpp: In function 'long long int get_ans(long long int, long long int, long long int, long long int, long long int)':
treearray.cpp:80:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   80 |         int m = l+r>>1;
      |                 ~^~
treearray.cpp: At global scope:
treearray.cpp:89:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   89 | main(){
      |      ^
treearray.cpp: In function 'int main()':
treearray.cpp:90:19: warning: unused variable 'mm' [-Wunused-variable]
   90 |     int n,m,t,i,j,mm;
      |                   ^~
treearray.cpp:11:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   11 | #define scan3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c);
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:91:5: note: in expansion of macro 'scan3'
   91 |     scan3(n,m,t)
      |     ^~~~~
treearray.cpp:10:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   10 | #define scan2(a,b) scanf("%lld %lld",&a, &b);
      |                    ~~~~~^~~~~~~~~~~~~~~~~~~~
treearray.cpp:94:9: note: in expansion of macro 'scan2'
   94 |         scan2(x,y)
      |         ^~~~~
treearray.cpp:9:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    9 | #define scan1(a) scanf("%lld",&a);
      |                  ~~~~~^~~~~~~~~~~
treearray.cpp:102:9: note: in expansion of macro 'scan1'
  102 |         scan1(q[i])
      |         ^~~~~
treearray.cpp:9:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    9 | #define scan1(a) scanf("%lld",&a);
      |                  ~~~~~^~~~~~~~~~~
treearray.cpp:106:9: note: in expansion of macro 'scan1'
  106 |         scan1(type)
      |         ^~~~~
treearray.cpp:10:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   10 | #define scan2(a,b) scanf("%lld %lld",&a, &b);
      |                    ~~~~~^~~~~~~~~~~~~~~~~~~~
treearray.cpp:109:13: note: in expansion of macro 'scan2'
  109 |             scan2(l,r)
      |             ^~~~~
treearray.cpp:11:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   11 | #define scan3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c);
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:115:13: note: in expansion of macro 'scan3'
  115 |             scan3(l,r,v)
      |             ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB n=5
2 Incorrect 10 ms 5100 KB Wrong range.
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB n=5
2 Incorrect 10 ms 5100 KB Wrong range.
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB n=5
2 Incorrect 10 ms 5100 KB Wrong range.
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB n=5
2 Incorrect 10 ms 5100 KB Wrong range.
3 Halted 0 ms 0 KB -