Submission #603085

# Submission time Handle Problem Language Result Execution time Memory
603085 2022-07-23T15:05:17 Z achibasadzishvili Werewolf (IOI18_werewolf) C++14
100 / 100
962 ms 150424 KB
#include<bits/stdc++.h>
#include "werewolf.h"
#define ll int
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define maxn 400005
using namespace std;
ll m,n,p[maxn],in1[maxn],out1[maxn],in2[maxn],out2[maxn],ans[maxn];
ll tout1[maxn],tout2[maxn],tin1[maxn],tin2[maxn],s[maxn],t[maxn],l[maxn],r[maxn];
ll P1[200005][19],P2[200005][19],q,was[maxn],pown = 1;
vector<ll>v[maxn],ne[maxn],ol[maxn],be[maxn],ed[maxn];
vector<pair<ll,ll> >g;
ll tt[6000005];
ll timer = 0;
ll find(ll x){
	if(p[x] == x)return x;
	return p[x] = find(p[x]);
}
void join(ll x,ll y){
	x = find(x);
	y = find(y);
	if(x == y)return;
    if(y > x)swap(x,y);
	p[y] = x;
}
void join1(ll x,ll y){
	x = find(x);
	y = find(y);
	if(x == y)return;
	if(y < x)swap(x , y);
	p[y] = x;
}
void dfs(ll x, ll par){
    timer++;
    in1[x] = timer;
    tin1[timer] = x;
    
    P1[x][0] = par;
    for(int i=1;i<=18;i++)
        P1[x][i] = P1[P1[x][i-1]][i-1];
    
    for(int i=0; i<ne[x].size(); i++)
        if(ne[x][i] != par)
            dfs(ne[x][i],x);
    timer++;
    out1[x] = timer;
    tout1[timer] = x;
}
void dfs1(ll x,ll par){
    timer++;
    in2[x] = timer;
    tin2[timer] = x;
    P2[x][0] = par;
    for(int i=1;i<=18;i++)
        P2[x][i] = P2[P2[x][i-1]][i-1];
        
    for(int i=0; i<ol[x].size(); i++)
        if(ol[x][i] != par)
            dfs1(ol[x][i],x);
    timer++;
    out2[x] = timer;
    tout2[timer] = x;
}
ll bigne(ll x,ll y){
    for(int i=18; i>=0; i--)
        if(P1[x][i])
            if(P1[x][i] <= y)
                x = P1[x][i];
    return x;
}
ll bigol(ll x,ll y){
    for(int i=18; i>=0; i--)
        if(P2[x][i])
            if(P2[x][i] >= y)
                x = P2[x][i];
    return x;
}
void upd(ll x){
	if(!x)return;
	tt[x] = tt[2 * x] + tt[2 * x + 1];
	upd(x / 2);
}
ll cnt(ll x,ll L,ll R,ll l1,ll r1){
	if(L > r1 || R < l1)return 0;
	if(L >= l1 && R <= r1)return tt[x];
	ll k1 = cnt(2 * x,L , (L + R) / 2 , l1 , r1);
	ll k2 = cnt(2 * x + 1,(L + R) / 2 + 1 , R , l1 , r1);
	return k1 + k2;
}

std::vector<int> check_validity(int N, std::vector<int> X, std::vector<int> Y,
                                std::vector<int> S, std::vector<int> E,
                            std::vector<int> L, std::vector<int> R) {
    n = N;
    while(pown <= 2 * n)pown *= 2;
    m = X.size();
    
    for(int i=1; i<=m; i++){
        ll x,y;
        x = X[i - 1];
        y = Y[i - 1];
		x++;
		y++;
        v[x].pb(y);
        v[y].pb(x);
        g.pb(mp(max(x,y) , min(x,y)));
    }
    
    
  int Q = S.size();
    q = Q;
    for(int i=1; i<=q; i++){
        s[i] = S[i - 1];
        t[i] = E[i - 1];
        l[i] = L[i - 1];
        r[i] = R[i - 1];
		s[i]++;
		t[i]++;
		l[i]++;
		r[i]++;
    }
    
    sort(g.begin(),g.end());
    
    for(int i=1; i<=n; i++){
        p[i] = i;
    }
    
    for(int i=0; i<g.size(); i++){
        ll x,y;
        x = g[i].f;
        y = g[i].s;
        x = find(x);
        y = find(y);
        if(x != y){
            join(x,y);
            ne[x].pb(y);
            ne[y].pb(x);
        }
    }
    
	for(int i=0; i<g.size(); i++){
		swap(g[i].f , g[i].s);
	}
	
	sort(g.begin() , g.end());
	
	reverse(g.begin() , g.end());
    
    for(int i=1; i<=n; i++){
        p[i] = i;
    }
	
    for(int i=0; i<g.size(); i++){
        ll x,y;
        x = g[i].f;
        y = g[i].s;
        x = find(x);
        y = find(y);
        if(x != y){
            join1(x,y);
            ol[x].pb(y);
            ol[y].pb(x);
        }
    }
    
    dfs(n,0);
    
    timer = 0;
    
    dfs1(1,0);
    
    for(int i=1; i<=q; i++){
        s[i] = bigol(s[i] , l[i]);
        t[i] = bigne(t[i] , r[i]);
		be[in2[s[i]]].pb(i);
		ed[out2[s[i]]].pb(i);
	}
    
	
	for(int i=1; i<=2 * n; i++){
		for(int j=0; j<be[i].size(); j++)
			was[be[i][j]] = cnt(1,1,pown,in1[t[be[i][j]]] , out1[t[be[i][j]]]);
		if(tin2[i] != 0){
			tt[pown + in1[tin2[i]] - 1]++;
			upd((pown + in1[tin2[i]] - 1) / 2);
		}
		
		for(int j=0; j < ed[i].size(); j++){
			ll now = cnt(1,1,pown,in1[t[ed[i][j]]] , out1[t[ed[i][j]]] );
			if(now > was[ed[i][j]])
				ans[ed[i][j]] = 1;
		}
	}

  std::vector<int> A(Q);
  for (int i = 0; i < Q; ++i)
    A[i] = ans[i + 1];
  return A;
}

Compilation message

werewolf.cpp: In function 'void dfs(int, int)':
werewolf.cpp:44:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |     for(int i=0; i<ne[x].size(); i++)
      |                  ~^~~~~~~~~~~~~
werewolf.cpp: In function 'void dfs1(int, int)':
werewolf.cpp:59:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |     for(int i=0; i<ol[x].size(); i++)
      |                  ~^~~~~~~~~~~~~
werewolf.cpp: In function 'std::vector<int> check_validity(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
werewolf.cpp:131:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |     for(int i=0; i<g.size(); i++){
      |                  ~^~~~~~~~~
werewolf.cpp:144:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  144 |  for(int i=0; i<g.size(); i++){
      |               ~^~~~~~~~~
werewolf.cpp:156:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  156 |     for(int i=0; i<g.size(); i++){
      |                  ~^~~~~~~~~
werewolf.cpp:184:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  184 |   for(int j=0; j<be[i].size(); j++)
      |                ~^~~~~~~~~~~~~
werewolf.cpp:191:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  191 |   for(int j=0; j < ed[i].size(); j++){
      |                ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 23 ms 47436 KB Output is correct
2 Correct 22 ms 47384 KB Output is correct
3 Correct 22 ms 47424 KB Output is correct
4 Correct 25 ms 47276 KB Output is correct
5 Correct 28 ms 47408 KB Output is correct
6 Correct 28 ms 47320 KB Output is correct
7 Correct 22 ms 47368 KB Output is correct
8 Correct 21 ms 47356 KB Output is correct
9 Correct 22 ms 47404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 47436 KB Output is correct
2 Correct 22 ms 47384 KB Output is correct
3 Correct 22 ms 47424 KB Output is correct
4 Correct 25 ms 47276 KB Output is correct
5 Correct 28 ms 47408 KB Output is correct
6 Correct 28 ms 47320 KB Output is correct
7 Correct 22 ms 47368 KB Output is correct
8 Correct 21 ms 47356 KB Output is correct
9 Correct 22 ms 47404 KB Output is correct
10 Correct 32 ms 48832 KB Output is correct
11 Correct 32 ms 48816 KB Output is correct
12 Correct 31 ms 48680 KB Output is correct
13 Correct 29 ms 48832 KB Output is correct
14 Correct 34 ms 48844 KB Output is correct
15 Correct 33 ms 48824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 796 ms 138632 KB Output is correct
2 Correct 711 ms 141468 KB Output is correct
3 Correct 679 ms 137976 KB Output is correct
4 Correct 628 ms 137368 KB Output is correct
5 Correct 670 ms 137540 KB Output is correct
6 Correct 741 ms 139024 KB Output is correct
7 Correct 682 ms 137696 KB Output is correct
8 Correct 703 ms 141536 KB Output is correct
9 Correct 603 ms 137692 KB Output is correct
10 Correct 469 ms 136400 KB Output is correct
11 Correct 514 ms 136408 KB Output is correct
12 Correct 554 ms 136040 KB Output is correct
13 Correct 835 ms 141612 KB Output is correct
14 Correct 829 ms 141472 KB Output is correct
15 Correct 875 ms 141660 KB Output is correct
16 Correct 844 ms 141564 KB Output is correct
17 Correct 733 ms 137908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 47436 KB Output is correct
2 Correct 22 ms 47384 KB Output is correct
3 Correct 22 ms 47424 KB Output is correct
4 Correct 25 ms 47276 KB Output is correct
5 Correct 28 ms 47408 KB Output is correct
6 Correct 28 ms 47320 KB Output is correct
7 Correct 22 ms 47368 KB Output is correct
8 Correct 21 ms 47356 KB Output is correct
9 Correct 22 ms 47404 KB Output is correct
10 Correct 32 ms 48832 KB Output is correct
11 Correct 32 ms 48816 KB Output is correct
12 Correct 31 ms 48680 KB Output is correct
13 Correct 29 ms 48832 KB Output is correct
14 Correct 34 ms 48844 KB Output is correct
15 Correct 33 ms 48824 KB Output is correct
16 Correct 796 ms 138632 KB Output is correct
17 Correct 711 ms 141468 KB Output is correct
18 Correct 679 ms 137976 KB Output is correct
19 Correct 628 ms 137368 KB Output is correct
20 Correct 670 ms 137540 KB Output is correct
21 Correct 741 ms 139024 KB Output is correct
22 Correct 682 ms 137696 KB Output is correct
23 Correct 703 ms 141536 KB Output is correct
24 Correct 603 ms 137692 KB Output is correct
25 Correct 469 ms 136400 KB Output is correct
26 Correct 514 ms 136408 KB Output is correct
27 Correct 554 ms 136040 KB Output is correct
28 Correct 835 ms 141612 KB Output is correct
29 Correct 829 ms 141472 KB Output is correct
30 Correct 875 ms 141660 KB Output is correct
31 Correct 844 ms 141564 KB Output is correct
32 Correct 733 ms 137908 KB Output is correct
33 Correct 869 ms 141664 KB Output is correct
34 Correct 409 ms 88676 KB Output is correct
35 Correct 938 ms 145112 KB Output is correct
36 Correct 838 ms 140876 KB Output is correct
37 Correct 935 ms 144068 KB Output is correct
38 Correct 902 ms 141676 KB Output is correct
39 Correct 866 ms 150424 KB Output is correct
40 Correct 933 ms 149884 KB Output is correct
41 Correct 687 ms 140544 KB Output is correct
42 Correct 576 ms 137088 KB Output is correct
43 Correct 962 ms 148808 KB Output is correct
44 Correct 770 ms 141620 KB Output is correct
45 Correct 670 ms 146604 KB Output is correct
46 Correct 668 ms 146424 KB Output is correct
47 Correct 847 ms 141708 KB Output is correct
48 Correct 853 ms 141548 KB Output is correct
49 Correct 816 ms 141736 KB Output is correct
50 Correct 836 ms 141596 KB Output is correct
51 Correct 849 ms 148460 KB Output is correct
52 Correct 805 ms 148352 KB Output is correct