답안 #992653

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
992653 2024-06-04T23:57:43 Z Savu_Stefan_Catalin Prize (CEOI22_prize) C++14
0 / 100
1169 ms 240384 KB
#include <iostream>
#include <algorithm>
#include <vector>
#define int long long
using namespace std;
const int NMAX=1e6+505;
int cnt,ine[2][NMAX+5],lv[NMAX+5],ar[4*NMAX+505],n,t[2][NMAX+5],cen[2],invine[2][NMAX+5],k,q,T,id[NMAX+5],dp[NMAX+5],rmq[25][NMAX+5],imp[NMAX+5],invid[NMAX+5],rr[2][NMAX+5];
vector <int> v[2][NMAX+5];
pair<pair<int,int>,pair<int,int>> ras[NMAX+5];
void dfs(int nod,int tata,int ind)
{
    ++cnt;
    ine[ind][nod]=cnt;
    if (ind==0) lv[nod]=lv[tata]+1;
    for (int i=0;i<v[ind][nod].size();++i)
    {
        if (v[ind][nod][i]!=tata)
        {
            dfs(v[ind][nod][i],nod,ind);
        }
    }
}
void read(int ind)
{
    for (int i=1;i<=n;++i)
    {
        cin>>t[ind][i];
        if (t[ind][i]==-1) cen[ind]=i;
        else {v[ind][t[ind][i]].push_back(i);}
    }
    cnt=0;
    dfs(cen[ind],0,ind);
    for (int i=1;i<=n;++i)
    {
        invine[ind][ine[ind][i]]=i;
    }
}
int cmp(int x,int y)
{
    return ine[1][x]<ine[1][y];
}
void update(int st,int dr,int nod,int poz,int val)
{
    if (st==dr)
    {
        ar[nod]=val;
        return ;
    }
    int mij=(st+dr)/2;
    if (poz<=mij) update(st,mij,nod*2,poz,val);
    else update(mij+1,dr,nod*2+1,poz,val);
    ar[nod]=ar[nod*2]+ar[nod*2+1];
}
int query(int st,int dr,int nod,int qa,int qb)
{
    if (qa<=st&&dr<=qb)
    {
        return ar[nod];
    }
    int mij=(st+dr)/2,ras1=0,ras2=0;
    if (qa<=mij) ras1=query(st,mij,nod*2,qa,qb);
    if (qb>mij) ras2=query(mij+1,dr,nod*2+1,qa,qb);
    return ras1+ras2;
}
void solve1()
{
    for (int i=2;i<k;++i)
    {
        update(1,k,1,i,ras[i].second.first-ras[i-1].second.second);
    }
}
void solve0()
{
    int ii=0;
    for (int i=1;i<=k;++i)
    {
        if (id[i]==cen[0]) {ii=i;}
    }
    dp[ii]=0;
    for (int i=ii+1;i<=k;++i)
    {
        dp[i]=dp[i-1]-ras[i-1].first.first+ras[i-1].first.second;
    }
    for (int i=ii-1;i>=1;--i)
    {
        dp[i]=dp[i+1]-ras[i].first.second+ras[i].first.first;
    }
}
void makermq()
{
    for (int i=1;i<=n;++i)
    {
        if (t[0][i]==-1) rmq[0][i]=0;
        else rmq[0][i]=t[0][i];
    }
    for (int j=1;j<=20;++j)
    {
        for (int i=1;i<=n;++i)
        {
            rmq[j][i]=rmq[j-1][rmq[j-1][i]];
        }
    }
}
int lca(int x,int y)
{
    x=id[x];
    y=id[y];
    if (lv[x]>lv[y]) swap(x,y);
    for (int i=20;i>=0;--i) {if (lv[x]+(1<<i)<=lv[y]) {y=rmq[i][y];}}
    if (x==y) return invid[x];
    for (int i=20;i>=0;--i) {if (rmq[i][x]!=rmq[i][y]) {x=rmq[i][x]; y=rmq[i][y];}}
    return invid[x];
}
signed main()
{
    cin>>n>>k>>q>>T;
    read(0);
    read(1);
    for (int i=1;i<=k;++i)
    {
        imp[i]=invine[0][i];
        cout<<imp[i]<<" ";
        cout.flush();
    }
    for (int i=1;i<=k;++i)
    {
        id[i]=imp[i];
    }
   // sort(id+1,id+k+1,cmp);
    for (int i=1;i<=k;++i)
    {
        invid[id[i]]=i;
    }
    for (int i=1;i<k;++i)
    {
        cout<<"? "<<id[i]<<" "<<id[i+1]<<'\n';
    }
    cout<<"!";
    cout.flush();
    for (int i=1;i<k;++i)
    {
        cin>>ras[i].first.first>>ras[i].first.second>>ras[i].second.first>>ras[i].second.second;
    }
    solve0();
    //solve1();
    makermq();
    for (int i=1;i<=T;++i)
    {
        int x,y;
        cin>>x>>y;
        x=invid[x];
        y=invid[y];
        if (x>y) swap(x,y);
   //     rr[1][i]=ras[x].second.first+ras[y-1].second.second+((x>=y-1) ? 0 : query(1,k,1,x+1,y-1));
        rr[0][i]=dp[x]+dp[y]-2*dp[lca(x,y)];
    }
    for (int i=1;i<=T;++i)
        cout<<rr[0][i]<<" "<<rr[0][i]<<'\n';
    cout.flush();
    return 0;
}

Compilation message

Main.cpp: In function 'void dfs(long long int, long long int, long long int)':
Main.cpp:15:19: 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]
   15 |     for (int i=0;i<v[ind][nod].size();++i)
      |                  ~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 545 ms 163832 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 514 ms 163952 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 464 ms 163580 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1164 ms 234352 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1169 ms 240384 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -