답안 #569020

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
569020 2022-05-26T13:33:32 Z nonsensenonsense1 Fish 2 (JOI22_fish2) C++17
0 / 100
73 ms 29612 KB
#include <cstdio>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>

const int N = 100005;
int n, q, a[N];

struct info {
	int cnt, need;
	long long sum;
};

struct item {
	std::vector<info> pref, suf;
	item() {}
	item(int x) {
		pref.push_back({1, x, x});
		suf.push_back(pref.back());
	}
} t[N * 2];

item operator+(item a, item b) {
	if (a.pref.empty()) return b;
	if (b.pref.empty()) return a;
	item res;
	res.pref = a.pref;
	res.pref.back().cnt = 0;
	int i;
	for (i = 0; i < b.pref.size(); ++i) {
		if (b.pref[i].need <= res.pref.back().sum) res.pref.back().sum = b.pref[i].sum + a.pref.back().sum;
		else {
			res.pref.push_back(b.pref[i]);
			res.pref.back().sum += a.pref.back().sum;
			if (i + 1 == b.pref.size()) res.pref.back().cnt = 0;
		}
	}
	res.suf = b.suf;
	res.suf.back().cnt = 0;
	for (i = 0; i < a.suf.size(); ++i) {
		if (a.suf[i].need <= res.suf.back().sum) res.suf.back().sum = a.suf[i].sum + b.pref.back().sum;
		else {
			res.suf.push_back(a.suf[i]);
			res.suf.back().sum += b.pref.back().sum;
			if (i + 1 == a.suf.size()) res.suf.back().cnt = 0;
		}
	}
	for (int t = 0; t < 2; ++t) {
		for (int i = 0;; ++i) {
			int j, k, amt;
			long long s;
			if (t) {
				if (i == a.suf.size()) break;
				s = a.suf[i].sum;
				j = i + 1;
				k = 0;
				amt = a.suf[i].cnt;
			} else {
				if (i == b.pref.size()) break;
				s = b.pref[i].sum;
				j = 0;
				k = i + 1;
				amt = b.pref[i].cnt;
			}
			while (true) {
				if (j < a.suf.size() && s >= a.suf[j].need) {
					s += a.suf[j].sum - (j ? a.suf[j - 1].sum : 0);
					++j;
				} else if (k < b.pref.size() && s >= b.pref[k].need) {
					s += b.pref[k].sum - (k ? b.pref[k - 1].sum : 0);
					++k;
				} else break;
			}
			if (j == a.suf.size()) {
				if (k == b.pref.size()) {
					res.pref.back().cnt += amt;
					res.suf.back().cnt += amt;
				} else {
					int l;
					for (l = 0; res.pref[l].need != b.pref[k].need; ++l);
					res.pref[l - 1].cnt += amt;
				}
			} else if (k == b.pref.size()) {
				int l;
				for (l = 0; res.suf[l].need != a.suf[j].need; ++l);
				res.suf[l - 1].cnt += amt;
			}
		}
	}
	return res;
}

void build(int v = 0, int tl = 0, int tr = n) {
	if (tr - tl == 1) t[v] = item(a[tl]);
	else {
		int m = tl + tr >> 1;
		build(v + 1, tl, m);
		build(v + (tr - tl & ~1), m, tr);
		t[v] = t[v + 1] + t[v + (tr - tl & ~1)];
	}
}

void update(int i, int x, int v = 0, int tl = 0, int tr = n) {
	if (tr - tl == 1) t[v] = item(x);
	else {
		int m = tl + tr >> 1;
		if (i < m) update(i, x, v + 1, tl, m);
		else update(i, x, v + (tr - tl & ~1), m, tr);
		t[v] = t[v + 1] + t[v + (tr - tl & ~1)];
	}
}

item query(int l, int r, int v = 0, int tl = 0, int tr = n) {
	if (tl >= r || tr <= l) return item();
	if (tl >= l && tr <= r) return t[v];
	int m = tl + tr >> 1;
	return query(l, r, v + 1, tl, m) + query(l, r, v + (tr - tl & ~1), m, tr);
}

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) scanf("%d", a + i);
	build();
	scanf("%d", &q);
	while (q--) {
		int type;
		scanf("%d", &type);
		if (type == 1) {
			int i, x;
			scanf("%d%d", &i, &x);
			update(i - 1, x);
		} else {
			int l, r;
			scanf("%d%d", &l, &r);
			printf("%d\n", query(l - 1, r).pref.back().cnt);
		}
	}
	return 0;
}

Compilation message

fish2.cpp: In function 'item operator+(item, item)':
fish2.cpp:31:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<info>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |  for (i = 0; i < b.pref.size(); ++i) {
      |              ~~^~~~~~~~~~~~~~~
fish2.cpp:36:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<info>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |    if (i + 1 == b.pref.size()) res.pref.back().cnt = 0;
      |        ~~~~~~^~~~~~~~~~~~~~~~
fish2.cpp:41:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<info>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |  for (i = 0; i < a.suf.size(); ++i) {
      |              ~~^~~~~~~~~~~~~~
fish2.cpp:46:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<info>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |    if (i + 1 == a.suf.size()) res.suf.back().cnt = 0;
      |        ~~~~~~^~~~~~~~~~~~~~~
fish2.cpp:54:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<info>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |     if (i == a.suf.size()) break;
      |         ~~^~~~~~~~~~~~~~~
fish2.cpp:60:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<info>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |     if (i == b.pref.size()) break;
      |         ~~^~~~~~~~~~~~~~~~
fish2.cpp:67:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<info>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |     if (j < a.suf.size() && s >= a.suf[j].need) {
      |         ~~^~~~~~~~~~~~~~
fish2.cpp:70:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<info>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     } else if (k < b.pref.size() && s >= b.pref[k].need) {
      |                ~~^~~~~~~~~~~~~~~
fish2.cpp:75:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<info>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |    if (j == a.suf.size()) {
      |        ~~^~~~~~~~~~~~~~~
fish2.cpp:76:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<info>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |     if (k == b.pref.size()) {
      |         ~~^~~~~~~~~~~~~~~~
fish2.cpp:84:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<info>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |    } else if (k == b.pref.size()) {
      |               ~~^~~~~~~~~~~~~~~~
fish2.cpp: In function 'void build(int, int, int)':
fish2.cpp:97:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   97 |   int m = tl + tr >> 1;
      |           ~~~^~~~
fish2.cpp:99:17: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   99 |   build(v + (tr - tl & ~1), m, tr);
      |              ~~~^~~~
fish2.cpp:100:31: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
  100 |   t[v] = t[v + 1] + t[v + (tr - tl & ~1)];
      |                            ~~~^~~~
fish2.cpp: In function 'void update(int, int, int, int, int)':
fish2.cpp:107:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  107 |   int m = tl + tr >> 1;
      |           ~~~^~~~
fish2.cpp:109:29: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
  109 |   else update(i, x, v + (tr - tl & ~1), m, tr);
      |                          ~~~^~~~
fish2.cpp:110:31: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
  110 |   t[v] = t[v + 1] + t[v + (tr - tl & ~1)];
      |                            ~~~^~~~
fish2.cpp: In function 'item query(int, int, int, int, int)':
fish2.cpp:117:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  117 |  int m = tl + tr >> 1;
      |          ~~~^~~~
fish2.cpp:118:57: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
  118 |  return query(l, r, v + 1, tl, m) + query(l, r, v + (tr - tl & ~1), m, tr);
      |                                                      ~~~^~~~
fish2.cpp: In function 'int main()':
fish2.cpp:122:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
fish2.cpp:123:35: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  123 |  for (int i = 0; i < n; ++i) scanf("%d", a + i);
      |                              ~~~~~^~~~~~~~~~~~~
fish2.cpp:125:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |  scanf("%d", &q);
      |  ~~~~~^~~~~~~~~~
fish2.cpp:128:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |   scanf("%d", &type);
      |   ~~~~~^~~~~~~~~~~~~
fish2.cpp:131:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  131 |    scanf("%d%d", &i, &x);
      |    ~~~~~^~~~~~~~~~~~~~~~
fish2.cpp:135:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  135 |    scanf("%d%d", &l, &r);
      |    ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Incorrect 5 ms 9696 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Incorrect 73 ms 29612 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Incorrect 5 ms 9696 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Incorrect 73 ms 29612 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Incorrect 73 ms 29612 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Incorrect 5 ms 9696 KB Output isn't correct
4 Halted 0 ms 0 KB -