답안 #942157

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
942157 2024-03-10T10:19:30 Z pan 3단 점프 (JOI19_jumps) C++17
100 / 100
979 ms 148052 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include "bits_stdc++.h"
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
using namespace std;
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, srb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> piii;
typedef pair<pi, pi> pii;
ll const INF = 1e9;
ll n, q, l, r;
ll arr[500005], ans[500005];
vector<ll> nxt[500005];
vector<pi> queries[500005];
struct node{

    int s, e, m; 
    ll aval, summ, pv;
    node *l, *r; 
	node (int S, int E)
	{ 
       s = S, e = E, m = (s+e)/2;
       summ = pv = -INF;
       if (s==e) {aval = arr[s]; return;}
       l = new node(s, m); 
	   r = new node(m+1, e);
	   aval = max(l->aval, r->aval);

    }

	void propogate()
	{
	   if (s==e) return;
       l->summ = max(l->summ, l->aval + pv);
       r->summ = max(r->summ, r->aval + pv);
       l->pv = max(l->pv, pv);
       r->pv = max(r->pv, pv);
    }
    
	void update(int S, int E, ll V)
	{ 
       if(s==S && e==E) 
       {
		   pv = max(pv, V);
		   summ = max(summ, pv+aval);
		   
	   }
       else{ 
		   propogate();
           if(E <= m) l->update(S, E, V); 
           else if (m < S) r->update(S, E, V); 
           else l->update(S, m, V),r->update(m+1, E, V);
           l->propogate(),r->propogate();
		   summ = max(l->summ, r->summ);
          
       }
    }
	ll query(int S, int E)
	{
       if(s == S && e == E) return summ; 
       propogate(); 
       if(E <= m) return l->query(S, E); 
       else if(S >= m+1) return r->query(S, E); 
       else return max(l->query(S, m), r->query(m+1, E)); 
   }

} *root;
 


int main()
{
	input(n);
	deque<ll> dq;
	for (ll i=0; i<n; ++i) input(arr[i]);
	input(q);
	for (ll i=0; i<q; ++i)
	{
		input2(l, r);
		l--; r--;
		queries[l].pb(mp(r, i));
	}
	
	// abuse monotonicity
	for (ll i=n-1; i>=0; --i)
	{
		while (dq.size() && arr[dq.back()]<arr[i]) dq.pop_back();
		if (dq.size()) nxt[i].pb(dq.back());
		dq.pb(i);
	}
	dq.clear();
	for (ll i=0; i<n; ++i)
	{
		while (dq.size() && arr[dq.back()]<arr[i]) dq.pop_back();
		if (dq.size()) nxt[dq.back()].pb(i);
		dq.pb(i);
	}
	// SGTREE
	
	root= new node(0, n-1);
	for (ll i=n-1; i>=0; --i)
	{
		//show(i);
		for (ll u: nxt[i])
		{
			//show2(u, u*2-i);
			ll dist = u-i;
		    if (u+dist<=n-1) root->update(u+dist, n-1, arr[i] + arr[u]);
		}
		for (pi q: queries[i])  {ans[q.s] = root ->query(i, q.f);}
	}
	for (ll i=0; i<q; ++i) print(ans[i], '\n');
	
	
	return 0;
}

Compilation message

jumps.cpp: In function 'int main()':
jumps.cpp:11:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | #define input(x) scanf("%lld", &x);
      |                  ~~~~~^~~~~~~~~~~~
jumps.cpp:94:2: note: in expansion of macro 'input'
   94 |  input(n);
      |  ^~~~~
jumps.cpp:11:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | #define input(x) scanf("%lld", &x);
      |                  ~~~~~^~~~~~~~~~~~
jumps.cpp:96:25: note: in expansion of macro 'input'
   96 |  for (ll i=0; i<n; ++i) input(arr[i]);
      |                         ^~~~~
jumps.cpp:11:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | #define input(x) scanf("%lld", &x);
      |                  ~~~~~^~~~~~~~~~~~
jumps.cpp:97:2: note: in expansion of macro 'input'
   97 |  input(q);
      |  ^~~~~
jumps.cpp:12:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 | #define input2(x, y) scanf("%lld%lld", &x, &y);
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~
jumps.cpp:100:3: note: in expansion of macro 'input2'
  100 |   input2(l, r);
      |   ^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 27228 KB Output is correct
2 Correct 6 ms 27228 KB Output is correct
3 Correct 6 ms 27228 KB Output is correct
4 Correct 7 ms 27260 KB Output is correct
5 Correct 6 ms 27228 KB Output is correct
6 Correct 6 ms 27228 KB Output is correct
7 Correct 6 ms 27228 KB Output is correct
8 Correct 6 ms 27228 KB Output is correct
9 Correct 7 ms 27228 KB Output is correct
10 Correct 6 ms 27228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 27228 KB Output is correct
2 Correct 6 ms 27228 KB Output is correct
3 Correct 6 ms 27228 KB Output is correct
4 Correct 7 ms 27260 KB Output is correct
5 Correct 6 ms 27228 KB Output is correct
6 Correct 6 ms 27228 KB Output is correct
7 Correct 6 ms 27228 KB Output is correct
8 Correct 6 ms 27228 KB Output is correct
9 Correct 7 ms 27228 KB Output is correct
10 Correct 6 ms 27228 KB Output is correct
11 Correct 210 ms 52284 KB Output is correct
12 Correct 220 ms 52328 KB Output is correct
13 Correct 213 ms 52308 KB Output is correct
14 Correct 206 ms 52304 KB Output is correct
15 Correct 207 ms 52304 KB Output is correct
16 Correct 207 ms 51792 KB Output is correct
17 Correct 206 ms 51792 KB Output is correct
18 Correct 204 ms 51404 KB Output is correct
19 Correct 199 ms 52308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 160 ms 64788 KB Output is correct
2 Correct 99 ms 62696 KB Output is correct
3 Correct 99 ms 64004 KB Output is correct
4 Correct 162 ms 64756 KB Output is correct
5 Correct 158 ms 64888 KB Output is correct
6 Correct 159 ms 64396 KB Output is correct
7 Correct 155 ms 64164 KB Output is correct
8 Correct 157 ms 64168 KB Output is correct
9 Correct 157 ms 64596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 27228 KB Output is correct
2 Correct 6 ms 27228 KB Output is correct
3 Correct 6 ms 27228 KB Output is correct
4 Correct 7 ms 27260 KB Output is correct
5 Correct 6 ms 27228 KB Output is correct
6 Correct 6 ms 27228 KB Output is correct
7 Correct 6 ms 27228 KB Output is correct
8 Correct 6 ms 27228 KB Output is correct
9 Correct 7 ms 27228 KB Output is correct
10 Correct 6 ms 27228 KB Output is correct
11 Correct 210 ms 52284 KB Output is correct
12 Correct 220 ms 52328 KB Output is correct
13 Correct 213 ms 52308 KB Output is correct
14 Correct 206 ms 52304 KB Output is correct
15 Correct 207 ms 52304 KB Output is correct
16 Correct 207 ms 51792 KB Output is correct
17 Correct 206 ms 51792 KB Output is correct
18 Correct 204 ms 51404 KB Output is correct
19 Correct 199 ms 52308 KB Output is correct
20 Correct 160 ms 64788 KB Output is correct
21 Correct 99 ms 62696 KB Output is correct
22 Correct 99 ms 64004 KB Output is correct
23 Correct 162 ms 64756 KB Output is correct
24 Correct 158 ms 64888 KB Output is correct
25 Correct 159 ms 64396 KB Output is correct
26 Correct 155 ms 64164 KB Output is correct
27 Correct 157 ms 64168 KB Output is correct
28 Correct 157 ms 64596 KB Output is correct
29 Correct 979 ms 145388 KB Output is correct
30 Correct 793 ms 139348 KB Output is correct
31 Correct 810 ms 143100 KB Output is correct
32 Correct 952 ms 145372 KB Output is correct
33 Correct 946 ms 145312 KB Output is correct
34 Correct 955 ms 143136 KB Output is correct
35 Correct 963 ms 142900 KB Output is correct
36 Correct 974 ms 142660 KB Output is correct
37 Correct 959 ms 144264 KB Output is correct
38 Correct 639 ms 148052 KB Output is correct
39 Correct 647 ms 147940 KB Output is correct
40 Correct 654 ms 144568 KB Output is correct
41 Correct 643 ms 144212 KB Output is correct
42 Correct 656 ms 144212 KB Output is correct
43 Correct 663 ms 145872 KB Output is correct
44 Correct 685 ms 147688 KB Output is correct
45 Correct 722 ms 147796 KB Output is correct
46 Correct 687 ms 144856 KB Output is correct
47 Correct 720 ms 144092 KB Output is correct
48 Correct 707 ms 144080 KB Output is correct
49 Correct 870 ms 146156 KB Output is correct
50 Correct 813 ms 146904 KB Output is correct
51 Correct 845 ms 146884 KB Output is correct
52 Correct 785 ms 144656 KB Output is correct
53 Correct 779 ms 144468 KB Output is correct
54 Correct 778 ms 144168 KB Output is correct
55 Correct 796 ms 146000 KB Output is correct