Submission #86963

# Submission time Handle Problem Language Result Execution time Memory
86963 2018-11-29T01:13:40 Z Kamisama Synchronization (JOI13_synchronization) C++14
100 / 100
271 ms 18696 KB
#include <bits/stdc++.h>
#include <unordered_map>
#define up(i,a,b) for(int i=a;i<=b;i++)
#define down(i,a,b) for(int i=a;i>=b;i--)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repd(i,a,b) for(int i=a-1;i>=b;i--)
#define self (*this)
#define pb push_back
#define CL(x) (x<<1)
#define CR(x) (x<<1|1)
#define x first
#define y second
#define Kamisama
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
 
const int maxn=1e5+7;
const int maxm=1e4+7;
const ll mod=123456789;
const int inf=1e9;
 
template<typename T> inline void Read(T &x){
    register char c;
    bool neg=false;
    for(c=getchar();c<'0'||c>'9';c=getchar()) if(c=='-') neg=true;
    for(x=0;c>='0'&&c<='9';c=getchar()) x=x*10+c-'0';
    if(neg) x=-x;
}
 
template<typename T> inline void Write(T x){
    if(x<0) putchar('-'), x=-x;
    if(x>9) Write(x/10);
    putchar(x%10+'0');
}
 
int n,m,q;
int in[maxn],out[maxn],pos[maxn];
int L[4*maxn],R[4*maxn],it[4*maxn];
int res[maxn],last[maxn];
bitset<maxn> avail;
struct Neko{int x,y;}e[maxn];
vector<int> adj[maxn];
 
inline void Enter(){
    Read(n); Read(m); Read(q);
    rep(i,1,n){
        Read(e[i].x); Read(e[i].y);
        adj[e[i].x].pb(e[i].y);
        adj[e[i].y].pb(e[i].x);
    }
}
 
inline void Build(const int &x, const int &low, const int &high){
    if(low>high) return;
    L[x]=low; R[x]=high;
    if(low==high){
        it[x]=out[pos[low]];
        return;
    }
    int mid=(low+high)>>1;
    Build(CL(x),low,mid);
    Build(CR(x),mid+1,high);
    it[x]=max(it[CL(x)],it[CR(x)]);
}
 
inline void Update(const int &x, const int &p, const int &v){
    if(L[x]>p || R[x]<p) return;
    if(L[x]==R[x]){
        it[x]=v;
        return;
    }
    Update(CL(x),p,v);
    Update(CR(x),p,v);
    it[x]=max(it[CL(x)],it[CR(x)]);
}
 
inline int Get(const int &x, const int &p, const int &v){
    if(L[x]>p || it[x]<v) return -1;
    if(L[x]==R[x]) return pos[L[x]];
    int childRight=Get(CR(x),p,v);
    if(childRight!=-1) return childRight;
    return Get(CL(x),p,v);
}
 
inline void Init(){
    function<void(int)> Dfs=[&](const int &u){
        static int nVisit=0;
        in[u]=++nVisit;
        pos[nVisit]=u;
        for(int v: adj[u]) if(!in[v]) Dfs(v);
        out[u]=nVisit;
    };
    Dfs(1);
    Build(1,1,n);
    fill(res,res+n+1,1);
}
 
inline void ChangeState(const int &eid, int u, int v){
    if(!avail[eid]){
        if(in[u]>in[v]) swap(u,v);
        int root=Get(1,in[u],out[u]);
        res[root]+=res[v]-last[v];
        Update(1,in[v],0);
        avail.flip(eid);
    } else{
        if(in[u]>in[v]) swap(u,v);
        int root=Get(1,in[u],out[u]);
        res[v]=last[v]=res[root];
        Update(1,in[v],out[v]);
        avail.flip(eid);
    }
}
 
inline void Solve(){
    for(int x;m;m--) Read(x),ChangeState(x,e[x].x,e[x].y);
    for(int x;q;q--) Read(x),Write(res[Get(1,in[x],out[x])]),putchar('\n');
}
 
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL),cout.tie(NULL);
    #undef Kamisama
    #ifdef Kamisama
    #define task "SYNCHRONIZATION"
    freopen(task".INP","r",stdin);
    freopen(task".OUT","w",stdout);
    #endif // Kamisama
//
 
    Enter();
    Init();
    Solve();
 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2804 KB Output is correct
3 Correct 4 ms 2804 KB Output is correct
4 Correct 4 ms 2840 KB Output is correct
5 Correct 4 ms 2900 KB Output is correct
6 Correct 5 ms 2976 KB Output is correct
7 Correct 18 ms 3872 KB Output is correct
8 Correct 26 ms 3888 KB Output is correct
9 Correct 23 ms 3888 KB Output is correct
10 Correct 192 ms 11988 KB Output is correct
11 Correct 205 ms 11988 KB Output is correct
12 Correct 209 ms 18096 KB Output is correct
13 Correct 152 ms 18096 KB Output is correct
14 Correct 161 ms 18096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 18096 KB Output is correct
2 Correct 108 ms 18096 KB Output is correct
3 Correct 92 ms 18096 KB Output is correct
4 Correct 92 ms 18096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 18096 KB Output is correct
2 Correct 4 ms 18096 KB Output is correct
3 Correct 4 ms 18096 KB Output is correct
4 Correct 4 ms 18096 KB Output is correct
5 Correct 4 ms 18096 KB Output is correct
6 Correct 5 ms 18096 KB Output is correct
7 Correct 19 ms 18096 KB Output is correct
8 Correct 201 ms 18428 KB Output is correct
9 Correct 218 ms 18428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 224 ms 18480 KB Output is correct
2 Correct 130 ms 18560 KB Output is correct
3 Correct 112 ms 18696 KB Output is correct
4 Correct 114 ms 18696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 18696 KB Output is correct
2 Correct 4 ms 18696 KB Output is correct
3 Correct 4 ms 18696 KB Output is correct
4 Correct 4 ms 18696 KB Output is correct
5 Correct 6 ms 18696 KB Output is correct
6 Correct 21 ms 18696 KB Output is correct
7 Correct 271 ms 18696 KB Output is correct
8 Correct 246 ms 18696 KB Output is correct
9 Correct 209 ms 18696 KB Output is correct
10 Correct 149 ms 18696 KB Output is correct
11 Correct 135 ms 18696 KB Output is correct
12 Correct 141 ms 18696 KB Output is correct
13 Correct 107 ms 18696 KB Output is correct