Submission #885755

# Submission time Handle Problem Language Result Execution time Memory
885755 2023-12-10T16:01:44 Z qin Fire (JOI20_ho_t5) C++17
100 / 100
266 ms 54980 KB
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define fi first
#define se second
#define pn printf("\n")
#define ssize(x) int(x.size())
#define bitcount(x) __builtin_popcount(x)
#define clz(x) __builtin_clz(x)
#define ctz(x) __builtin_ctz(x)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef double db;
typedef long double ldb;
#define V vector
void read(int &a){
		char c = getchar_unlocked(); a = 0;
		while(c<'0' || '9'<c) c = getchar_unlocked();
		while('0'<=c&&c<='9') a = (a<<3)+(a<<1)+c-'0', c = getchar_unlocked();
}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif
int inf = 2e09; ll infll = 2e18; int mod = (1<<23)*119+1;
int base = 1;
struct seg{
		struct Node{
				ll a, b; int la, lb;
				Node(): a(0), b(0), la(0), lb(0) {}
				Node(ll aa, ll bb, int laa, int lbb) : a(aa), b(bb), la(laa), lb(lbb) {}
		};
		vector<Node> t;
		vector<ll> tab;
		void merge(int &i){
				t[i].a = t[i<<1].a + t[i<<1|1].a, t[i].b = t[i<<1].b + t[i<<1|1].b;
				t[i].la = t[i<<1].la + t[i<<1|1].la, t[i].lb = t[i<<1].lb + t[i<<1|1].lb;
		}
		void init(int n, vector<ll> &a){
				while(base < n) base <<= 1;
				t.resize(base<<1); tab = a;
				for(int i = 1; i <= n; ++i) t[i+base-1] = Node(a[i], a[i], 1, 1);
				for(int i = base-1; i; --i) merge(i);
		}
		void update_point(int i, ll a, ll b, int la, int lb){ for(i += base-1, t[i] = Node(a, b, la, lb), i >>= 1; i; i >>= 1) merge(i); }
		ll first_k(int i, int s, int e, int &x, int &y, int &k, int &T){
				if(!k) return 0;
				int mid = (s+e)>>1;
				if(x <= s && e <= y){
						int sum = t[i].la*T + t[i].lb;
						if(sum <= k){ k -= sum; return t[i].a*T + t[i].b; }
						else{
								if(s != e) return first_k(i<<1, s, mid, x, y, k, T) + first_k(i<<1|1, mid+1, e, x, y, k, T);
								else{ ll ret = tab[s]*k; k = 0; return ret; }
						}
				} ll ret = 0;
				if(x <= mid) ret += first_k(i<<1, s, mid, x, y, k, T);
				if(mid < y) ret += first_k(i<<1|1, mid+1, e, x, y, k, T);
				return ret;
		}
		ll query_interval(int i, int s, int e, int &x, int &y, int &T){
				if(x <= s && e <= y) return t[i].a*T + t[i].b;
				int mid = (s+e)>>1; ll ret = 0;
				if(x <= mid) ret += query_interval(i<<1, s, mid, x, y, T);
				if(mid < y) ret += query_interval(i<<1|1, mid+1, e, x, y, T);
				return ret;
		}
		ll query(int x, int y, int T){
				if(x > y) return 0;
				return query_interval(1, 1, base, x, y, T);
		}
		ll firstk(int x, int y, int k, int T){
				if(x > y) return 0;
				return first_k(1, 1, base, x, y, k, T);
		}
};
struct ev{
		int t, i;
		ev(){}
		ev(int tt, int ii) : t(tt), i(ii) {}
		bool operator <(const ev &x) const{ return t < x.t; }
};
struct qr{
		int a, b, i;
		qr(){}
		qr(int aa, int bb, int ii) : a(aa), b(bb), i(ii) {}
};
void answer(){
		int n, q, tt; scanf("%d%d", &n, &q);
		vector<ll> t(n+1);
		vector<int> nxt(n+1), bef(n+1);
		for(int i = 1; i <= n; ++i) read(tt), t[i] = tt;
		seg seg; seg.init(n, t);
		
		vector<int> st;
		for(int i = 1; i <= n; ++i){
				while(!st.empty() && t[st.back()] <= t[i]) st.pop_back();
				if(st.empty()) bef[i] = n+1;
				else bef[i] = i-st.back();
				st.emplace_back(i);
		} st = vector<int>();
		for(int i = n; i; --i){
				while(!st.empty() && t[st.back()] < t[i]) st.pop_back();
				if(st.empty()) nxt[i] = n+1-i;
				else nxt[i] = st.back()-i;
				st.emplace_back(i);
		} st = vector<int>();
		
		vector<vector<ev>> event(n+2);
		set<int> active;
		for(int i = 1; i <= n; ++i) event[min(n+1, min(bef[i], nxt[i]))].emplace_back(0, i), event[min(n+1, max(bef[i], nxt[i]))].emplace_back(1, i),
									event[min(n+1, min(bef[i], nxt[i])+max(bef[i], nxt[i]))].emplace_back(2, i), active.emplace(i);
									
		vector<vector<qr>> queries(n+1);
		for(int i = 1; i <= q; ++i){ int a, b, T; read(T), read(a), read(b), queries[T].emplace_back(a, b, i);}
		vector<ll> result(q+1);
		
		for(int T = 1; T <= n; ++T){
				for(ev &u : event[T]){
						if(!u.t){
								if(u.i != 1) active.erase(u.i);
								seg.update_point(u.i, 0, t[u.i]*T, 0, T);
						} else if(u.t == 1) seg.update_point(u.i, -t[u.i], t[u.i]*(min(bef[u.i], nxt[u.i])+T-1), -1, min(bef[u.i], nxt[u.i])+T-1);
						else seg.update_point(u.i, 0, 0, 0, 0);
				}
				for(qr &u : queries[T]){
						auto it1 = active.upper_bound(u.a); it1 = prev(it1);
						auto it2 = active.upper_bound(u.b); it2 = prev(it2);
						result[u.i] = seg.query(*it1, (*it2)-1, T)-seg.firstk(*it1, u.a-1, u.a-(*it1), T)+seg.firstk(*it2, u.b, u.b-(*it2)+1, T);
				}
		}
		for(int i = 1; i <= q; ++i) printf("%lld\n", result[i]);
}
int main(){
		int T = 1; //scanf("%d", &T);
		//ios_base::sync_with_stdio(0); cin.tie(0); cin >> T;
		for(++T; --T; ) answer();
		return 0;
}

Compilation message

ho_t5.cpp: In function 'void answer()':
ho_t5.cpp:93:22: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |   int n, q, tt; scanf("%d%d", &n, &q);
      |                 ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 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 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 344 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 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 187 ms 47020 KB Output is correct
3 Correct 192 ms 46728 KB Output is correct
4 Correct 194 ms 47280 KB Output is correct
5 Correct 182 ms 47720 KB Output is correct
6 Correct 201 ms 47156 KB Output is correct
7 Correct 195 ms 47288 KB Output is correct
8 Correct 209 ms 48032 KB Output is correct
9 Correct 191 ms 49696 KB Output is correct
10 Correct 183 ms 46644 KB Output is correct
11 Correct 201 ms 49076 KB Output is correct
12 Correct 184 ms 46756 KB Output is correct
13 Correct 211 ms 47856 KB Output is correct
14 Correct 177 ms 47980 KB Output is correct
15 Correct 190 ms 48432 KB Output is correct
16 Correct 216 ms 47196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 219 ms 48092 KB Output is correct
3 Correct 207 ms 47532 KB Output is correct
4 Correct 203 ms 48872 KB Output is correct
5 Correct 196 ms 47896 KB Output is correct
6 Correct 216 ms 48244 KB Output is correct
7 Correct 216 ms 48344 KB Output is correct
8 Correct 218 ms 48152 KB Output is correct
9 Correct 199 ms 47800 KB Output is correct
10 Correct 213 ms 47344 KB Output is correct
11 Correct 210 ms 48944 KB Output is correct
12 Correct 204 ms 48568 KB Output is correct
13 Correct 224 ms 48704 KB Output is correct
14 Correct 200 ms 47800 KB Output is correct
15 Correct 198 ms 48720 KB Output is correct
16 Correct 205 ms 48308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 217 ms 46884 KB Output is correct
2 Correct 245 ms 50824 KB Output is correct
3 Correct 220 ms 51880 KB Output is correct
4 Correct 255 ms 51040 KB Output is correct
5 Correct 209 ms 50940 KB Output is correct
6 Correct 240 ms 51268 KB Output is correct
7 Correct 222 ms 51916 KB Output is correct
8 Correct 247 ms 51492 KB Output is correct
9 Correct 217 ms 51012 KB Output is correct
10 Correct 266 ms 51472 KB Output is correct
11 Correct 217 ms 51200 KB Output is correct
12 Correct 241 ms 51316 KB Output is correct
13 Correct 231 ms 51136 KB Output is correct
14 Correct 225 ms 51348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 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 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 344 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 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 187 ms 47020 KB Output is correct
34 Correct 192 ms 46728 KB Output is correct
35 Correct 194 ms 47280 KB Output is correct
36 Correct 182 ms 47720 KB Output is correct
37 Correct 201 ms 47156 KB Output is correct
38 Correct 195 ms 47288 KB Output is correct
39 Correct 209 ms 48032 KB Output is correct
40 Correct 191 ms 49696 KB Output is correct
41 Correct 183 ms 46644 KB Output is correct
42 Correct 201 ms 49076 KB Output is correct
43 Correct 184 ms 46756 KB Output is correct
44 Correct 211 ms 47856 KB Output is correct
45 Correct 177 ms 47980 KB Output is correct
46 Correct 190 ms 48432 KB Output is correct
47 Correct 216 ms 47196 KB Output is correct
48 Correct 219 ms 48092 KB Output is correct
49 Correct 207 ms 47532 KB Output is correct
50 Correct 203 ms 48872 KB Output is correct
51 Correct 196 ms 47896 KB Output is correct
52 Correct 216 ms 48244 KB Output is correct
53 Correct 216 ms 48344 KB Output is correct
54 Correct 218 ms 48152 KB Output is correct
55 Correct 199 ms 47800 KB Output is correct
56 Correct 213 ms 47344 KB Output is correct
57 Correct 210 ms 48944 KB Output is correct
58 Correct 204 ms 48568 KB Output is correct
59 Correct 224 ms 48704 KB Output is correct
60 Correct 200 ms 47800 KB Output is correct
61 Correct 198 ms 48720 KB Output is correct
62 Correct 205 ms 48308 KB Output is correct
63 Correct 217 ms 46884 KB Output is correct
64 Correct 245 ms 50824 KB Output is correct
65 Correct 220 ms 51880 KB Output is correct
66 Correct 255 ms 51040 KB Output is correct
67 Correct 209 ms 50940 KB Output is correct
68 Correct 240 ms 51268 KB Output is correct
69 Correct 222 ms 51916 KB Output is correct
70 Correct 247 ms 51492 KB Output is correct
71 Correct 217 ms 51012 KB Output is correct
72 Correct 266 ms 51472 KB Output is correct
73 Correct 217 ms 51200 KB Output is correct
74 Correct 241 ms 51316 KB Output is correct
75 Correct 231 ms 51136 KB Output is correct
76 Correct 225 ms 51348 KB Output is correct
77 Correct 223 ms 54340 KB Output is correct
78 Correct 218 ms 54816 KB Output is correct
79 Correct 247 ms 54980 KB Output is correct
80 Correct 210 ms 54368 KB Output is correct
81 Correct 210 ms 54204 KB Output is correct
82 Correct 221 ms 54796 KB Output is correct
83 Correct 263 ms 54500 KB Output is correct
84 Correct 221 ms 54052 KB Output is correct
85 Correct 215 ms 54936 KB Output is correct
86 Correct 220 ms 54228 KB Output is correct
87 Correct 208 ms 54548 KB Output is correct
88 Correct 235 ms 54524 KB Output is correct
89 Correct 195 ms 53432 KB Output is correct
90 Correct 205 ms 54396 KB Output is correct
91 Correct 196 ms 54072 KB Output is correct
92 Correct 190 ms 53392 KB Output is correct
93 Correct 192 ms 54072 KB Output is correct
94 Correct 216 ms 54748 KB Output is correct
95 Correct 197 ms 54652 KB Output is correct
96 Correct 209 ms 53908 KB Output is correct
97 Correct 198 ms 53996 KB Output is correct
98 Correct 245 ms 53424 KB Output is correct
99 Correct 232 ms 53616 KB Output is correct
100 Correct 237 ms 54440 KB Output is correct
101 Correct 228 ms 53820 KB Output is correct
102 Correct 242 ms 54448 KB Output is correct