AtCoder Beginner Contest 014

Submission #7468803

Source codeソースコード

#include<bits/stdc++.h>
using namespace std;

#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define spa << " " <<
#define lfs <<fixed<<setprecision(10)<<
#define test cout<<"test"<<endl;
#define fi first
#define se second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = n; i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = n - 1; i >= (ll)(m); i--)
typedef long long ll;
typedef long double ld;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template<typename T>
void chmin(T &a,T b){if(a>b)a=b;}
template<typename T>
void chmax(T &a,T b){if(a<b)a=b;}
void pmod(ll &a,ll b){a=(a+b)%MOD;}
void pmod(ll &a,ll b,ll c){a=(b+c)%MOD;}
void qmod(ll &a,ll b){a=(a*b)%MOD;}
void qmod(ll &a,ll b,ll c){a=(b*c)%MOD;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>
void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}  
template<typename T>
void debug(vector<vector<T>>v,ll h,ll w){for(ll i=0;i<h;i++)
{cout<<v[i][0];for(ll j=1;j<w;j++)cout spa v[i][j];cout<<endl;}};
void debug(vector<string>v,ll h,ll w){for(ll i=0;i<h;i++)
{for(ll j=0;j<w;j++)cout<<v[i][j];cout<<endl;}};
template<typename T>
void debug(vector<T>v,ll n){cout<<v[0];
for(ll i=1;i<n;i++)cout spa v[i];cout<<endl;};
template<typename T>
vector<vector<T>>vec(ll x, ll y, T w){
  vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
template<typename T>
void emp(map<T,ll>&m, T x){m.emplace(x,0).first->second++;}
vector<ll>dx={1,0,-1,0,1,1,-1,-1};
vector<ll>dy={0,1,0,-1,1,-1,1,-1};

struct edge {
  ll to,cost;
  edge(ll x,ll y):to(x),cost(y){};
  edge():to(0LL),cost(0LL){};
};
//辺のオイラーツアー
struct Eulertour{
  ll n,m;
  ll root=0;
  vector<ll>vertex;//頂点列
  vector<pair<edge,ll>>data;//辺情報
  vector<ll>depth;//0から
  vector<vector<edge>>G;
  vector<pair<ll,ll>>index;//<最左index,最右index>
  Eulertour(vector<vector<edge>>g):G(g),n(g.size()){
    depth.assign(n,-1);
    index.assign(n,make_pair(10*n,10*n));
    dfs(root,0);
    m=vertex.size();
  }
  void dfs(ll k,ll d){
    depth[k]=d;
    for(ll i=0;i<G[k].size();i++){
      if(depth[G[k][i].to]==-1){
        chmin(index[k].fi,(ll)vertex.size());
        index[k].se = (ll)vertex.size();
        vertex.push_back(k);
        data.push_back(make_pair(G[k][i],1));

        dfs(G[k][i].to,d+1);

        chmin(index[G[k][i].to].fi,(ll)vertex.size());
        index[G[k][i].to].se = (ll)vertex.size();
        vertex.push_back(G[k][i].to);
        data.push_back(make_pair(G[k][i],-1));
      }
    }
    if(k==root){
      chmin(index[root].fi,(ll)vertex.size());
      index[root].se = (ll)vertex.size();
      vertex.push_back(root);
      data.push_back(MP(edge(),0));
    }
  }
  ll deep(ll x){return depth[x];}
};

template<typename T>
struct BIT{
  ll n;
  ll k=1;
  vector<T>data;
  BIT(ll size):n(size){
    data.assign(n,0);
    while(k*2<=n)k*=2;
  }
  void add(ll a,T w){
    for(ll i=a+1;i<=n;i+=i&-i)data[i-1]+=w;
  }
  T sum(ll a){
    T ret = 0;
    for(ll i=a+1;i>0;i-=i&-i)ret+=data[i-1];
    return ret;
  }
  T sum(ll a,ll b){return a==0?sum(b):sum(b)-sum(a-1);}
};

template<typename T>
struct SegmentTree{
  using F = function<T(T,T)>;
  vector<T> data;
  ll n,lastlen = 1;
  F func = [](T a, T b){return a < b ? a : b;};
  T iden = {LLONG_MAX,-1}; //identity element
  SegmentTree(vector<T> v){
    n = (ll)v.size();
    while(lastlen < n)lastlen *= 2;
    data.assign(lastlen*2-1,iden);
    for(ll i=0;i<n;i++)data[i+lastlen-1] = v[i];
    for(ll i=lastlen-2;i>=0;i--){
      data[i] = func(data[2*i+1], data[2*i+2]);
    }
  }
  void update(ll point, T x){
    point+=lastlen-1;
    data[point] = x;
    while(point!=0){
      point=(point-1)/2;
      data[point]=func(data[2*point+1],data[2*point+2]);
    }
  }
  T query(ll a,ll b,ll point=0,ll left=0,ll right=-1){
    if(right<0)right=lastlen;
    T ret = iden;
    if(b <= left || right <= a);
    else if(a <= left && right <= b ){
      ret = func(ret, data[point]);
    }
    else{
      T p,q;
      p = query(a,b,point*2+1,left, (left+right)/2);
      q = query(a,b,point*2+2,(left+right)/2, right);
      ret = func(ret,p);
      ret = func(ret,q);
    }      
    return ret;
  }
};

int main(){
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  ll res=0,res1=INF,res2=-INF,buf=0;
  bool judge = true;
  ll n;cin>>n;
  vector<vector<struct edge>>g(n);
  for(ll i=0;i<n-1;i++){
    ll u,v,c;
    cin>>u>>v;
    g[u-1].emplace_back(v-1,1LL);
    g[v-1].emplace_back(u-1,1LL);
  }
  Eulertour et(g);
  auto v=et.vertex;
  vector<ll>idx(n);
  rep(i,0,n)idx[i]=et.index[i].fi;
  struct BIT<ll>bit(et.m);
  vector<P>d(et.m);
  rep(i,0,et.m){
    d[i]={et.deep(v[i]),i};
  }
  SegmentTree<P>segtree(d);
  rep(i,0,et.m){
    ll x=et.data[i].fi.cost;
    ll y=et.data[i].se;
    bit.add(i,x*y);
  }
  //debug(v,et.m);
  //debug(idx,n);
  //debug(bit.data,et.m);
  ll q;cin>>q;
  rep(i,0,q){
    ll x,y;cin>>x>>y;
    x--;y--;
    //cout<<idx[x] spa idx[y]<<endl;
    if(idx[x]>idx[y])swap(x,y);
    ll k=segtree.query(idx[x],idx[y]+1).se;
    cout<<-bit.sum(idx[x],k-1)
    +bit.sum(k,idx[y]-1)+1<<endl;
  }

  return 0;
}

Submission

Task問題 D - 閉路
User nameユーザ名 tute7627
Created time投稿日時
Language言語 C++14 (GCC 5.4.1)
Status状態 AC
Score得点 100
Source lengthソースコード長 5722 Byte
File nameファイル名
Exec time実行時間 401 ms
Memory usageメモリ使用量 50552 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - subtask0_sample01.txt,subtask0_sample02.txt,subtask0_sample03.txt
Subtask1 30 / 30 subtask1_01.txt,subtask1_02.txt,subtask1_03.txt,subtask1_04.txt,subtask1_05.txt,subtask1_06.txt,subtask1_07.txt,subtask1_08.txt,subtask1_09.txt,subtask1_10.txt,subtask1_11.txt,subtask1_12.txt
Subtask2 70 / 70 subtask0_sample01.txt,subtask0_sample02.txt,subtask0_sample03.txt,subtask1_01.txt,subtask1_02.txt,subtask1_03.txt,subtask1_04.txt,subtask1_05.txt,subtask1_06.txt,subtask1_07.txt,subtask1_08.txt,subtask1_09.txt,subtask1_10.txt,subtask1_11.txt,subtask1_12.txt,subtask2_01.txt,subtask2_02.txt,subtask2_03.txt,subtask2_04.txt,subtask2_05.txt,subtask2_06.txt,subtask2_07.txt,subtask2_08.txt,subtask2_09.txt,subtask2_10.txt,subtask2_11.txt,subtask2_12.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
subtask0_sample01.txt AC 1 ms 256 KB
subtask0_sample02.txt AC 1 ms 256 KB
subtask0_sample03.txt AC 1 ms 256 KB
subtask1_01.txt AC 64 ms 49272 KB
subtask1_02.txt AC 65 ms 50552 KB
subtask1_03.txt AC 1 ms 256 KB
subtask1_04.txt AC 1 ms 256 KB
subtask1_05.txt AC 2 ms 640 KB
subtask1_06.txt AC 2 ms 768 KB
subtask1_07.txt AC 86 ms 43384 KB
subtask1_08.txt AC 86 ms 43000 KB
subtask1_09.txt AC 92 ms 44024 KB
subtask1_10.txt AC 88 ms 42744 KB
subtask1_11.txt AC 90 ms 42744 KB
subtask1_12.txt AC 104 ms 42744 KB
subtask2_01.txt AC 277 ms 50552 KB
subtask2_02.txt AC 321 ms 49528 KB
subtask2_03.txt AC 197 ms 512 KB
subtask2_04.txt AC 230 ms 512 KB
subtask2_05.txt AC 236 ms 1024 KB
subtask2_06.txt AC 241 ms 1024 KB
subtask2_07.txt AC 401 ms 43768 KB
subtask2_08.txt AC 380 ms 42744 KB
subtask2_09.txt AC 382 ms 43896 KB
subtask2_10.txt AC 380 ms 43512 KB
subtask2_11.txt AC 377 ms 42744 KB
subtask2_12.txt AC 377 ms 42744 KB