Submission #886568

# Submission time Handle Problem Language Result Execution time Memory
886568 2023-12-12T10:29:54 Z marvinthang Fire (JOI20_ho_t5) C++17
100 / 100
289 ms 26076 KB
/*************************************
*    author: marvinthang             *
*    created: 12.12.2023 15:28:33    *
*************************************/

#include <bits/stdc++.h>

using namespace std;

#define                  fi  first
#define                  se  second
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define              ALL(v)  (v).begin(), (v).end()
#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define          REPD(i, n)  for (int i = (n); i-- > 0; )
#define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i) 
#define       FORD(i, b, a)  for (int i = (b), _a = (a); --i >= _a; ) 
#define       FORE(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define      FORDE(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i) 
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
    #include "debug.h"
#else
    #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
    #define DB(...) 23
    #define db(...) 23
    #define debug(...) 23
#endif

template <class U, class V> scan_op(pair <U, V>)  { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>)  { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")";  else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }

// index from 0
template <class T> struct Fenwick {
    int n; vector <T> bit;
    Fenwick(int _n = 0): n(_n), bit(n + 1, 0) {}
    void reset(void) { fill(bit.begin(), bit.end(), 0); }
    void resize(int _n) { bit.assign(_n + 1, 0); }
    void update(int i, T val) { for (++i; i <= n; i += i & -i) bit[i] += val; }
    T get(int i) {
        if (i < 0) return 0;
        T res(0);
        for (i = min(i + 1, n); i > 0; i &= i - 1) res += bit[i];
        return res;
    }
    int upper_bound(T val) {
        int res = 0;
        for (int i = __lg(n); i >= 0; --i) 
            if ((res | MASK(i)) <= n && val >= bit[res | MASK(i)]) res |= MASK(i), val -= bit[res];
        return res;
    }
    int lower_bound(T val) {
        int res = 0;
        for (int i = __lg(n); i >= 0; --i)
            if ((res | MASK(i)) <= n && val > bit[res | MASK(i)]) res |= MASK(i), val -= bit[res];
        return res;
    }
};

// end of template

void process(void) {
	int n, q; cin >> n >> q;
	vector <int> s(n); cin >> s;
	vector <vector <pair <int, int>>> queriesAt(n);
	REP(i, q) {
		int t, l, r; cin >> t >> l >> r; --l; --r;
		queriesAt[r].emplace_back(i + 1, t);
		if (l) queriesAt[l - 1].emplace_back(-1 - i, t);
	}
	vector <int> st;
	Fenwick <long long> sum_idx(n + 1), sum_const(n + 1);
	auto update = [&] (int i, int v) {
		// sum[x] += (x - i + 1) * v;
		sum_const.update(i, 1LL * (i - 1) * v);
		sum_idx.update(i, v);
	};
	auto get = [&] (int i) {
		return sum_idx.get(i) * i - sum_const.get(i);
	};
	vector <long long> sum_st;
	vector <long long> res(q);
	REP(i, n) {
		while (!st.empty() && s[st.back()] <= s[i]) {
			int j = st.back(); st.pop_back(); sum_st.pop_back();
			update(i - j, -s[j]);
			if (!st.empty()) update(i - st.back(), s[j]);
		}
		update(0, s[i]);
		if (!st.empty()) {
			update(i - st.back(), -s[i]);
			sum_st.push_back(1LL * (i - st.back()) * s[i] + sum_st.back());
		} else sum_st.push_back(0);
		st.push_back(i);
		for (auto [x, t]: queriesAt[i]) {
			long long cur = get(t);
			// i - f <= t => f >= i - t
			int p = lower_bound(ALL(st), i - t) - st.begin();
			cur -= sum_st.back() - sum_st[p] + 1LL * (t + st[p] - i) * s[st[p]];
			if (x > 0) res[x - 1] += cur;
			else res[-1 - x] -= cur;
		}
	}
	REP(i, q) cout << res[i] << '\n';
}

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr);
	file("JOI20_ho_t5");
	// int t; cin >> t; while (t--)
	process();
	// cerr << "Time elapsed: " << TIME << " s.\n";
	return (0^0);
}

Compilation message

ho_t5.cpp: In function 'int main()':
ho_t5.cpp:30:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
ho_t5.cpp:119:2: note: in expansion of macro 'file'
  119 |  file("JOI20_ho_t5");
      |  ^~~~
ho_t5.cpp:30:94: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
ho_t5.cpp:119:2: note: in expansion of macro 'file'
  119 |  file("JOI20_ho_t5");
      |  ^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 452 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 448 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 282 ms 25488 KB Output is correct
3 Correct 247 ms 25408 KB Output is correct
4 Correct 242 ms 25516 KB Output is correct
5 Correct 248 ms 25424 KB Output is correct
6 Correct 240 ms 25168 KB Output is correct
7 Correct 248 ms 25680 KB Output is correct
8 Correct 242 ms 25680 KB Output is correct
9 Correct 234 ms 25680 KB Output is correct
10 Correct 229 ms 25428 KB Output is correct
11 Correct 251 ms 26076 KB Output is correct
12 Correct 255 ms 25168 KB Output is correct
13 Correct 242 ms 25392 KB Output is correct
14 Correct 236 ms 25316 KB Output is correct
15 Correct 248 ms 25452 KB Output is correct
16 Correct 229 ms 25212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 249 ms 24916 KB Output is correct
3 Correct 231 ms 24364 KB Output is correct
4 Correct 250 ms 25164 KB Output is correct
5 Correct 245 ms 24404 KB Output is correct
6 Correct 255 ms 24656 KB Output is correct
7 Correct 246 ms 24712 KB Output is correct
8 Correct 250 ms 24912 KB Output is correct
9 Correct 244 ms 24436 KB Output is correct
10 Correct 289 ms 24272 KB Output is correct
11 Correct 243 ms 24948 KB Output is correct
12 Correct 275 ms 24432 KB Output is correct
13 Correct 253 ms 24916 KB Output is correct
14 Correct 251 ms 24588 KB Output is correct
15 Correct 268 ms 24916 KB Output is correct
16 Correct 239 ms 24400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 202 ms 22100 KB Output is correct
2 Correct 207 ms 21872 KB Output is correct
3 Correct 235 ms 22492 KB Output is correct
4 Correct 204 ms 22156 KB Output is correct
5 Correct 211 ms 22148 KB Output is correct
6 Correct 213 ms 21840 KB Output is correct
7 Correct 202 ms 22352 KB Output is correct
8 Correct 223 ms 22248 KB Output is correct
9 Correct 195 ms 22336 KB Output is correct
10 Correct 223 ms 22168 KB Output is correct
11 Correct 234 ms 22376 KB Output is correct
12 Correct 204 ms 22296 KB Output is correct
13 Correct 203 ms 22356 KB Output is correct
14 Correct 218 ms 22352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 452 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 448 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 282 ms 25488 KB Output is correct
34 Correct 247 ms 25408 KB Output is correct
35 Correct 242 ms 25516 KB Output is correct
36 Correct 248 ms 25424 KB Output is correct
37 Correct 240 ms 25168 KB Output is correct
38 Correct 248 ms 25680 KB Output is correct
39 Correct 242 ms 25680 KB Output is correct
40 Correct 234 ms 25680 KB Output is correct
41 Correct 229 ms 25428 KB Output is correct
42 Correct 251 ms 26076 KB Output is correct
43 Correct 255 ms 25168 KB Output is correct
44 Correct 242 ms 25392 KB Output is correct
45 Correct 236 ms 25316 KB Output is correct
46 Correct 248 ms 25452 KB Output is correct
47 Correct 229 ms 25212 KB Output is correct
48 Correct 249 ms 24916 KB Output is correct
49 Correct 231 ms 24364 KB Output is correct
50 Correct 250 ms 25164 KB Output is correct
51 Correct 245 ms 24404 KB Output is correct
52 Correct 255 ms 24656 KB Output is correct
53 Correct 246 ms 24712 KB Output is correct
54 Correct 250 ms 24912 KB Output is correct
55 Correct 244 ms 24436 KB Output is correct
56 Correct 289 ms 24272 KB Output is correct
57 Correct 243 ms 24948 KB Output is correct
58 Correct 275 ms 24432 KB Output is correct
59 Correct 253 ms 24916 KB Output is correct
60 Correct 251 ms 24588 KB Output is correct
61 Correct 268 ms 24916 KB Output is correct
62 Correct 239 ms 24400 KB Output is correct
63 Correct 202 ms 22100 KB Output is correct
64 Correct 207 ms 21872 KB Output is correct
65 Correct 235 ms 22492 KB Output is correct
66 Correct 204 ms 22156 KB Output is correct
67 Correct 211 ms 22148 KB Output is correct
68 Correct 213 ms 21840 KB Output is correct
69 Correct 202 ms 22352 KB Output is correct
70 Correct 223 ms 22248 KB Output is correct
71 Correct 195 ms 22336 KB Output is correct
72 Correct 223 ms 22168 KB Output is correct
73 Correct 234 ms 22376 KB Output is correct
74 Correct 204 ms 22296 KB Output is correct
75 Correct 203 ms 22356 KB Output is correct
76 Correct 218 ms 22352 KB Output is correct
77 Correct 261 ms 25416 KB Output is correct
78 Correct 261 ms 25920 KB Output is correct
79 Correct 269 ms 25568 KB Output is correct
80 Correct 257 ms 25396 KB Output is correct
81 Correct 267 ms 25240 KB Output is correct
82 Correct 244 ms 25504 KB Output is correct
83 Correct 248 ms 25248 KB Output is correct
84 Correct 225 ms 25428 KB Output is correct
85 Correct 254 ms 25400 KB Output is correct
86 Correct 248 ms 25424 KB Output is correct
87 Correct 247 ms 25940 KB Output is correct
88 Correct 248 ms 25588 KB Output is correct
89 Correct 270 ms 25356 KB Output is correct
90 Correct 256 ms 25620 KB Output is correct
91 Correct 252 ms 25400 KB Output is correct
92 Correct 252 ms 25424 KB Output is correct
93 Correct 254 ms 25428 KB Output is correct
94 Correct 245 ms 25948 KB Output is correct
95 Correct 250 ms 25940 KB Output is correct
96 Correct 252 ms 25424 KB Output is correct
97 Correct 265 ms 25532 KB Output is correct
98 Correct 249 ms 25232 KB Output is correct
99 Correct 233 ms 24916 KB Output is correct
100 Correct 285 ms 25340 KB Output is correct
101 Correct 231 ms 25092 KB Output is correct
102 Correct 245 ms 25684 KB Output is correct