답안 #224143

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
224143 2020-04-17T08:49:11 Z shenxy Fire (JOI20_ho_t5) C++11
20 / 100
1000 ms 44920 KB
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, ii> query;
struct seg {
    int s, e, m, v;
    seg *l, *r;
    seg(int _s, int _e) {
        s = _s, e = _e, m = (s + e) / 2, v = 0;
        if (s != e) {
            l = new seg(s, m);
            r = new seg(m + 1, e);
        }
    }
    void update(int i, int nv) {
        if (s != e) {
            if (i > m) r -> update(i, nv);
            else l -> update(i, nv);
            v = max(l -> v, r -> v);
        } else v = nv;
    }
    int query(int a, int b) {
        if (s == a && e == b) return v;
        if (a > m) return r -> query(a, b);
        if (b <= m) return l -> query(a, b);
        return max(l -> query(a, m), r -> query(m + 1, b));
    }
} *root;
struct Fenwick {
	int s;
	vector<long long> f;
	Fenwick(int N) {
		s = N;
		f = vector<long long>(s + 1, 0);
	}
	void update(int i, long long v) {
		for (; i <= s; i += i & -i) f[i] += v;
	}
	long long query(int a) {
		long long ans = 0;
		for (; a > 0; a -= a & -a) ans += f[a];
		return ans;
	}
};
int main() {
	int N, Q;
	scanf("%d %d", &N, &Q);
	long long S[N];
	int T[Q], L[Q], R[Q];
	for (int i = 0; i < N; ++i) scanf("%lld", &S[i]);
	map<int, vector<int> > queries;
	for (int i = 0; i < Q; ++i) scanf("%d %d %d", &T[i], &L[i], &R[i]);
	for (int i = 0; i < Q; ++i) queries[T[i]].push_back(i);
	if (queries.size() == 1) {
		root = new seg(0, N - 1);
		for (int i = 0; i < N; ++i) root -> update(i, S[i]);
		for (int i = 0; i < N; ++i) S[i] = root -> query(max(0, i - queries.begin() -> first), i);
		for (int i = 1; i < N; ++i) S[i] += S[i - 1];
		for (int i = 0; i < Q; ++i) printf("%lld\n", S[R[i] - 1] - (L[i] == 1 ? 0 : S[L[i] - 2]));
	} else if (*max_element(S, S + N) <= 2) {
		int prev1 = -1, prev2 = -1;
		set<query> q;
		Fenwick f(N);
		for (int i = 0; i < N; ++i) f.update(i + 1, S[i]);
		long long ans[Q];
		for (int i = 0; i < N; ++i) {
			if (S[i] == 0) {
				if (prev2 != -1) q.insert(query(ii(i - prev2, -1), ii(2, i + 1)));
				else if (prev1 != -1) q.insert(query(ii(i - prev1, -1), ii(1, i + 1)));
			} else if (S[i] == 1) {
				if (prev2 != -1) q.insert(query(ii(i - prev2, -1), ii(1, i + 1)));
				prev1 = i;
			} else prev2 = i;
		}
		for (int i = 0; i < Q; ++i) q.insert(query(ii(T[i], i), ii(L[i], R[i])));
		for (query i: q) {
			if (i.first.second == -1) f.update(i.second.second, i.second.first);
			else ans[i.first.second] = f.query(i.second.second) - f.query(i.second.first - 1);
		}
		for (int i = 0; i < Q; ++i) printf("%lld\n", ans[i]);
	} else {
		root = new seg(0, N - 1);
		for (int i = 0; i < N; ++i) root -> update(i, S[i]);
		for (int i = 0; i < Q; ++i) {
			long long ans = 0;
			for (int j = L[i] - 1; j < R[i]; ++j) ans += root -> query(max(0, j - T[i]), j);
			printf("%lld\n", ans);
		}
	}
	return 0;
}

Compilation message

ho_t5.cpp: In function 'int main()':
ho_t5.cpp:51:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &N, &Q);
  ~~~~~^~~~~~~~~~~~~~~~~
ho_t5.cpp:54:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i = 0; i < N; ++i) scanf("%lld", &S[i]);
                              ~~~~~^~~~~~~~~~~~~~~
ho_t5.cpp:56:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i = 0; i < Q; ++i) scanf("%d %d %d", &T[i], &L[i], &R[i]);
                              ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 256 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 6 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 6 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 256 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 256 KB Output is correct
19 Correct 5 ms 256 KB Output is correct
20 Correct 5 ms 256 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 256 KB Output is correct
23 Correct 5 ms 256 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 256 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 5 ms 256 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 256 KB Output is correct
2 Correct 215 ms 27368 KB Output is correct
3 Correct 205 ms 26856 KB Output is correct
4 Correct 219 ms 27116 KB Output is correct
5 Correct 228 ms 27372 KB Output is correct
6 Correct 225 ms 27116 KB Output is correct
7 Correct 229 ms 27372 KB Output is correct
8 Correct 233 ms 27496 KB Output is correct
9 Correct 217 ms 27368 KB Output is correct
10 Correct 223 ms 26860 KB Output is correct
11 Correct 217 ms 27408 KB Output is correct
12 Correct 212 ms 26732 KB Output is correct
13 Correct 220 ms 27368 KB Output is correct
14 Correct 214 ms 27244 KB Output is correct
15 Correct 217 ms 27368 KB Output is correct
16 Correct 216 ms 27116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 256 KB Output is correct
2 Correct 437 ms 38772 KB Output is correct
3 Correct 426 ms 38008 KB Output is correct
4 Correct 437 ms 39544 KB Output is correct
5 Correct 412 ms 38136 KB Output is correct
6 Correct 419 ms 38520 KB Output is correct
7 Correct 425 ms 38648 KB Output is correct
8 Correct 435 ms 38648 KB Output is correct
9 Correct 425 ms 38136 KB Output is correct
10 Correct 421 ms 37880 KB Output is correct
11 Correct 439 ms 39292 KB Output is correct
12 Correct 428 ms 38648 KB Output is correct
13 Correct 459 ms 39136 KB Output is correct
14 Correct 424 ms 38392 KB Output is correct
15 Correct 432 ms 39160 KB Output is correct
16 Correct 430 ms 38576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 426 ms 40728 KB Output is correct
2 Correct 436 ms 43896 KB Output is correct
3 Correct 441 ms 44920 KB Output is correct
4 Correct 452 ms 44664 KB Output is correct
5 Correct 436 ms 44536 KB Output is correct
6 Correct 437 ms 43844 KB Output is correct
7 Correct 428 ms 44280 KB Output is correct
8 Correct 431 ms 44536 KB Output is correct
9 Correct 440 ms 44664 KB Output is correct
10 Correct 423 ms 44024 KB Output is correct
11 Correct 430 ms 44860 KB Output is correct
12 Correct 502 ms 44664 KB Output is correct
13 Correct 430 ms 44536 KB Output is correct
14 Correct 439 ms 44784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 256 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 6 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 6 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 256 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 256 KB Output is correct
19 Correct 5 ms 256 KB Output is correct
20 Correct 5 ms 256 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 256 KB Output is correct
23 Correct 5 ms 256 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 256 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 5 ms 256 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Execution timed out 1084 ms 36728 KB Time limit exceeded
34 Halted 0 ms 0 KB -