Submission #61388

# Submission time Handle Problem Language Result Execution time Memory
61388 2018-07-25T16:53:53 Z egorlifar Cats or Dogs (JOI18_catdog) C++17
100 / 100
512 ms 136088 KB
#include "catdog.h"
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <iomanip>
#include <deque>
    
     
using namespace std;
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left228
#define right right228
#define next next228
#define rank rank228
#define prev prev228
#define y1 y1228                                                         
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
#define pb push_back
#define x first
#define y second
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; } 
const long long inf = 1e16;
const int MAXN = 1000228, MAXNN = 100228;

 
struct node {
  long long val[2][2];
  node(){
    val[0][0] = val[1][1] = 0, val[0][1] = val[1][0] = inf;
  }
  node operator+(const node &p) const{
    node ret;
    for(int i=0; i<2; i++)
      for(int j=0; j<2; j++){
        ret.val[i][j] = inf;
        for(int k=0; k<2; k++)
          for(int l=0; l<2; l++)
            ret.val[i][j] = min(ret.val[i][j], val[i][k] + p.val[l][j] + (k ^ l));
      }
    return ret;
  }
};
 
struct segtree {
  	vector<node> val;
  	int sz;
  	void init(int root, int lo, int hi){
	    if(lo == hi) return;
	    int mid = (lo + hi)>>1, left = root<<1, right = left | 1;
	    init(left, lo, mid);
	    init(right, mid + 1, hi);
	    val[root] = val[left] + val[right];
  	}
  	void init(int n){
	    int s = 1;
	    sz = n;
	    while(s < (sz << 1)) s <<= 1;
	    val.resize(s);
	    init(1, 1, sz);
 	}
  	void update(int root, int lo, int hi, int x, long long v0, long long v1){
	    if (lo == hi){
		    val[root].val[0][0] += v0;
		    val[root].val[1][1] += v1;
		    return;
	    }
	    int mid = (lo + hi)>>1, left = root<<1, right = left | 1;
	    if (x <= mid) {
	    	update(left, lo, mid, x, v0, v1);
	    } else {
	    	update(right, mid + 1, hi, x, v0, v1);
	    }
	    val[root] = val[left] + val[right];
  	}
  	void update(int pos, long long x0, long long x1){
    	update(1, 1, sz, pos, x0, x1);
  	}
 	pair<int, int> query() const{
    	int x = min(val[1].val[0][0], val[1].val[0][1]);
    	int y = min(val[1].val[1][0], val[1].val[1][1]);
    	return make_pair(min(x, y + 1), min(x + 1, y));
  	}
} seg[MAXNN];
 

int n;
vector<int> edge[MAXNN];
int sz[MAXNN], pathPar[MAXNN], pathPos[MAXNN], par[MAXNN], st[MAXNN];
 

void dfsSize(int x, int p){
	par[x] = p, sz[x] = 1;
	for(auto i: edge[x]){
	    if(i == p) continue;
	    dfsSize(i, x);
	    sz[x] += sz[i];
	}
}

 
void dfsDecomp(int x, int p){
	int hpath = 0;
	for(int i : edge[x]){
	    if(i == p) continue;
	    if(sz[x] <= (sz[i] << 1)){
	        hpath = i;
	        break;
	    }
	 }
	if(hpath == 0) seg[pathPar[x]].init(pathPos[x]);
	for (int i: edge[x]){
	    if (i == p) continue;
	    if (i != hpath){
	      pathPar[i] = i;
	      pathPos[i] = 1;
	    }
	    else{
	      pathPar[i] = pathPar[x];
	      pathPos[i] = pathPos[x] + 1;
	    }
	    dfsDecomp(i, x);
	}
}
 

void initialize(int N, vector<int> A, vector<int> B) {
 	n = N;
	for (int i = 0; i < n - 1; i++) {
	    edge[A[i]].pb(B[i]);
	    edge[B[i]].pb(A[i]);
	}
  	dfsSize(1, 0);
  	pathPar[1] = 1, pathPos[1] = 1;
  	dfsDecomp(1, 0);
}

 
void update(int x, int v0, int v1) {
  	while (x) {
	    int path = pathPar[x];
	    int pr0, pr1, nx0, nx1;
	    tie(pr0, pr1) = seg[path].query();
	    seg[path].update(pathPos[x], v0, v1);
	    tie(nx0, nx1) = seg[path].query();
	    v0 = nx0 - pr0;
	    v1 = nx1 - pr1;
	    x = par[pathPar[x]];
  	}
}


int getAns() {
  	pair<int, int> it = seg[1].query();
  	return min(it.first, it.second);
}


int cat(int v) {
	st[v] = 1;
  	update(v, MAXN, 0);
  	return getAns();
}


int dog(int v) {
	st[v] = 2;
  	update(v, 0, MAXN);
  	return getAns();
}

int neighbor(int v) {
	if (st[v] == 1) {
		update(v, -MAXN, 0);
	} else {
		update(v, 0, -MAXN);
	}
  	st[v] = 0;
  	return getAns();
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 5880 KB Output is correct
2 Correct 9 ms 5880 KB Output is correct
3 Correct 9 ms 5988 KB Output is correct
4 Correct 8 ms 5988 KB Output is correct
5 Correct 8 ms 5996 KB Output is correct
6 Correct 8 ms 6216 KB Output is correct
7 Correct 8 ms 6216 KB Output is correct
8 Correct 12 ms 6216 KB Output is correct
9 Correct 8 ms 6216 KB Output is correct
10 Correct 8 ms 6216 KB Output is correct
11 Correct 12 ms 6216 KB Output is correct
12 Correct 7 ms 6324 KB Output is correct
13 Correct 8 ms 6324 KB Output is correct
14 Correct 10 ms 6324 KB Output is correct
15 Correct 9 ms 6324 KB Output is correct
16 Correct 8 ms 6324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 5880 KB Output is correct
2 Correct 9 ms 5880 KB Output is correct
3 Correct 9 ms 5988 KB Output is correct
4 Correct 8 ms 5988 KB Output is correct
5 Correct 8 ms 5996 KB Output is correct
6 Correct 8 ms 6216 KB Output is correct
7 Correct 8 ms 6216 KB Output is correct
8 Correct 12 ms 6216 KB Output is correct
9 Correct 8 ms 6216 KB Output is correct
10 Correct 8 ms 6216 KB Output is correct
11 Correct 12 ms 6216 KB Output is correct
12 Correct 7 ms 6324 KB Output is correct
13 Correct 8 ms 6324 KB Output is correct
14 Correct 10 ms 6324 KB Output is correct
15 Correct 9 ms 6324 KB Output is correct
16 Correct 8 ms 6324 KB Output is correct
17 Correct 11 ms 6412 KB Output is correct
18 Correct 9 ms 6424 KB Output is correct
19 Correct 10 ms 6452 KB Output is correct
20 Correct 10 ms 6452 KB Output is correct
21 Correct 10 ms 6452 KB Output is correct
22 Correct 10 ms 6452 KB Output is correct
23 Correct 11 ms 6516 KB Output is correct
24 Correct 10 ms 6528 KB Output is correct
25 Correct 11 ms 6528 KB Output is correct
26 Correct 8 ms 6528 KB Output is correct
27 Correct 8 ms 6528 KB Output is correct
28 Correct 11 ms 6832 KB Output is correct
29 Correct 9 ms 6968 KB Output is correct
30 Correct 9 ms 6968 KB Output is correct
31 Correct 9 ms 6968 KB Output is correct
32 Correct 10 ms 6968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 5880 KB Output is correct
2 Correct 9 ms 5880 KB Output is correct
3 Correct 9 ms 5988 KB Output is correct
4 Correct 8 ms 5988 KB Output is correct
5 Correct 8 ms 5996 KB Output is correct
6 Correct 8 ms 6216 KB Output is correct
7 Correct 8 ms 6216 KB Output is correct
8 Correct 12 ms 6216 KB Output is correct
9 Correct 8 ms 6216 KB Output is correct
10 Correct 8 ms 6216 KB Output is correct
11 Correct 12 ms 6216 KB Output is correct
12 Correct 7 ms 6324 KB Output is correct
13 Correct 8 ms 6324 KB Output is correct
14 Correct 10 ms 6324 KB Output is correct
15 Correct 9 ms 6324 KB Output is correct
16 Correct 8 ms 6324 KB Output is correct
17 Correct 11 ms 6412 KB Output is correct
18 Correct 9 ms 6424 KB Output is correct
19 Correct 10 ms 6452 KB Output is correct
20 Correct 10 ms 6452 KB Output is correct
21 Correct 10 ms 6452 KB Output is correct
22 Correct 10 ms 6452 KB Output is correct
23 Correct 11 ms 6516 KB Output is correct
24 Correct 10 ms 6528 KB Output is correct
25 Correct 11 ms 6528 KB Output is correct
26 Correct 8 ms 6528 KB Output is correct
27 Correct 8 ms 6528 KB Output is correct
28 Correct 11 ms 6832 KB Output is correct
29 Correct 9 ms 6968 KB Output is correct
30 Correct 9 ms 6968 KB Output is correct
31 Correct 9 ms 6968 KB Output is correct
32 Correct 10 ms 6968 KB Output is correct
33 Correct 204 ms 15948 KB Output is correct
34 Correct 155 ms 18012 KB Output is correct
35 Correct 159 ms 18012 KB Output is correct
36 Correct 410 ms 25460 KB Output is correct
37 Correct 63 ms 25460 KB Output is correct
38 Correct 453 ms 29272 KB Output is correct
39 Correct 418 ms 31212 KB Output is correct
40 Correct 421 ms 33112 KB Output is correct
41 Correct 512 ms 35140 KB Output is correct
42 Correct 472 ms 37176 KB Output is correct
43 Correct 427 ms 38964 KB Output is correct
44 Correct 461 ms 40952 KB Output is correct
45 Correct 421 ms 42852 KB Output is correct
46 Correct 478 ms 44804 KB Output is correct
47 Correct 422 ms 46648 KB Output is correct
48 Correct 133 ms 46648 KB Output is correct
49 Correct 191 ms 47756 KB Output is correct
50 Correct 48 ms 47756 KB Output is correct
51 Correct 85 ms 47756 KB Output is correct
52 Correct 42 ms 47756 KB Output is correct
53 Correct 266 ms 51268 KB Output is correct
54 Correct 148 ms 51268 KB Output is correct
55 Correct 316 ms 51268 KB Output is correct
56 Correct 192 ms 51268 KB Output is correct
57 Correct 271 ms 55092 KB Output is correct
58 Correct 58 ms 55092 KB Output is correct
59 Correct 69 ms 55092 KB Output is correct
60 Correct 175 ms 56420 KB Output is correct
61 Correct 171 ms 58616 KB Output is correct
62 Correct 119 ms 58616 KB Output is correct
63 Correct 136 ms 74928 KB Output is correct
64 Correct 104 ms 87068 KB Output is correct
65 Correct 199 ms 109272 KB Output is correct
66 Correct 96 ms 109272 KB Output is correct
67 Correct 144 ms 109272 KB Output is correct
68 Correct 299 ms 109272 KB Output is correct
69 Correct 39 ms 109272 KB Output is correct
70 Correct 14 ms 109272 KB Output is correct
71 Correct 114 ms 109272 KB Output is correct
72 Correct 187 ms 111364 KB Output is correct
73 Correct 325 ms 132708 KB Output is correct
74 Correct 396 ms 132708 KB Output is correct
75 Correct 334 ms 136088 KB Output is correct
76 Correct 296 ms 136088 KB Output is correct
77 Correct 308 ms 136088 KB Output is correct