Submission #203380

# Submission time Handle Problem Language Result Execution time Memory
203380 2020-02-20T12:25:47 Z Bruteforceman Fire (JOI20_ho_t5) C++11
14 / 100
1000 ms 70376 KB
#include <bits/stdc++.h>
using namespace std;
#define prev previous
const int maxn = 200005;
using pii = pair <int, int>;

int n;
int s[maxn];
struct query {
	int l, r, t;
} Q[maxn];
vector <int> v[maxn];
int prev[maxn];
vector <int> g[maxn];
int sub[maxn];
long long pred[maxn];
long long pref[maxn];
vector <pii> h[maxn];

void eulerOrder(int x) {
	for(int i : g[x]) {
		eulerOrder(i);
		pred[i] = sub[x];
		sub[x] += sub[i]; 
	}
	sub[x] += 1;
}
struct BinaryIndexedTree {
	long long t[maxn]; 
	int size;
	void update(int x, long long val) {
		x += 1;
		for(int i = x; i <= size + 1; i += i & (-i)) {
			t[i] += val;
		}
	}
	long long sum(int x) {
		x = min(x, size);
		x += 1;
		long long ans = 0;
		for(int i = x; i > 0; i -= i & (-i)) {
			ans += t[i];
		}
		return ans;
	}
	long long sum(int l, int r) {
		if(l > r) return 0;
		return sum(r) - sum(l - 1);
	}
};
int main() {
	int q;
	scanf("%d %d", &n, &q);
	for_each(s + 1, s + n + 1, [] (int &i) { scanf("%d", &i); });
	for_each(Q + 1, Q + q + 1, [&] (query &i) { 
					scanf("%d %d %d", &i.t, &i.l, &i.r); 
					v[i.r].emplace_back(i.t);
					v[i.l - 1].emplace_back(i.t); });
	for(int i = 1; i <= n; i++) {
		pref[i] = pref[i - 1] + s[i];
	}
	stack <int> st;
	for(int i = 1; i <= n; i++) {
		while(!st.empty() && s[st.top()] < s[i]) {
			st.pop();
		} 
		prev[i] = st.empty() ? 0 : st.top();
		st.push(i);
	}
	for(int i = 1; i <= n; i++) {
		g[prev[i]].push_back(i);
	}
	eulerOrder(0);
	map <pii, long long> res;
	auto cost = [&] (int x) {
		if(prev[x] == 0) return 0;
		else return s[prev[x]] - s[x];
	};
	BinaryIndexedTree A, B, C, D;
	A.size = B.size = C.size = D.size = n;
	vector <int> rmq;;
	for(int i = 1; i <= n; i++) {
		while(!rmq.empty() && s[rmq.back()] < s[i]) {
			rmq.pop_back();
		}
		rmq.push_back(i);
		for(int t : v[i]) {
			if(res.find(pii(i, t)) != res.end()) continue;
			int last = *lower_bound(rmq.begin(), rmq.end(), i - t);
			long long ans = 1LL * s[last] * (i - last) + pref[last];
			h[last].emplace_back(i, t);
			res[pii(i, t)] += ans;
		}
	}
	for(int i = 1; i <= n; i++) {
		int k = i;
		A.update(pred[k], 1LL * cost(k) * pred[k]);
		B.update(pred[k], 1LL * cost(k));
		C.update(pred[k] + sub[k], 1LL * cost(k) * (sub[k] + pred[k]));
		D.update(pred[k] + sub[k], 1LL * cost(k));	
		for(auto j : h[i]) {
			int t = j.second;
			res[j] += B.sum(t) * t - A.sum(t);
			res[j] += C.sum(t) - D.sum(t) * t;
		}
	}
	for_each(Q + 1, Q + q + 1, [&] (query i) { 
				printf("%lld\n", res[pii(i.r, i.t)] - res[pii(i.l - 1, i.t)]); });
	return 0;
}

Compilation message

ho_t5.cpp: In function 'int main()':
ho_t5.cpp:53: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: In lambda function:
ho_t5.cpp:54:48: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for_each(s + 1, s + n + 1, [] (int &i) { scanf("%d", &i); });
                                           ~~~~~^~~~~~~~~~
ho_t5.cpp: In lambda function:
ho_t5.cpp:56:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
      scanf("%d %d %d", &i.t, &i.l, &i.r); 
      ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14456 KB Output is correct
2 Correct 13 ms 14456 KB Output is correct
3 Correct 13 ms 14456 KB Output is correct
4 Correct 14 ms 14584 KB Output is correct
5 Correct 13 ms 14456 KB Output is correct
6 Correct 14 ms 14584 KB Output is correct
7 Correct 14 ms 14456 KB Output is correct
8 Correct 14 ms 14584 KB Output is correct
9 Correct 14 ms 14584 KB Output is correct
10 Correct 13 ms 14456 KB Output is correct
11 Correct 13 ms 14456 KB Output is correct
12 Correct 13 ms 14456 KB Output is correct
13 Correct 13 ms 14456 KB Output is correct
14 Correct 13 ms 14460 KB Output is correct
15 Correct 13 ms 14456 KB Output is correct
16 Correct 14 ms 14456 KB Output is correct
17 Correct 13 ms 14456 KB Output is correct
18 Correct 13 ms 14456 KB Output is correct
19 Correct 13 ms 14456 KB Output is correct
20 Correct 14 ms 14456 KB Output is correct
21 Correct 15 ms 14456 KB Output is correct
22 Correct 13 ms 14456 KB Output is correct
23 Correct 13 ms 14456 KB Output is correct
24 Correct 13 ms 14456 KB Output is correct
25 Correct 14 ms 14456 KB Output is correct
26 Correct 13 ms 14456 KB Output is correct
27 Correct 13 ms 14456 KB Output is correct
28 Correct 13 ms 14456 KB Output is correct
29 Correct 13 ms 14456 KB Output is correct
30 Correct 13 ms 14456 KB Output is correct
31 Correct 13 ms 14456 KB Output is correct
32 Correct 13 ms 14460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14456 KB Output is correct
2 Correct 669 ms 45808 KB Output is correct
3 Correct 698 ms 45556 KB Output is correct
4 Correct 649 ms 45800 KB Output is correct
5 Correct 632 ms 45804 KB Output is correct
6 Correct 580 ms 45788 KB Output is correct
7 Correct 624 ms 46316 KB Output is correct
8 Correct 634 ms 46312 KB Output is correct
9 Correct 711 ms 46204 KB Output is correct
10 Correct 671 ms 45500 KB Output is correct
11 Correct 626 ms 46196 KB Output is correct
12 Correct 627 ms 45292 KB Output is correct
13 Correct 561 ms 45940 KB Output is correct
14 Correct 587 ms 45932 KB Output is correct
15 Correct 590 ms 46188 KB Output is correct
16 Correct 587 ms 45812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14456 KB Output is correct
2 Correct 712 ms 62056 KB Output is correct
3 Correct 705 ms 61032 KB Output is correct
4 Correct 734 ms 62568 KB Output is correct
5 Correct 704 ms 60904 KB Output is correct
6 Correct 699 ms 61160 KB Output is correct
7 Correct 699 ms 61408 KB Output is correct
8 Correct 719 ms 62056 KB Output is correct
9 Correct 711 ms 61504 KB Output is correct
10 Correct 696 ms 60632 KB Output is correct
11 Correct 721 ms 62228 KB Output is correct
12 Correct 714 ms 61144 KB Output is correct
13 Correct 694 ms 62308 KB Output is correct
14 Correct 685 ms 61544 KB Output is correct
15 Correct 685 ms 61544 KB Output is correct
16 Correct 683 ms 60776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1075 ms 70376 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14456 KB Output is correct
2 Correct 13 ms 14456 KB Output is correct
3 Correct 13 ms 14456 KB Output is correct
4 Correct 14 ms 14584 KB Output is correct
5 Correct 13 ms 14456 KB Output is correct
6 Correct 14 ms 14584 KB Output is correct
7 Correct 14 ms 14456 KB Output is correct
8 Correct 14 ms 14584 KB Output is correct
9 Correct 14 ms 14584 KB Output is correct
10 Correct 13 ms 14456 KB Output is correct
11 Correct 13 ms 14456 KB Output is correct
12 Correct 13 ms 14456 KB Output is correct
13 Correct 13 ms 14456 KB Output is correct
14 Correct 13 ms 14460 KB Output is correct
15 Correct 13 ms 14456 KB Output is correct
16 Correct 14 ms 14456 KB Output is correct
17 Correct 13 ms 14456 KB Output is correct
18 Correct 13 ms 14456 KB Output is correct
19 Correct 13 ms 14456 KB Output is correct
20 Correct 14 ms 14456 KB Output is correct
21 Correct 15 ms 14456 KB Output is correct
22 Correct 13 ms 14456 KB Output is correct
23 Correct 13 ms 14456 KB Output is correct
24 Correct 13 ms 14456 KB Output is correct
25 Correct 14 ms 14456 KB Output is correct
26 Correct 13 ms 14456 KB Output is correct
27 Correct 13 ms 14456 KB Output is correct
28 Correct 13 ms 14456 KB Output is correct
29 Correct 13 ms 14456 KB Output is correct
30 Correct 13 ms 14456 KB Output is correct
31 Correct 13 ms 14456 KB Output is correct
32 Correct 13 ms 14460 KB Output is correct
33 Correct 800 ms 62264 KB Output is correct
34 Correct 836 ms 63088 KB Output is correct
35 Correct 798 ms 62056 KB Output is correct
36 Correct 793 ms 62440 KB Output is correct
37 Correct 817 ms 61944 KB Output is correct
38 Correct 827 ms 62160 KB Output is correct
39 Correct 787 ms 61928 KB Output is correct
40 Correct 799 ms 62696 KB Output is correct
41 Correct 773 ms 61672 KB Output is correct
42 Correct 809 ms 62568 KB Output is correct
43 Correct 799 ms 62688 KB Output is correct
44 Correct 767 ms 62440 KB Output is correct
45 Correct 771 ms 62032 KB Output is correct
46 Correct 771 ms 61924 KB Output is correct
47 Correct 777 ms 61148 KB Output is correct
48 Correct 762 ms 61152 KB Output is correct
49 Correct 769 ms 61536 KB Output is correct
50 Correct 766 ms 62568 KB Output is correct
51 Correct 785 ms 62824 KB Output is correct
52 Correct 792 ms 62176 KB Output is correct
53 Correct 775 ms 62428 KB Output is correct
54 Correct 783 ms 61812 KB Output is correct
55 Correct 769 ms 62064 KB Output is correct
56 Correct 799 ms 62320 KB Output is correct
57 Correct 770 ms 61684 KB Output is correct
58 Correct 799 ms 63088 KB Output is correct
59 Correct 669 ms 45808 KB Output is correct
60 Correct 698 ms 45556 KB Output is correct
61 Correct 649 ms 45800 KB Output is correct
62 Correct 632 ms 45804 KB Output is correct
63 Correct 580 ms 45788 KB Output is correct
64 Correct 624 ms 46316 KB Output is correct
65 Correct 634 ms 46312 KB Output is correct
66 Correct 711 ms 46204 KB Output is correct
67 Correct 671 ms 45500 KB Output is correct
68 Correct 626 ms 46196 KB Output is correct
69 Correct 627 ms 45292 KB Output is correct
70 Correct 561 ms 45940 KB Output is correct
71 Correct 587 ms 45932 KB Output is correct
72 Correct 590 ms 46188 KB Output is correct
73 Correct 587 ms 45812 KB Output is correct
74 Correct 712 ms 62056 KB Output is correct
75 Correct 705 ms 61032 KB Output is correct
76 Correct 734 ms 62568 KB Output is correct
77 Correct 704 ms 60904 KB Output is correct
78 Correct 699 ms 61160 KB Output is correct
79 Correct 699 ms 61408 KB Output is correct
80 Correct 719 ms 62056 KB Output is correct
81 Correct 711 ms 61504 KB Output is correct
82 Correct 696 ms 60632 KB Output is correct
83 Correct 721 ms 62228 KB Output is correct
84 Correct 714 ms 61144 KB Output is correct
85 Correct 694 ms 62308 KB Output is correct
86 Correct 685 ms 61544 KB Output is correct
87 Correct 685 ms 61544 KB Output is correct
88 Correct 683 ms 60776 KB Output is correct
89 Execution timed out 1075 ms 70376 KB Time limit exceeded
90 Halted 0 ms 0 KB -