답안 #799743

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
799743 2023-07-31T22:53:59 Z lovrot Inside information (BOI21_servers) C++17
100 / 100
430 ms 68856 KB
#include <cstdio> 
#include <cassert>
#include <algorithm> 
#include <vector>

#define X first
#define Y second
#define PB push_back

using namespace std; 

typedef pair<int, int> pii;
typedef pair<int, pii> pip;
typedef pair<pii, pii> ppp;

const int N = 3e5 + 10; 
const int oo = 0x7FFFFFFF;

int n, m, ANS[N];
char Q[N];

vector<pii> G[N];
vector<int> QC[N];
vector<pip> QQ[N];

bool BAN[N], MON[N][2];
int FIR[N], LST[N];
int CMP[N], SIZ[N];

int F[N];
vector<int> IND;
vector<pip> S; 

void add(int x, int val) {
	for(x += 2; x < N; x += x & -x) F[x] += val;
}

int sum(int x) {
	int ret = 0;
	for(x += 2; x; x -= x & -x) ret += F[x];
	return ret;
}

void dfs(int u, int p) { // ako je 'u' root, 'p' mora bit jednak 'u'
	if(p == u) {
		FIR[u] = oo; 
		LST[u] = -oo;
		CMP[u] = u;
		MON[u][0] = MON[u][1] = 1;
	}
	SIZ[u] = 1;
	
	if(MON[u][0]) // trebas RUCNO querijat 
		for(int x : QC[u]) {
			if(p == u) {
				IND.PB(x);
				S.PB({0, {0, x}});
				++ANS[x];
			} else {
				IND.PB(x); 
				S.PB({FIR[u], {0, x}}); 			
				if(x > FIR[u]) ++ANS[x]; // update od centroida
			}
		}
	if(MON[u][1] && p != u) // trebas RUCNO dodat taj update 
		S.PB({FIR[u], {1, LST[u]}}); 

	for(pii e : G[u]) {
		int v = e.X, w = e.Y; 
		if(v == p || BAN[v]) continue;
		if(p == u) {
			FIR[v] = LST[v] = w;
			CMP[v] = v;
			MON[v][0] = MON[v][1] = 1;
		} else {
			FIR[v] = FIR[u];
			LST[v] = w;
			CMP[v] = CMP[u];
			MON[v][0] = MON[u][0] && LST[u] > w;
			MON[v][1] = MON[u][1] && LST[u] < w;
		}
		dfs(v, u); 
		SIZ[u] += SIZ[v];
	}
}

void centroid(int u, int p, int &cent, int siz) {
	SIZ[u] = 1;
	bool flag = true;
	for(pii e : G[u]) {
		int v = e.X, w = e.Y;
		if(v == p || BAN[v]) continue;
		centroid(v, u, cent, siz);
		SIZ[u] += SIZ[v];
		flag &= SIZ[v] <= siz / 2;
	}
	if(flag && siz - SIZ[u] <= siz / 2) cent = u;
}

void solve(int c, int siz) {
	IND.clear(); 
	S.clear(); 

	int old = c;
	centroid(c, c, c, siz); // 'c' postaje centroid
		
	swap(QQ[c], QQ[old]); 

	dfs(c, c); 
	
	sort(IND.begin(), IND.end());
	sort(S.begin(), S.end(), [](pip a, pip b) {
		return a.X > b.X || (a.X == b.X && a.Y < b.Y);
	});

	for(int i = 0; i < (int) IND.size() + 10; ++i) F[i] = 0;
	
	for(pip q : S) {
		int ind = q.Y.Y;
		int pos = q.Y.X == 0 ?  
			lower_bound(IND.begin(), IND.end(), ind) - IND.begin() :
			lower_bound(IND.begin(), IND.end(), ind) - IND.begin();
		if(q.Y.X == 0) 	ANS[ind] += sum(pos);
		else add(pos, 1);
	}
	for(pip q : QQ[c]) {
		int ind = q.X, u = q.Y.X, v = q.Y.Y;
		if(CMP[u] == CMP[v] && CMP[u] != c) QQ[CMP[u]].PB(q);
		else {
			if(v == c)
				ANS[ind] = MON[u][1] && LST[u] < ind;
			else 
				ANS[ind] = MON[u][1] && MON[v][0] && FIR[v] < FIR[u] && max(LST[u], FIR[v]) < ind; 
		}
	}

	BAN[c] = 1;
	for(pii e : G[c]) {
		int v = e.X; 
		if(BAN[v]) continue;
		solve(v, SIZ[v]);
	}
}

int main() {
	scanf("%d%d", &n, &m);
	for(int i = 1; i < n + m; ++i) {
		int u, v;
		scanf(" %c ", Q + i); 
		if(Q[i] == 'S') {
			scanf("%d%d", &u, &v); 
			G[u].PB({v, i}); 
			G[v].PB({u, i}); 
		} else if(Q[i] == 'Q') {
			scanf("%d%d", &u, &v); 
			QQ[1].PB({i, {u, v}});
		} else {
			scanf("%d", &u); 
			QC[u].PB(i); 
		}
	}

	solve(1, n); 
	for(int i = 1; i < n + m; ++i) {
		if(Q[i] == 'Q') printf("%s\n", ANS[i] ? "yes" : "no"); 
		else if(Q[i] == 'C') printf("%d\n", ANS[i]); 
	}
	return 0;
}

Compilation message

servers.cpp: In function 'void centroid(int, int, int&, int)':
servers.cpp:91:16: warning: unused variable 'w' [-Wunused-variable]
   91 |   int v = e.X, w = e.Y;
      |                ^
servers.cpp: In function 'int main()':
servers.cpp:146:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  146 |  scanf("%d%d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~
servers.cpp:149:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  149 |   scanf(" %c ", Q + i);
      |   ~~~~~^~~~~~~~~~~~~~~
servers.cpp:151:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  151 |    scanf("%d%d", &u, &v);
      |    ~~~~~^~~~~~~~~~~~~~~~
servers.cpp:155:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  155 |    scanf("%d%d", &u, &v);
      |    ~~~~~^~~~~~~~~~~~~~~~
servers.cpp:158:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  158 |    scanf("%d", &u);
      |    ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 25152 KB Output is correct
2 Correct 40 ms 26176 KB Output is correct
3 Correct 41 ms 28192 KB Output is correct
4 Correct 41 ms 27160 KB Output is correct
5 Correct 52 ms 27896 KB Output is correct
6 Correct 39 ms 25552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 25152 KB Output is correct
2 Correct 40 ms 26176 KB Output is correct
3 Correct 41 ms 28192 KB Output is correct
4 Correct 41 ms 27160 KB Output is correct
5 Correct 52 ms 27896 KB Output is correct
6 Correct 39 ms 25552 KB Output is correct
7 Correct 38 ms 25540 KB Output is correct
8 Correct 54 ms 25428 KB Output is correct
9 Correct 64 ms 27036 KB Output is correct
10 Correct 54 ms 25760 KB Output is correct
11 Correct 54 ms 26276 KB Output is correct
12 Correct 54 ms 26648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 24868 KB Output is correct
2 Correct 97 ms 35712 KB Output is correct
3 Correct 95 ms 35784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 24868 KB Output is correct
2 Correct 97 ms 35712 KB Output is correct
3 Correct 95 ms 35784 KB Output is correct
4 Correct 36 ms 24908 KB Output is correct
5 Correct 106 ms 36648 KB Output is correct
6 Correct 144 ms 36648 KB Output is correct
7 Correct 135 ms 36804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 25948 KB Output is correct
2 Correct 212 ms 68740 KB Output is correct
3 Correct 210 ms 68624 KB Output is correct
4 Correct 171 ms 49928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 25948 KB Output is correct
2 Correct 212 ms 68740 KB Output is correct
3 Correct 210 ms 68624 KB Output is correct
4 Correct 171 ms 49928 KB Output is correct
5 Correct 48 ms 25948 KB Output is correct
6 Correct 240 ms 57516 KB Output is correct
7 Correct 271 ms 48400 KB Output is correct
8 Correct 279 ms 46724 KB Output is correct
9 Correct 228 ms 46596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 25744 KB Output is correct
2 Correct 178 ms 43284 KB Output is correct
3 Correct 203 ms 58876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 25744 KB Output is correct
2 Correct 178 ms 43284 KB Output is correct
3 Correct 203 ms 58876 KB Output is correct
4 Correct 40 ms 25668 KB Output is correct
5 Correct 273 ms 40464 KB Output is correct
6 Correct 201 ms 47108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 25932 KB Output is correct
2 Correct 254 ms 68684 KB Output is correct
3 Correct 208 ms 68552 KB Output is correct
4 Correct 172 ms 49824 KB Output is correct
5 Correct 36 ms 25764 KB Output is correct
6 Correct 176 ms 43264 KB Output is correct
7 Correct 180 ms 58876 KB Output is correct
8 Correct 175 ms 38108 KB Output is correct
9 Correct 206 ms 51496 KB Output is correct
10 Correct 259 ms 61152 KB Output is correct
11 Correct 237 ms 48752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 25932 KB Output is correct
2 Correct 254 ms 68684 KB Output is correct
3 Correct 208 ms 68552 KB Output is correct
4 Correct 172 ms 49824 KB Output is correct
5 Correct 36 ms 25764 KB Output is correct
6 Correct 176 ms 43264 KB Output is correct
7 Correct 180 ms 58876 KB Output is correct
8 Correct 175 ms 38108 KB Output is correct
9 Correct 206 ms 51496 KB Output is correct
10 Correct 259 ms 61152 KB Output is correct
11 Correct 237 ms 48752 KB Output is correct
12 Correct 37 ms 25928 KB Output is correct
13 Correct 226 ms 57616 KB Output is correct
14 Correct 267 ms 48468 KB Output is correct
15 Correct 230 ms 46696 KB Output is correct
16 Correct 217 ms 46636 KB Output is correct
17 Correct 39 ms 25612 KB Output is correct
18 Correct 274 ms 40568 KB Output is correct
19 Correct 189 ms 47176 KB Output is correct
20 Correct 194 ms 37248 KB Output is correct
21 Correct 255 ms 44288 KB Output is correct
22 Correct 371 ms 43040 KB Output is correct
23 Correct 430 ms 53004 KB Output is correct
24 Correct 341 ms 62584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 25288 KB Output is correct
2 Correct 39 ms 26272 KB Output is correct
3 Correct 40 ms 28144 KB Output is correct
4 Correct 42 ms 27092 KB Output is correct
5 Correct 44 ms 27916 KB Output is correct
6 Correct 37 ms 25572 KB Output is correct
7 Correct 35 ms 24868 KB Output is correct
8 Correct 103 ms 35848 KB Output is correct
9 Correct 99 ms 35808 KB Output is correct
10 Correct 36 ms 25952 KB Output is correct
11 Correct 215 ms 68856 KB Output is correct
12 Correct 216 ms 68544 KB Output is correct
13 Correct 168 ms 49848 KB Output is correct
14 Correct 35 ms 25664 KB Output is correct
15 Correct 175 ms 43288 KB Output is correct
16 Correct 189 ms 58872 KB Output is correct
17 Correct 185 ms 38132 KB Output is correct
18 Correct 204 ms 51420 KB Output is correct
19 Correct 282 ms 61140 KB Output is correct
20 Correct 272 ms 48828 KB Output is correct
21 Correct 113 ms 37216 KB Output is correct
22 Correct 117 ms 37152 KB Output is correct
23 Correct 149 ms 42096 KB Output is correct
24 Correct 152 ms 41728 KB Output is correct
25 Correct 218 ms 60740 KB Output is correct
26 Correct 207 ms 48732 KB Output is correct
27 Correct 192 ms 48156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 25288 KB Output is correct
2 Correct 39 ms 26272 KB Output is correct
3 Correct 40 ms 28144 KB Output is correct
4 Correct 42 ms 27092 KB Output is correct
5 Correct 44 ms 27916 KB Output is correct
6 Correct 37 ms 25572 KB Output is correct
7 Correct 35 ms 24868 KB Output is correct
8 Correct 103 ms 35848 KB Output is correct
9 Correct 99 ms 35808 KB Output is correct
10 Correct 36 ms 25952 KB Output is correct
11 Correct 215 ms 68856 KB Output is correct
12 Correct 216 ms 68544 KB Output is correct
13 Correct 168 ms 49848 KB Output is correct
14 Correct 35 ms 25664 KB Output is correct
15 Correct 175 ms 43288 KB Output is correct
16 Correct 189 ms 58872 KB Output is correct
17 Correct 185 ms 38132 KB Output is correct
18 Correct 204 ms 51420 KB Output is correct
19 Correct 282 ms 61140 KB Output is correct
20 Correct 272 ms 48828 KB Output is correct
21 Correct 113 ms 37216 KB Output is correct
22 Correct 117 ms 37152 KB Output is correct
23 Correct 149 ms 42096 KB Output is correct
24 Correct 152 ms 41728 KB Output is correct
25 Correct 218 ms 60740 KB Output is correct
26 Correct 207 ms 48732 KB Output is correct
27 Correct 192 ms 48156 KB Output is correct
28 Correct 38 ms 25528 KB Output is correct
29 Correct 56 ms 25500 KB Output is correct
30 Correct 64 ms 27036 KB Output is correct
31 Correct 57 ms 25808 KB Output is correct
32 Correct 53 ms 26356 KB Output is correct
33 Correct 55 ms 26676 KB Output is correct
34 Correct 36 ms 24944 KB Output is correct
35 Correct 103 ms 36616 KB Output is correct
36 Correct 117 ms 36656 KB Output is correct
37 Correct 126 ms 36776 KB Output is correct
38 Correct 38 ms 25936 KB Output is correct
39 Correct 226 ms 57596 KB Output is correct
40 Correct 279 ms 48552 KB Output is correct
41 Correct 219 ms 46668 KB Output is correct
42 Correct 219 ms 46668 KB Output is correct
43 Correct 38 ms 25668 KB Output is correct
44 Correct 258 ms 40540 KB Output is correct
45 Correct 194 ms 47136 KB Output is correct
46 Correct 207 ms 37352 KB Output is correct
47 Correct 217 ms 44304 KB Output is correct
48 Correct 371 ms 43076 KB Output is correct
49 Correct 378 ms 53000 KB Output is correct
50 Correct 314 ms 62588 KB Output is correct
51 Correct 141 ms 37448 KB Output is correct
52 Correct 133 ms 37572 KB Output is correct
53 Correct 140 ms 37160 KB Output is correct
54 Correct 120 ms 37740 KB Output is correct
55 Correct 155 ms 36908 KB Output is correct
56 Correct 163 ms 38476 KB Output is correct
57 Correct 231 ms 43344 KB Output is correct
58 Correct 306 ms 38584 KB Output is correct
59 Correct 202 ms 47324 KB Output is correct