답안 #381188

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
381188 2021-03-24T17:47:16 Z MatheusLealV 늑대인간 (IOI18_werewolf) C++17
100 / 100
1252 ms 196392 KB
#include "werewolf.h"
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
#define N 500050

int n;
int bit[N];
void upd(int x, int v){
	x+=5;
	assert(x>0);
	for(int i = x; i < N; i += (i&-i)) bit[i] += v;
}
int query(int x){
	x+=5;
	assert(x>0);
	int sum = 0;
	for(int i = x; i > 0; i -=(i&-i)) sum += bit[i];
	return sum;
}
struct kruskal_tree{
	vector<int>grafo[N];
	int pai[N], nivel[N], anc[N][20],cnt, v[N];
	int ini[N], fim[N], tempo;
	void init(){
		for(int i=0;i< N;i++)pai[i]=i,nivel[i]=v[i]=0;
		tempo =0;cnt=n;
	}
	int find(int x){
		if(x==pai[x])return x;
		return pai[x]=find(pai[x]);
	}
	void join(int a, int b, int cost){
		a=find(a);b=find(b);
		if(a==b) return;
		++cnt;
		pai[cnt]=cnt;
		grafo[cnt].pb(a);
		grafo[cnt].pb(b);
		pai[a]=cnt;pai[b]=cnt;
		v[cnt] = cost;
	}
	void dfs(int x, int p){
		ini[x]=++tempo;
		nivel[x]=nivel[p]+1;
		anc[x][0]=p;
		for(auto v:grafo[x]){
			if(v==p)continue;
			dfs(v,x);
		}
		fim[x]=tempo;
	}
	void get_anc(){
		memset(anc,-1,sizeof anc);
		for(int i = cnt;i>=1;i--){
			if(!nivel[i]) dfs(i,i);
		}
		for(int j=1;j<20;j++)
			for(int i=1;i<=cnt;i++)
				anc[i][j]=anc[anc[i][j-1]][j-1];
	}
} mn,mx;
struct line{
	int x, yl, yr, id, tipo; //0=ponto,-1 =close, +1 = open
};
vector<line> lines;
vector<int> check_validity(int nn, vector<int> X, vector<int> Y,vector<int> S, vector<int> E, vector<int> L, vector<int> R) {
  n=nn;
  mn.init();mx.init();
  int m = sz(X),q=sz(S);
  vector<array<int,3>> ed[2];
  //criar max mst com arestas = min(a, b)
  //criar min mst com arestas = max(a, b)
  for(int i = 0; i < m;i++){
  	int a = X[i]+1, b = Y[i]+1, c1 = min(a,b),c2=max(a,b);
  	ed[0].pb({c1,a,b});ed[1].pb({c2,a,b});
  }
  sort(all(ed[0]), [&](auto a, auto b){
  	return a[0] > b[0];
  });
  sort(all(ed[1]));
  for(auto w: ed[0]) mx.join(w[1],w[2],w[0]);
  for(auto w: ed[1]) mn.join(w[1],w[2],w[0]);
  mx.get_anc();
  mn.get_anc();
  vector<int> ans(q);
  for(int i =0;i<q; i++){
  	int st = S[i]+1, en = E[i]+1, l = L[i]+1, r = R[i]+1;
  	int u=st,v=en;
  	for(int j = 19; j >= 0; j--)
  		if(mx.anc[u][j] != -1 and mx.v[mx.anc[u][j]] >= l)
  			u = mx.anc[u][j];
  	for(int j = 19; j>=0;j--)
  		if(mn.anc[v][j] != -1 and mn.v[mn.anc[v][j]] <= r)
  			v = mn.anc[v][j];
  	if(st < l or en > r) continue;
  	lines.pb({mn.ini[v], mx.ini[u], mx.fim[u], i, -1});
  	lines.pb({mn.fim[v], mx.ini[u], mx.fim[u], i, 1});
  }
  for(int i = 1; i <= n; i++){
  	lines.pb({mn.ini[i], mx.ini[i], mx.ini[i], i, 0});
  }
  sort(all(lines), [&](auto a, auto b){
  	if(a.x != b.x) return a.x<b.x;
  	return a.tipo < b.tipo;
  });
  for(int i = 0; i < sz(lines); i++){
  	if(lines[i].tipo == -1){
  		ans[lines[i].id] -= query(lines[i].yr);
  		ans[lines[i].id] += query(lines[i].yl - 1);
  	}
  	else if(lines[i].tipo == 1){
  		ans[lines[i].id] += query(lines[i].yr);
  		ans[lines[i].id] -= query(lines[i].yl -1);
  	}
  	else upd(lines[i].yl,1);
  }
  for(int i=0;i<q;i++)ans[i]=min(ans[i],1);
  return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 113900 KB Output is correct
2 Correct 68 ms 113900 KB Output is correct
3 Correct 67 ms 113900 KB Output is correct
4 Correct 67 ms 113900 KB Output is correct
5 Correct 71 ms 113900 KB Output is correct
6 Correct 67 ms 113900 KB Output is correct
7 Correct 68 ms 114028 KB Output is correct
8 Correct 67 ms 113900 KB Output is correct
9 Correct 67 ms 113900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 113900 KB Output is correct
2 Correct 68 ms 113900 KB Output is correct
3 Correct 67 ms 113900 KB Output is correct
4 Correct 67 ms 113900 KB Output is correct
5 Correct 71 ms 113900 KB Output is correct
6 Correct 67 ms 113900 KB Output is correct
7 Correct 68 ms 114028 KB Output is correct
8 Correct 67 ms 113900 KB Output is correct
9 Correct 67 ms 113900 KB Output is correct
10 Correct 75 ms 114924 KB Output is correct
11 Correct 75 ms 114892 KB Output is correct
12 Correct 75 ms 114892 KB Output is correct
13 Correct 77 ms 115148 KB Output is correct
14 Correct 75 ms 115020 KB Output is correct
15 Correct 77 ms 115020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 941 ms 170804 KB Output is correct
2 Correct 1099 ms 185440 KB Output is correct
3 Correct 1002 ms 181432 KB Output is correct
4 Correct 897 ms 179684 KB Output is correct
5 Correct 900 ms 179508 KB Output is correct
6 Correct 940 ms 179456 KB Output is correct
7 Correct 911 ms 179328 KB Output is correct
8 Correct 1070 ms 185332 KB Output is correct
9 Correct 854 ms 181428 KB Output is correct
10 Correct 735 ms 179580 KB Output is correct
11 Correct 757 ms 179508 KB Output is correct
12 Correct 798 ms 179252 KB Output is correct
13 Correct 1162 ms 185560 KB Output is correct
14 Correct 1149 ms 185460 KB Output is correct
15 Correct 1174 ms 185524 KB Output is correct
16 Correct 1151 ms 185452 KB Output is correct
17 Correct 884 ms 179124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 113900 KB Output is correct
2 Correct 68 ms 113900 KB Output is correct
3 Correct 67 ms 113900 KB Output is correct
4 Correct 67 ms 113900 KB Output is correct
5 Correct 71 ms 113900 KB Output is correct
6 Correct 67 ms 113900 KB Output is correct
7 Correct 68 ms 114028 KB Output is correct
8 Correct 67 ms 113900 KB Output is correct
9 Correct 67 ms 113900 KB Output is correct
10 Correct 75 ms 114924 KB Output is correct
11 Correct 75 ms 114892 KB Output is correct
12 Correct 75 ms 114892 KB Output is correct
13 Correct 77 ms 115148 KB Output is correct
14 Correct 75 ms 115020 KB Output is correct
15 Correct 77 ms 115020 KB Output is correct
16 Correct 941 ms 170804 KB Output is correct
17 Correct 1099 ms 185440 KB Output is correct
18 Correct 1002 ms 181432 KB Output is correct
19 Correct 897 ms 179684 KB Output is correct
20 Correct 900 ms 179508 KB Output is correct
21 Correct 940 ms 179456 KB Output is correct
22 Correct 911 ms 179328 KB Output is correct
23 Correct 1070 ms 185332 KB Output is correct
24 Correct 854 ms 181428 KB Output is correct
25 Correct 735 ms 179580 KB Output is correct
26 Correct 757 ms 179508 KB Output is correct
27 Correct 798 ms 179252 KB Output is correct
28 Correct 1162 ms 185560 KB Output is correct
29 Correct 1149 ms 185460 KB Output is correct
30 Correct 1174 ms 185524 KB Output is correct
31 Correct 1151 ms 185452 KB Output is correct
32 Correct 884 ms 179124 KB Output is correct
33 Correct 1085 ms 180628 KB Output is correct
34 Correct 567 ms 163856 KB Output is correct
35 Correct 1196 ms 185464 KB Output is correct
36 Correct 1010 ms 180276 KB Output is correct
37 Correct 1194 ms 184300 KB Output is correct
38 Correct 1088 ms 181224 KB Output is correct
39 Correct 1055 ms 191548 KB Output is correct
40 Correct 1170 ms 195860 KB Output is correct
41 Correct 956 ms 183196 KB Output is correct
42 Correct 805 ms 180380 KB Output is correct
43 Correct 1252 ms 193888 KB Output is correct
44 Correct 1111 ms 184120 KB Output is correct
45 Correct 937 ms 192440 KB Output is correct
46 Correct 969 ms 191648 KB Output is correct
47 Correct 1166 ms 185840 KB Output is correct
48 Correct 1172 ms 185396 KB Output is correct
49 Correct 1159 ms 185716 KB Output is correct
50 Correct 1162 ms 185520 KB Output is correct
51 Correct 1093 ms 196392 KB Output is correct
52 Correct 1102 ms 196228 KB Output is correct