Submission #617436

# Submission time Handle Problem Language Result Execution time Memory
617436 2022-08-01T11:20:09 Z patrikpavic2 Sprinkler (JOI22_sprinkler) C++17
41 / 100
4000 ms 686264 KB
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

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

using namespace std;

typedef vector < int > vi;
typedef long long ll;

const int N = 2e5 + 500;
const int LOG = 20;
const int INF = 0x3f3f3f3f;

int MOD;

inline int mul(int A, int B){
	return (ll)A * B % MOD;
}

struct tournament{
	int siz, off;
	vi T;
	void init(int _siz){
		siz = _siz; off = 1;
		while(off < siz) off <<= 1;
		T.resize(2 * off);
		for(int &x : T) x = 1;
	}
	int get(int x){
		int ret = 1;
		for(x = (x + off); x ; x /= 2)
			ret = mul(ret, T[x]);
		return ret;
	}
	void Tmultiply(int i, int a, int b, int lo, int hi, int x){
		if(lo <= a && b <= hi) { T[i] = mul(T[i], x); return; }
		if(a > hi || b < lo) return;
		Tmultiply(2 * i, a, (a + b) / 2, lo, hi, x);
		Tmultiply(2 * i + 1, (a + b) / 2 + 1, b, lo, hi, x);
	}
	inline void multiply(int l, int r, int x){
		if(r < l ) return;
		Tmultiply(1, 0, off - 1, l, r, x);
	}
};

tournament T[N][41];
int cen[N], siz[N], par[N], dep[N][LOG], gore[N], tko[N][41];
vector < int > pos;
int L[N][41], R[N][41], gdje[N][LOG], osb[N], gdje2[N][LOG];
int n, q;
vector < int > v[N];

void dfs(int x, int lst){
	siz[x] = 1; pos.PB(x);
	for(int y : v[x]){
		if(cen[y] != INF || y == lst) continue;
		dfs(y, x); siz[x] += siz[y];
	}
}

int TATA, RAZ;

void dfs2(int x, int lst, int dis){
	gdje2[x][RAZ] = dis;
	if(dis <= 40) {
		gdje[x][RAZ] = tko[TATA][dis];
		tko[TATA][dis]++;
	}
	for(int y : v[x]){
		if(y == lst || cen[y] <= RAZ) continue;
		dfs2(y, x, dis + 1);
	}
}

int nadi_centroid(int x){
	pos.clear(); dfs(x, x); 
	int bst = x;
	for(int y : pos)
		if(2 * siz[y] >= (int)pos.size() && siz[y] < siz[bst])
			bst = y;
	return bst;
}


int centroidna(int x, int dub){
	x = nadi_centroid(x);
	cen[x] = dub;
	for(int y : v[x]){
		if(cen[y] != INF) continue;
		int nov = centroidna(y, dub + 1);
		for(int i = 1;i <= 40;i++)
			L[nov][i] = tko[x][i];
		TATA = x; RAZ = dub;
		dfs2(y, x, 1);
		for(int i = 1;i <= 40;i++)
			R[nov][i] = tko[x][i] - 1;
		par[nov] = x; 
	}
	return x;
}

void obradi(int x, int d, int k){
	osb[x] = mul(osb[x], k);
	for(int i = 1;i <= d;i++) 
		T[x][i].multiply(0, T[x][i].siz - 1, k);
	int lst = x, poc = x; 
	x = par[x]; 
	while(x){
		if(gdje2[poc][cen[x]] <= d)
			osb[x] = mul(osb[x], k);		
		for(int i = 1;i <= d - gdje2[poc][cen[x]];i++){
			T[x][i].multiply(0, L[lst][i] - 1, k);
			T[x][i].multiply(R[lst][i] + 1, T[x][i].siz - 1, k);
		}
		lst = x; x = par[x];	
	}
}

int get(int x){
	int ret = osb[x];
	int cur = x;
	while(cur){
		if(gdje[x][cen[cur]] != -1){
			ret = mul(ret, T[cur][gdje2[x][cen[cur]]].get(gdje[x][cen[cur]]));
		}
		cur = par[cur];
	}
	return ret;
	
}

int main(){
	memset(cen, INF, sizeof(cen));
	memset(gdje, -1, sizeof(gdje));
	scanf("%d%d", &n, &MOD);
	for(int i = 1;i < n;i++){
		int x, y; scanf("%d%d", &x, &y);
		v[x].PB(y), v[y].PB(x);
	}
	for(int i = 1;i <= n;i++)
		scanf("%d", osb + i);
	centroidna(1, 1);
	for(int i = 1;i <= n;i++)
		for(int j = 0;j <= 40;j++)
			T[i][j].init(tko[i][j]);
	scanf("%d", &q);
	for(;q--;){
		int ty; scanf("%d", &ty);
		if(ty == 2){
			int x; scanf("%d", &x);
			printf("%d\n", get(x));
		}
		else{
			int x, d, k; scanf("%d%d%d", &x, &d, &k);
			obradi(x, d, k);
		}
	}
	return 0;
}





Compilation message

sprinkler.cpp: In function 'int main()':
sprinkler.cpp:141:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |  scanf("%d%d", &n, &MOD);
      |  ~~~~~^~~~~~~~~~~~~~~~~~
sprinkler.cpp:143:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  143 |   int x, y; scanf("%d%d", &x, &y);
      |             ~~~~~^~~~~~~~~~~~~~~~
sprinkler.cpp:147:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  147 |   scanf("%d", osb + i);
      |   ~~~~~^~~~~~~~~~~~~~~
sprinkler.cpp:152:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  152 |  scanf("%d", &q);
      |  ~~~~~^~~~~~~~~~
sprinkler.cpp:154:16: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  154 |   int ty; scanf("%d", &ty);
      |           ~~~~~^~~~~~~~~~~
sprinkler.cpp:156:16: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  156 |    int x; scanf("%d", &x);
      |           ~~~~~^~~~~~~~~~
sprinkler.cpp:160:22: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  160 |    int x, d, k; scanf("%d%d%d", &x, &d, &k);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 130 ms 278820 KB Output is correct
2 Correct 134 ms 279004 KB Output is correct
3 Correct 125 ms 278904 KB Output is correct
4 Correct 149 ms 280864 KB Output is correct
5 Correct 150 ms 280864 KB Output is correct
6 Correct 131 ms 280740 KB Output is correct
7 Correct 128 ms 280848 KB Output is correct
8 Correct 138 ms 280640 KB Output is correct
9 Correct 134 ms 278924 KB Output is correct
10 Correct 132 ms 278828 KB Output is correct
11 Correct 126 ms 278856 KB Output is correct
12 Correct 132 ms 278884 KB Output is correct
13 Correct 139 ms 278956 KB Output is correct
14 Correct 136 ms 278816 KB Output is correct
15 Correct 128 ms 278832 KB Output is correct
16 Correct 129 ms 278900 KB Output is correct
17 Correct 140 ms 278892 KB Output is correct
18 Correct 154 ms 278924 KB Output is correct
19 Correct 150 ms 278840 KB Output is correct
20 Correct 148 ms 278900 KB Output is correct
21 Correct 149 ms 278928 KB Output is correct
22 Correct 157 ms 278880 KB Output is correct
23 Correct 149 ms 278936 KB Output is correct
24 Correct 137 ms 278832 KB Output is correct
25 Correct 153 ms 278924 KB Output is correct
26 Correct 134 ms 278908 KB Output is correct
27 Correct 147 ms 278936 KB Output is correct
28 Correct 136 ms 278904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 135 ms 278900 KB Output is correct
2 Correct 1792 ms 679060 KB Output is correct
3 Correct 1714 ms 679576 KB Output is correct
4 Correct 1818 ms 678920 KB Output is correct
5 Correct 1921 ms 679312 KB Output is correct
6 Correct 2045 ms 684856 KB Output is correct
7 Correct 1877 ms 683704 KB Output is correct
8 Correct 1084 ms 639796 KB Output is correct
9 Correct 2156 ms 683764 KB Output is correct
10 Correct 1955 ms 682984 KB Output is correct
11 Correct 2355 ms 679124 KB Output is correct
12 Correct 1861 ms 679584 KB Output is correct
13 Correct 1235 ms 668580 KB Output is correct
14 Correct 1387 ms 669708 KB Output is correct
15 Correct 1397 ms 672264 KB Output is correct
16 Correct 1428 ms 673312 KB Output is correct
17 Correct 1414 ms 677520 KB Output is correct
18 Correct 162 ms 278852 KB Output is correct
19 Correct 166 ms 278924 KB Output is correct
20 Correct 163 ms 278896 KB Output is correct
21 Correct 141 ms 278924 KB Output is correct
22 Correct 135 ms 278880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 135 ms 278900 KB Output is correct
2 Correct 1792 ms 679060 KB Output is correct
3 Correct 1714 ms 679576 KB Output is correct
4 Correct 1818 ms 678920 KB Output is correct
5 Correct 1921 ms 679312 KB Output is correct
6 Correct 2045 ms 684856 KB Output is correct
7 Correct 1877 ms 683704 KB Output is correct
8 Correct 1084 ms 639796 KB Output is correct
9 Correct 2156 ms 683764 KB Output is correct
10 Correct 1955 ms 682984 KB Output is correct
11 Correct 2355 ms 679124 KB Output is correct
12 Correct 1861 ms 679584 KB Output is correct
13 Correct 1235 ms 668580 KB Output is correct
14 Correct 1387 ms 669708 KB Output is correct
15 Correct 1397 ms 672264 KB Output is correct
16 Correct 1428 ms 673312 KB Output is correct
17 Correct 1414 ms 677520 KB Output is correct
18 Correct 162 ms 278852 KB Output is correct
19 Correct 166 ms 278924 KB Output is correct
20 Correct 163 ms 278896 KB Output is correct
21 Correct 141 ms 278924 KB Output is correct
22 Correct 135 ms 278880 KB Output is correct
23 Correct 157 ms 278912 KB Output is correct
24 Correct 2333 ms 679196 KB Output is correct
25 Correct 2124 ms 679608 KB Output is correct
26 Correct 2330 ms 683408 KB Output is correct
27 Correct 2124 ms 679288 KB Output is correct
28 Correct 2243 ms 686264 KB Output is correct
29 Correct 2482 ms 682528 KB Output is correct
30 Correct 1443 ms 639876 KB Output is correct
31 Correct 2350 ms 679268 KB Output is correct
32 Correct 2410 ms 683144 KB Output is correct
33 Correct 2222 ms 678968 KB Output is correct
34 Correct 2012 ms 679404 KB Output is correct
35 Correct 173 ms 278864 KB Output is correct
36 Correct 175 ms 278920 KB Output is correct
37 Correct 164 ms 278852 KB Output is correct
38 Correct 141 ms 278944 KB Output is correct
39 Correct 164 ms 278868 KB Output is correct
40 Correct 140 ms 278852 KB Output is correct
41 Correct 181 ms 278916 KB Output is correct
42 Correct 155 ms 278912 KB Output is correct
43 Correct 158 ms 278868 KB Output is correct
44 Correct 158 ms 278940 KB Output is correct
45 Correct 155 ms 278900 KB Output is correct
46 Correct 169 ms 278860 KB Output is correct
47 Correct 172 ms 278928 KB Output is correct
48 Correct 165 ms 278808 KB Output is correct
49 Correct 151 ms 278944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 151 ms 278864 KB Output is correct
2 Correct 2559 ms 680692 KB Output is correct
3 Execution timed out 4024 ms 677860 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 149 ms 278920 KB Output is correct
2 Correct 2832 ms 678372 KB Output is correct
3 Correct 3790 ms 674728 KB Output is correct
4 Correct 2925 ms 676672 KB Output is correct
5 Correct 3049 ms 678076 KB Output is correct
6 Execution timed out 4113 ms 682524 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 130 ms 278820 KB Output is correct
2 Correct 134 ms 279004 KB Output is correct
3 Correct 125 ms 278904 KB Output is correct
4 Correct 149 ms 280864 KB Output is correct
5 Correct 150 ms 280864 KB Output is correct
6 Correct 131 ms 280740 KB Output is correct
7 Correct 128 ms 280848 KB Output is correct
8 Correct 138 ms 280640 KB Output is correct
9 Correct 134 ms 278924 KB Output is correct
10 Correct 132 ms 278828 KB Output is correct
11 Correct 126 ms 278856 KB Output is correct
12 Correct 132 ms 278884 KB Output is correct
13 Correct 139 ms 278956 KB Output is correct
14 Correct 136 ms 278816 KB Output is correct
15 Correct 128 ms 278832 KB Output is correct
16 Correct 129 ms 278900 KB Output is correct
17 Correct 140 ms 278892 KB Output is correct
18 Correct 154 ms 278924 KB Output is correct
19 Correct 150 ms 278840 KB Output is correct
20 Correct 148 ms 278900 KB Output is correct
21 Correct 149 ms 278928 KB Output is correct
22 Correct 157 ms 278880 KB Output is correct
23 Correct 149 ms 278936 KB Output is correct
24 Correct 137 ms 278832 KB Output is correct
25 Correct 153 ms 278924 KB Output is correct
26 Correct 134 ms 278908 KB Output is correct
27 Correct 147 ms 278936 KB Output is correct
28 Correct 136 ms 278904 KB Output is correct
29 Correct 135 ms 278900 KB Output is correct
30 Correct 1792 ms 679060 KB Output is correct
31 Correct 1714 ms 679576 KB Output is correct
32 Correct 1818 ms 678920 KB Output is correct
33 Correct 1921 ms 679312 KB Output is correct
34 Correct 2045 ms 684856 KB Output is correct
35 Correct 1877 ms 683704 KB Output is correct
36 Correct 1084 ms 639796 KB Output is correct
37 Correct 2156 ms 683764 KB Output is correct
38 Correct 1955 ms 682984 KB Output is correct
39 Correct 2355 ms 679124 KB Output is correct
40 Correct 1861 ms 679584 KB Output is correct
41 Correct 1235 ms 668580 KB Output is correct
42 Correct 1387 ms 669708 KB Output is correct
43 Correct 1397 ms 672264 KB Output is correct
44 Correct 1428 ms 673312 KB Output is correct
45 Correct 1414 ms 677520 KB Output is correct
46 Correct 162 ms 278852 KB Output is correct
47 Correct 166 ms 278924 KB Output is correct
48 Correct 163 ms 278896 KB Output is correct
49 Correct 141 ms 278924 KB Output is correct
50 Correct 135 ms 278880 KB Output is correct
51 Correct 157 ms 278912 KB Output is correct
52 Correct 2333 ms 679196 KB Output is correct
53 Correct 2124 ms 679608 KB Output is correct
54 Correct 2330 ms 683408 KB Output is correct
55 Correct 2124 ms 679288 KB Output is correct
56 Correct 2243 ms 686264 KB Output is correct
57 Correct 2482 ms 682528 KB Output is correct
58 Correct 1443 ms 639876 KB Output is correct
59 Correct 2350 ms 679268 KB Output is correct
60 Correct 2410 ms 683144 KB Output is correct
61 Correct 2222 ms 678968 KB Output is correct
62 Correct 2012 ms 679404 KB Output is correct
63 Correct 173 ms 278864 KB Output is correct
64 Correct 175 ms 278920 KB Output is correct
65 Correct 164 ms 278852 KB Output is correct
66 Correct 141 ms 278944 KB Output is correct
67 Correct 164 ms 278868 KB Output is correct
68 Correct 140 ms 278852 KB Output is correct
69 Correct 181 ms 278916 KB Output is correct
70 Correct 155 ms 278912 KB Output is correct
71 Correct 158 ms 278868 KB Output is correct
72 Correct 158 ms 278940 KB Output is correct
73 Correct 155 ms 278900 KB Output is correct
74 Correct 169 ms 278860 KB Output is correct
75 Correct 172 ms 278928 KB Output is correct
76 Correct 165 ms 278808 KB Output is correct
77 Correct 151 ms 278944 KB Output is correct
78 Correct 151 ms 278864 KB Output is correct
79 Correct 2559 ms 680692 KB Output is correct
80 Execution timed out 4024 ms 677860 KB Time limit exceeded
81 Halted 0 ms 0 KB -