Submission #746707

# Submission time Handle Problem Language Result Execution time Memory
746707 2023-05-23T01:18:09 Z arthur_nascimento Fish 2 (JOI22_fish2) C++14
25 / 100
4000 ms 13784 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <map>
#include <set>
#include <ctime>
using namespace std;
 
#define maxn 200200
#define pb push_back
#define debug 
#define pii pair<int,int>
#define ll long long
#define mod 998244353
#define inf (1<<30)
 
int v_ord[maxn];
int v_rev[maxn];
 
struct query {
	int l, r, id;
	query(int l=0,int r=0,int id=0): l(l), r(r), id(id) {}
};
 
int ans[maxn];
 
int stck[maxn];
int ans_acc[maxn];
int sz;
 
vector<query> Q[maxn];
vector<int> event[maxn];
 
ll acc[maxn];
 
ll sum(int a,int b){
	ll r = acc[b];
	if(a) r -= acc[a-1];
	return r;
}
 
int greater_L[maxn];
int greater_R[maxn];
 
int value[maxn];
 
void erase(set<int> &S,int a){
	if(S.find(a) != S.end()) S.erase(a);
}
 
void solve(int *fish, int n, int eps , vector<query> vq){
 
	debug("hey\n");
 
	stck[0] = 0;
	ans_acc[0] = 0;
	sz = 1;
 
	for(int i=0;i<n;i++){
 
		acc[i] = fish[i];
		if(i) acc[i] += acc[i-1];
 
		Q[i].clear();
		event[i].clear();
 
		value[i] = 1;
 
	}
 
	for(auto q : vq)
		Q[q.r].pb(q);
 
	set<int> S, happy, unhappy;
	unhappy.insert(n+1);
	happy.insert(-1);
 
	for(int i=vq[0].l;i<n-1;i++){
 
		while(fish[i] + eps > fish[stck[sz-1]]){
 
			int rem = stck[sz-1];
			greater_R[rem] = i;
 
			int jmp = i;
			if(fish[i] + eps > fish[stck[sz-2]])
				jmp = stck[sz-2];
 
			ll eats = sum(greater_L[rem] + 1, greater_R[rem] - 1), survives = 0;
			if(eats >= fish[jmp])
				value[jmp] += value[rem], survives = 1;
 
			//erase(S, stck[sz-1]);
			//erase(happy, stck[sz-1]);
			//erase(unhappy, stck[sz-1]);
			debug("pop %d, eats %d, surv %d jmp %d\n",stck[sz-1],eats,survives,jmp);
			sz--;
 
		}
 
		greater_L[i] = stck[sz-1];
		ans_acc[i] = value[i] + ans_acc[stck[sz-1]];
 
		stck[sz++] = i;
 
		int lo = i, hi = n;
		/*while(lo < hi){
			int mid = (lo+hi)/2;
			if(sum(greater_L[i]+1,mid) >= fish[greater_L[i]])
				hi = mid;
			else
				lo = mid+1;
		}
 
		event[lo].pb(i);*/
		debug("i %d gr_L %d walk_R %d\n\n",i,greater_L[i],lo); 
 
		//S.insert(i);
		//unhappy.insert(i);
 
		for(int a : event[i]){
			if(S.find(a) != S.end()){
				//unhappy.erase(a);
				//happy.insert(a);
			}
		}
 
		for(auto q : Q[i]){
			int L = q.l;
			int top = 0;
			while(stck[top] < L) top++;
			int last_good = top;
			while(last_good+1 < sz){
				int cur = stck[last_good+1];
				if(sum(greater_L[cur]+1, i) >= fish[greater_L[cur]]) last_good++;
				else break;
			}	
			
			ans[q.id] += ans_acc[stck[last_good]] - ans_acc[stck[top]];
		}

		if(Q[i].size()) break;
	
	}
 
}
 
int 
main(){
 
	int n,q;
	scanf("%d",&n);
 
	for(int i=1;i<=n;i++){
		scanf("%d",v_ord+i);
		v_rev[n-i+1] = v_ord[i];
	}
 
	v_ord[0] = v_ord[n+1] = v_rev[0] = v_rev[n+1] = inf;
 
	scanf("%d",&q);
 
	vector<query> vq;
 
	for(int i=0;i<q;i++){
 
		int l,r;
        int ty;
		scanf("%d%d%d",&ty,&l,&r);
 
        if(ty == 1){
            v_ord[l] = r;
            v_rev[n-l+1] = r;
        }
        else {
            vq.clear();
            ans[0] = 0;
            vq.pb(query(l,r,0));
            solve(v_ord, n+2, 0, vq);
 
            for(auto &e : vq){
                e.l = n - e.l + 1;
                e.r = n - e.r + 1;
                swap(e.l,e.r);
            }
        
            solve(v_rev, n+2, 1, vq);
        
            printf("%d\n",1+ans[0]);
 
        }
		//vq.pb(query(l,r,i));
 
	}
 
}

Compilation message

fish2.cpp: In function 'void solve(int*, int, int, std::vector<query>)':
fish2.cpp:54:8: warning: statement has no effect [-Wunused-value]
   54 |  debug("hey\n");
      |       ~^~~~~~~~
fish2.cpp:97:10: warning: left operand of comma operator has no effect [-Wunused-value]
   97 |    debug("pop %d, eats %d, surv %d jmp %d\n",stck[sz-1],eats,survives,jmp);
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fish2.cpp:97:55: warning: right operand of comma operator has no effect [-Wunused-value]
   97 |    debug("pop %d, eats %d, surv %d jmp %d\n",stck[sz-1],eats,survives,jmp);
      |                                              ~~~~~~~~~^
fish2.cpp:97:62: warning: right operand of comma operator has no effect [-Wunused-value]
   97 |    debug("pop %d, eats %d, surv %d jmp %d\n",stck[sz-1],eats,survives,jmp);
      |                                                              ^~~~~~~~
fish2.cpp:97:71: warning: right operand of comma operator has no effect [-Wunused-value]
   97 |    debug("pop %d, eats %d, surv %d jmp %d\n",stck[sz-1],eats,survives,jmp);
      |                                                                       ^~~
fish2.cpp:117:9: warning: left operand of comma operator has no effect [-Wunused-value]
  117 |   debug("i %d gr_L %d walk_R %d\n\n",i,greater_L[i],lo);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
fish2.cpp:117:51: warning: right operand of comma operator has no effect [-Wunused-value]
  117 |   debug("i %d gr_L %d walk_R %d\n\n",i,greater_L[i],lo);
      |                                                   ^
fish2.cpp:117:51: warning: right operand of comma operator has no effect [-Wunused-value]
  117 |   debug("i %d gr_L %d walk_R %d\n\n",i,greater_L[i],lo);
      |                                        ~~~~~~~~~~~^
fish2.cpp:107:15: warning: unused variable 'hi' [-Wunused-variable]
  107 |   int lo = i, hi = n;
      |               ^~
fish2.cpp: In function 'int main()':
fish2.cpp:153:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  153 |  scanf("%d",&n);
      |  ~~~~~^~~~~~~~~
fish2.cpp:156:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  156 |   scanf("%d",v_ord+i);
      |   ~~~~~^~~~~~~~~~~~~~
fish2.cpp:162:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  162 |  scanf("%d",&q);
      |  ~~~~~^~~~~~~~~
fish2.cpp:170:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  170 |   scanf("%d%d%d",&ty,&l,&r);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 8 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 7 ms 9684 KB Output is correct
9 Correct 9 ms 9660 KB Output is correct
10 Correct 7 ms 9684 KB Output is correct
11 Correct 11 ms 9684 KB Output is correct
12 Correct 7 ms 9684 KB Output is correct
13 Correct 8 ms 9684 KB Output is correct
14 Correct 6 ms 9708 KB Output is correct
15 Correct 7 ms 9684 KB Output is correct
16 Correct 11 ms 9684 KB Output is correct
17 Correct 6 ms 9768 KB Output is correct
18 Correct 9 ms 9684 KB Output is correct
19 Correct 7 ms 9684 KB Output is correct
20 Correct 10 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 27 ms 12820 KB Output is correct
3 Correct 19 ms 12772 KB Output is correct
4 Correct 21 ms 12856 KB Output is correct
5 Correct 18 ms 12888 KB Output is correct
6 Correct 21 ms 12760 KB Output is correct
7 Correct 20 ms 12932 KB Output is correct
8 Correct 22 ms 12780 KB Output is correct
9 Correct 19 ms 12772 KB Output is correct
10 Correct 21 ms 12848 KB Output is correct
11 Correct 23 ms 12892 KB Output is correct
12 Correct 22 ms 12848 KB Output is correct
13 Correct 19 ms 12884 KB Output is correct
14 Correct 19 ms 12908 KB Output is correct
15 Correct 21 ms 12792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 8 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 7 ms 9684 KB Output is correct
9 Correct 9 ms 9660 KB Output is correct
10 Correct 7 ms 9684 KB Output is correct
11 Correct 11 ms 9684 KB Output is correct
12 Correct 7 ms 9684 KB Output is correct
13 Correct 8 ms 9684 KB Output is correct
14 Correct 6 ms 9708 KB Output is correct
15 Correct 7 ms 9684 KB Output is correct
16 Correct 11 ms 9684 KB Output is correct
17 Correct 6 ms 9768 KB Output is correct
18 Correct 9 ms 9684 KB Output is correct
19 Correct 7 ms 9684 KB Output is correct
20 Correct 10 ms 9684 KB Output is correct
21 Correct 5 ms 9684 KB Output is correct
22 Correct 27 ms 12820 KB Output is correct
23 Correct 19 ms 12772 KB Output is correct
24 Correct 21 ms 12856 KB Output is correct
25 Correct 18 ms 12888 KB Output is correct
26 Correct 21 ms 12760 KB Output is correct
27 Correct 20 ms 12932 KB Output is correct
28 Correct 22 ms 12780 KB Output is correct
29 Correct 19 ms 12772 KB Output is correct
30 Correct 21 ms 12848 KB Output is correct
31 Correct 23 ms 12892 KB Output is correct
32 Correct 22 ms 12848 KB Output is correct
33 Correct 19 ms 12884 KB Output is correct
34 Correct 19 ms 12908 KB Output is correct
35 Correct 21 ms 12792 KB Output is correct
36 Correct 564 ms 13028 KB Output is correct
37 Correct 869 ms 13016 KB Output is correct
38 Correct 1163 ms 13140 KB Output is correct
39 Correct 230 ms 13000 KB Output is correct
40 Correct 1113 ms 12908 KB Output is correct
41 Correct 1193 ms 13020 KB Output is correct
42 Correct 1534 ms 12908 KB Output is correct
43 Correct 1187 ms 13464 KB Output is correct
44 Correct 1457 ms 13144 KB Output is correct
45 Correct 597 ms 13388 KB Output is correct
46 Correct 858 ms 13616 KB Output is correct
47 Correct 1229 ms 13208 KB Output is correct
48 Correct 479 ms 13128 KB Output is correct
49 Correct 1531 ms 13292 KB Output is correct
50 Correct 715 ms 13528 KB Output is correct
51 Correct 2417 ms 13660 KB Output is correct
52 Correct 2638 ms 13784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 27 ms 12820 KB Output is correct
3 Correct 19 ms 12772 KB Output is correct
4 Correct 21 ms 12856 KB Output is correct
5 Correct 18 ms 12888 KB Output is correct
6 Correct 21 ms 12760 KB Output is correct
7 Correct 20 ms 12932 KB Output is correct
8 Correct 22 ms 12780 KB Output is correct
9 Correct 19 ms 12772 KB Output is correct
10 Correct 21 ms 12848 KB Output is correct
11 Correct 23 ms 12892 KB Output is correct
12 Correct 22 ms 12848 KB Output is correct
13 Correct 19 ms 12884 KB Output is correct
14 Correct 19 ms 12908 KB Output is correct
15 Correct 21 ms 12792 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Execution timed out 4054 ms 13180 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 27 ms 12820 KB Output is correct
3 Correct 19 ms 12772 KB Output is correct
4 Correct 21 ms 12856 KB Output is correct
5 Correct 18 ms 12888 KB Output is correct
6 Correct 21 ms 12760 KB Output is correct
7 Correct 20 ms 12932 KB Output is correct
8 Correct 22 ms 12780 KB Output is correct
9 Correct 19 ms 12772 KB Output is correct
10 Correct 21 ms 12848 KB Output is correct
11 Correct 23 ms 12892 KB Output is correct
12 Correct 22 ms 12848 KB Output is correct
13 Correct 19 ms 12884 KB Output is correct
14 Correct 19 ms 12908 KB Output is correct
15 Correct 21 ms 12792 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Execution timed out 4030 ms 12872 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 8 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 7 ms 9684 KB Output is correct
9 Correct 9 ms 9660 KB Output is correct
10 Correct 7 ms 9684 KB Output is correct
11 Correct 11 ms 9684 KB Output is correct
12 Correct 7 ms 9684 KB Output is correct
13 Correct 8 ms 9684 KB Output is correct
14 Correct 6 ms 9708 KB Output is correct
15 Correct 7 ms 9684 KB Output is correct
16 Correct 11 ms 9684 KB Output is correct
17 Correct 6 ms 9768 KB Output is correct
18 Correct 9 ms 9684 KB Output is correct
19 Correct 7 ms 9684 KB Output is correct
20 Correct 10 ms 9684 KB Output is correct
21 Correct 5 ms 9684 KB Output is correct
22 Correct 27 ms 12820 KB Output is correct
23 Correct 19 ms 12772 KB Output is correct
24 Correct 21 ms 12856 KB Output is correct
25 Correct 18 ms 12888 KB Output is correct
26 Correct 21 ms 12760 KB Output is correct
27 Correct 20 ms 12932 KB Output is correct
28 Correct 22 ms 12780 KB Output is correct
29 Correct 19 ms 12772 KB Output is correct
30 Correct 21 ms 12848 KB Output is correct
31 Correct 23 ms 12892 KB Output is correct
32 Correct 22 ms 12848 KB Output is correct
33 Correct 19 ms 12884 KB Output is correct
34 Correct 19 ms 12908 KB Output is correct
35 Correct 21 ms 12792 KB Output is correct
36 Correct 564 ms 13028 KB Output is correct
37 Correct 869 ms 13016 KB Output is correct
38 Correct 1163 ms 13140 KB Output is correct
39 Correct 230 ms 13000 KB Output is correct
40 Correct 1113 ms 12908 KB Output is correct
41 Correct 1193 ms 13020 KB Output is correct
42 Correct 1534 ms 12908 KB Output is correct
43 Correct 1187 ms 13464 KB Output is correct
44 Correct 1457 ms 13144 KB Output is correct
45 Correct 597 ms 13388 KB Output is correct
46 Correct 858 ms 13616 KB Output is correct
47 Correct 1229 ms 13208 KB Output is correct
48 Correct 479 ms 13128 KB Output is correct
49 Correct 1531 ms 13292 KB Output is correct
50 Correct 715 ms 13528 KB Output is correct
51 Correct 2417 ms 13660 KB Output is correct
52 Correct 2638 ms 13784 KB Output is correct
53 Correct 5 ms 9684 KB Output is correct
54 Execution timed out 4054 ms 13180 KB Time limit exceeded
55 Halted 0 ms 0 KB -