답안 #670611

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
670611 2022-12-09T17:48:06 Z rainboy 수확 (JOI20_harvest) C
100 / 100
358 ms 72164 KB
#include <stdio.h>
#include <stdlib.h>

#define N	200000
#define M	200000
#define Q	200000

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

int aa[N], pp[N], ww[N], n; char cycle[N];
int bb[M], jj[M], tt[M], m; long long vv[M];
int ii[Q], q; long long ss[Q], uu[Q], ans[Q];
long long rr[M + Q], l_;
int l, c;

long long *xx;

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k)
			if (xx[ii[j]] == xx[i_])
				j++;
			else if (xx[ii[j]] < xx[i_]) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		sort(ii, l, i);
		l = k;
	}
}

int *ej[N], eo[N], *eg[N], eo_[N], *eh[N], eo1[N];

void append(int **ej, int *eo, int i, int j) {
	int o = eo[i]++;

	if (o >= 2 && (o & o - 1) == 0)
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
	ej[i][o] = j;
}

char visited[N]; long long dd[N]; int ta[N], tb[N];
int gg[M], m_;
int hh[Q], q_;
int gh[M + Q];

void dfs(int i, long long d) {
	static int time;
	int o;

	if (visited[i] == 1)
		return;
	ta[i] = time++;
	visited[i] = 1;
	dd[i] = d;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		dfs(j, d + ww[j]);
	}
	for (o = eo_[i]; o--; ) {
		int g = eg[i][o];

		gg[m_++] = g;
	}
	for (o = eo1[i]; o--; ) {
		int h = eh[i][o];

		hh[q_++] = h;
	}
	tb[i] = time;
}

int ft[M + Q];

void update(int i, int n, int x) {
	while (i < n) {
		ft[i] += x;
		i |= i + 1;
	}
}

int query(int i) {
	int x = 0;

	while (i >= 0) {
		x += ft[i];
		i &= i + 1, i--;
	}
	return x;
}

void solve1() {
	int g, g_, h, h_;

	for (g = 0, h = 0; h < q_; h++) {
		h_ = hh[h];
		while (g < m_ && uu[g_ = gg[g]] <= vv[h_])
			update(ta[jj[g_]], n, 1), g++;
		ans[h_] += query(tb[ii[h_]] - 1) - query(ta[ii[h_]] - 1);
	}
	while (g--) {
		g_ = gg[g];
		update(ta[jj[g_]], n, -1);
	}
}

void solve2() {
	long long sum, cnt;
	int g, g_, h, h_, r;

	sum = cnt = 0;
	for (g = 0, h = 0; h < q_; h++) {
		h_ = hh[h];
		if (!cycle[ii[h_]])
			continue;
		while (g < m_ && uu[g_ = gg[g]] <= vv[h_])
			sum += uu[g_] / l_, cnt++, g++;
		ans[h_] += vv[h_] / l_ * cnt - sum;
	}
	for (g = 0; g < m_; g++) {
		g_ = gg[g];
		rr[g] = uu[g_] % l_;
	}
	for (h = 0; h < q_; h++) {
		h_ = hh[h];
		rr[m_ + h] = vv[h_] % l_;
	}
	for (g = 0; g < m_ + q_; g++)
		gh[g] = g;
	xx = rr, sort(gh, 0, m_ + q_);
	for (g = 0, r = 0; g < m_ + q_; g++)
		rr[gh[g]] = g + 1 == m_ + q || rr[gh[g + 1]] != rr[gh[g]] ? r++ : r;
	for (g = 0, h = 0; h < q_; h++) {
		h_ = hh[h];
		if (!cycle[ii[h_]])
			continue;
		while (g < m_ && uu[gg[g]] <= vv[h_])
			update(m_ + q_ - 1 - rr[g++], m_ + q_, 1);
		ans[h_] -= query(m_ + q_ - 2 - rr[m_ + h]);
	}
	while (g--)
		update(m_ + q_ - 1 - rr[g], m_ + q_, -1);
}

int main() {
	int g, g_, h, h_, i, i_, j;

	scanf("%d%d%d%d", &n, &m, &l, &c);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	for (g = 0; g < m; g++)
		scanf("%d", &bb[g]);
	for (i = 0; i < n; i++) {
		eg[i] = (int *) malloc(2 * sizeof *eg[i]);
		eh[i] = (int *) malloc(2 * sizeof *eh[i]);
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	}
	scanf("%d", &q);
	for (h = 0; h < q; h++) {
		scanf("%d%lld", &i, &ss[h]), i--;
		ii[h] = i, append(eh, eo1, i, h);
	}
	for (i = n - 1, j = n - 1; j >= 0; j--) {
		while (aa[j] - (i >= 0 ? aa[i] : aa[i + n] - l) < c % l)
			i--;
		pp[j] = (i + n) % n, ww[j] = c / l * l + aa[j] - (i >= 0 ? aa[i] : aa[i + n] - l);
	}
	for (i = 0, g = 0; g < m; g++) {
		while (i < n && aa[i] < bb[g])
			i++;
		jj[g] = (i - 1 + n) % n, tt[g] = i == 0 ? bb[g] - aa[n - 1] + l : bb[g] - aa[i - 1];
		append(eg, eo_, jj[g], g);
	}
	for (i = 0; i < n; i++)
		append(ej, eo, pp[i], i);
	for (i = 0; i < n; i++) {
		if (visited[i])
			continue;
		i_ = i;
		while (!visited[i_])
			visited[i_] = -1, i_ = pp[i_];
		l_ = 0;
		while (!cycle[i_])
			l_ += ww[i_], cycle[i_] = 1, i_ = pp[i_];
		m_ = q_ = 0;
		dfs(i_, 0);
		for (g = 0; g < m_; g++) {
			g_ = gg[g];
			uu[g_] = dd[jj[g_]] + tt[g_];
		}
		for (h = 0; h < q_; h++) {
			h_ = hh[h];
			vv[h_] = dd[ii[h_]] + ss[h_];
		}
		xx = uu, sort(gg, 0, m_);
		xx = vv, sort(hh, 0, q_);
		solve1();
		solve2();
	}
	for (h = 0; h < q; h++)
		printf("%lld\n", ans[h]);
	return 0;
}

Compilation message

harvest.c: In function 'append':
harvest.c:46:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   46 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
harvest.c: In function 'main':
harvest.c:158:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  158 |  scanf("%d%d%d%d", &n, &m, &l, &c);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
harvest.c:160:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  160 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
harvest.c:162:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  162 |   scanf("%d", &bb[g]);
      |   ^~~~~~~~~~~~~~~~~~~
harvest.c:168:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  168 |  scanf("%d", &q);
      |  ^~~~~~~~~~~~~~~
harvest.c:170:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  170 |   scanf("%d%lld", &i, &ss[h]), i--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 3 ms 1108 KB Output is correct
3 Correct 4 ms 1208 KB Output is correct
4 Correct 4 ms 1236 KB Output is correct
5 Correct 3 ms 1364 KB Output is correct
6 Correct 4 ms 1364 KB Output is correct
7 Correct 4 ms 1464 KB Output is correct
8 Correct 4 ms 1196 KB Output is correct
9 Correct 3 ms 1236 KB Output is correct
10 Correct 4 ms 1236 KB Output is correct
11 Correct 3 ms 1236 KB Output is correct
12 Correct 5 ms 1456 KB Output is correct
13 Correct 5 ms 1452 KB Output is correct
14 Correct 4 ms 1328 KB Output is correct
15 Correct 4 ms 1336 KB Output is correct
16 Correct 4 ms 1332 KB Output is correct
17 Correct 4 ms 1364 KB Output is correct
18 Correct 4 ms 1364 KB Output is correct
19 Correct 4 ms 1364 KB Output is correct
20 Correct 4 ms 1364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 144 ms 18080 KB Output is correct
2 Correct 242 ms 48624 KB Output is correct
3 Correct 182 ms 45744 KB Output is correct
4 Correct 201 ms 50508 KB Output is correct
5 Correct 237 ms 61656 KB Output is correct
6 Correct 228 ms 60156 KB Output is correct
7 Correct 223 ms 48844 KB Output is correct
8 Correct 223 ms 48764 KB Output is correct
9 Correct 243 ms 63748 KB Output is correct
10 Correct 224 ms 62172 KB Output is correct
11 Correct 279 ms 62668 KB Output is correct
12 Correct 288 ms 62752 KB Output is correct
13 Correct 283 ms 62756 KB Output is correct
14 Correct 247 ms 61112 KB Output is correct
15 Correct 239 ms 55728 KB Output is correct
16 Correct 231 ms 55344 KB Output is correct
17 Correct 231 ms 53700 KB Output is correct
18 Correct 171 ms 25912 KB Output is correct
19 Correct 171 ms 25428 KB Output is correct
20 Correct 188 ms 49176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 3 ms 1108 KB Output is correct
3 Correct 4 ms 1208 KB Output is correct
4 Correct 4 ms 1236 KB Output is correct
5 Correct 3 ms 1364 KB Output is correct
6 Correct 4 ms 1364 KB Output is correct
7 Correct 4 ms 1464 KB Output is correct
8 Correct 4 ms 1196 KB Output is correct
9 Correct 3 ms 1236 KB Output is correct
10 Correct 4 ms 1236 KB Output is correct
11 Correct 3 ms 1236 KB Output is correct
12 Correct 5 ms 1456 KB Output is correct
13 Correct 5 ms 1452 KB Output is correct
14 Correct 4 ms 1328 KB Output is correct
15 Correct 4 ms 1336 KB Output is correct
16 Correct 4 ms 1332 KB Output is correct
17 Correct 4 ms 1364 KB Output is correct
18 Correct 4 ms 1364 KB Output is correct
19 Correct 4 ms 1364 KB Output is correct
20 Correct 4 ms 1364 KB Output is correct
21 Correct 144 ms 18080 KB Output is correct
22 Correct 242 ms 48624 KB Output is correct
23 Correct 182 ms 45744 KB Output is correct
24 Correct 201 ms 50508 KB Output is correct
25 Correct 237 ms 61656 KB Output is correct
26 Correct 228 ms 60156 KB Output is correct
27 Correct 223 ms 48844 KB Output is correct
28 Correct 223 ms 48764 KB Output is correct
29 Correct 243 ms 63748 KB Output is correct
30 Correct 224 ms 62172 KB Output is correct
31 Correct 279 ms 62668 KB Output is correct
32 Correct 288 ms 62752 KB Output is correct
33 Correct 283 ms 62756 KB Output is correct
34 Correct 247 ms 61112 KB Output is correct
35 Correct 239 ms 55728 KB Output is correct
36 Correct 231 ms 55344 KB Output is correct
37 Correct 231 ms 53700 KB Output is correct
38 Correct 171 ms 25912 KB Output is correct
39 Correct 171 ms 25428 KB Output is correct
40 Correct 188 ms 49176 KB Output is correct
41 Correct 286 ms 57448 KB Output is correct
42 Correct 207 ms 48728 KB Output is correct
43 Correct 178 ms 46280 KB Output is correct
44 Correct 252 ms 57716 KB Output is correct
45 Correct 256 ms 67536 KB Output is correct
46 Correct 258 ms 68268 KB Output is correct
47 Correct 299 ms 69868 KB Output is correct
48 Correct 232 ms 68856 KB Output is correct
49 Correct 242 ms 68936 KB Output is correct
50 Correct 281 ms 55884 KB Output is correct
51 Correct 257 ms 55696 KB Output is correct
52 Correct 300 ms 70132 KB Output is correct
53 Correct 323 ms 72164 KB Output is correct
54 Correct 301 ms 70116 KB Output is correct
55 Correct 358 ms 69224 KB Output is correct
56 Correct 256 ms 62132 KB Output is correct
57 Correct 263 ms 62152 KB Output is correct
58 Correct 294 ms 64384 KB Output is correct
59 Correct 226 ms 62260 KB Output is correct
60 Correct 234 ms 62608 KB Output is correct
61 Correct 262 ms 62576 KB Output is correct
62 Correct 316 ms 62284 KB Output is correct
63 Correct 219 ms 34020 KB Output is correct
64 Correct 212 ms 34132 KB Output is correct
65 Correct 217 ms 34772 KB Output is correct
66 Correct 201 ms 34092 KB Output is correct
67 Correct 195 ms 33324 KB Output is correct
68 Correct 205 ms 33556 KB Output is correct
69 Correct 261 ms 59024 KB Output is correct
70 Correct 235 ms 55564 KB Output is correct
71 Correct 260 ms 55744 KB Output is correct
72 Correct 249 ms 55980 KB Output is correct