답안 #596870

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
596870 2022-07-15T08:17:56 Z 조영욱(#8449) Fish 2 (JOI22_fish2) C++17
25 / 100
4000 ms 3320 KB
#include <bits/stdc++.h>
using namespace std;

int le[100001];
int ri[100001];
int arr[100001];
long long psum[100001];
int n;
int q;
typedef pair<int,int> P;
vector<P> vec;
bool win[100001];

int main(void) {
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        scanf("%d",&arr[i]);
    }
    stack<int> s;
    for(int i=1;i<=n;i++) {
        psum[i]=psum[i-1]+arr[i];
        while (!s.empty()&&arr[s.top()]<=arr[i]) {
            s.pop();
        }
        if (s.empty()) {
            le[i]=-1;
        }
        else {
            le[i]=s.top();
        }
        s.push(i);
    }
    while (!s.empty()) {
        s.pop();
    }
    for(int i=n;i>0;i--) {
        vec.push_back(P(arr[i],i));
        while (!s.empty()&&arr[s.top()]<arr[i]) {
            s.pop();
        }
        if (s.empty()) {
            ri[i]=-1;
        }
        else {
            ri[i]=s.top();
        }
        s.push(i);
    }
    sort(vec.begin(),vec.end());
    reverse(vec.begin(),vec.end());
    scanf("%d",&q);
    for(int ind=0;ind<q;ind++) {
        int t,l,r;
        scanf("%d %d %d",&t,&l,&r);
        if (t==1) {
            arr[l]=r;
            stack<int> s;
            for(int i=1;i<=n;i++) {
                psum[i]=psum[i-1]+arr[i];
                while (!s.empty()&&arr[s.top()]<=arr[i]) {
                    s.pop();
                }
                if (s.empty()) {
                    le[i]=-1;
                }
                else {
                    le[i]=s.top();
                }
                s.push(i);
            }
            while (!s.empty()) {
                s.pop();
            }
            for(int i=n;i>0;i--) {
                while (!s.empty()&&arr[s.top()]<arr[i]) {
                    s.pop();
                }
                if (s.empty()) {
                    ri[i]=-1;
                }
                else {
                    ri[i]=s.top();
                }
                s.push(i);
            }
            for(int i=0;i<vec.size();i++) {
                if (vec[i].second==l) {
//printf("%d %d\n",vec[i].first,vec[i].second);
                    vec.erase(vec.begin()+i);
                    break;
                }
            }
            for(int i=0;i<=vec.size();i++) {
                if (i==vec.size()) {
                    vec.insert(vec.begin()+i,P(arr[l],l));
                    break;
                }
                if (P(arr[l],l)>vec[i]) {
                    vec.insert(vec.begin()+i,P(arr[l],l));
                    break;
                }
            }
        }
        else {
            int ret=0;
            for(int i=0;i<vec.size();i++) {
                int now=vec[i].second;
win[now]=false;
                if (now<l||now>r) {
                    continue;
                }
                if (le[now]!=-1&&le[now]>=l&&!win[le[now]]) {
                    continue;
                }
                if (ri[now]!=-1&&ri[now]<=r&&!win[ri[now]]) {
                    continue;
                }
                int lef=((le[now]==-1||le[now]<l)?l:le[now]+1);
                int rig=((ri[now]==-1||ri[now]>r)?r:ri[now]-1);
//if (ind==1) printf("%d %d\n",lef,rig);
                long long val=psum[rig]-psum[lef-1];
                if (lef==l&&rig==r) {
                    win[now]=true;
                    ret++;
                }
                else if (lef!=l&&arr[le[now]]<=val) {
                    win[now]=true;
                    ret++;
                }
                else if (rig!=r&&arr[ri[now]]<=val) {
                    win[now]=true;
                    ret++;
                }
            }
            printf("%d\n",ret);
        }
    }
}

Compilation message

fish2.cpp: In function 'int main()':
fish2.cpp:86:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |             for(int i=0;i<vec.size();i++) {
      |                         ~^~~~~~~~~~~
fish2.cpp:93:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |             for(int i=0;i<=vec.size();i++) {
      |                         ~^~~~~~~~~~~~
fish2.cpp:94:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |                 if (i==vec.size()) {
      |                     ~^~~~~~~~~~~~
fish2.cpp:106:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |             for(int i=0;i<vec.size();i++) {
      |                         ~^~~~~~~~~~~
fish2.cpp:15:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
fish2.cpp:17:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |         scanf("%d",&arr[i]);
      |         ~~~~~^~~~~~~~~~~~~~
fish2.cpp:51:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |     scanf("%d",&q);
      |     ~~~~~^~~~~~~~~
fish2.cpp:54:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |         scanf("%d %d %d",&t,&l,&r);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 4 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 4 ms 340 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 3 ms 340 KB Output is correct
10 Correct 5 ms 336 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 7 ms 340 KB Output is correct
13 Correct 3 ms 340 KB Output is correct
14 Correct 4 ms 212 KB Output is correct
15 Correct 5 ms 340 KB Output is correct
16 Correct 2 ms 212 KB Output is correct
17 Correct 5 ms 212 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 4 ms 340 KB Output is correct
20 Correct 3 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 22 ms 3276 KB Output is correct
3 Correct 25 ms 3276 KB Output is correct
4 Correct 26 ms 3276 KB Output is correct
5 Correct 25 ms 3276 KB Output is correct
6 Correct 24 ms 3276 KB Output is correct
7 Correct 27 ms 3276 KB Output is correct
8 Correct 30 ms 3276 KB Output is correct
9 Correct 19 ms 3244 KB Output is correct
10 Correct 26 ms 3200 KB Output is correct
11 Correct 18 ms 3276 KB Output is correct
12 Correct 21 ms 3240 KB Output is correct
13 Correct 22 ms 3276 KB Output is correct
14 Correct 25 ms 3276 KB Output is correct
15 Correct 29 ms 3276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 4 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 4 ms 340 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 3 ms 340 KB Output is correct
10 Correct 5 ms 336 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 7 ms 340 KB Output is correct
13 Correct 3 ms 340 KB Output is correct
14 Correct 4 ms 212 KB Output is correct
15 Correct 5 ms 340 KB Output is correct
16 Correct 2 ms 212 KB Output is correct
17 Correct 5 ms 212 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 4 ms 340 KB Output is correct
20 Correct 3 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 22 ms 3276 KB Output is correct
23 Correct 25 ms 3276 KB Output is correct
24 Correct 26 ms 3276 KB Output is correct
25 Correct 25 ms 3276 KB Output is correct
26 Correct 24 ms 3276 KB Output is correct
27 Correct 27 ms 3276 KB Output is correct
28 Correct 30 ms 3276 KB Output is correct
29 Correct 19 ms 3244 KB Output is correct
30 Correct 26 ms 3200 KB Output is correct
31 Correct 18 ms 3276 KB Output is correct
32 Correct 21 ms 3240 KB Output is correct
33 Correct 22 ms 3276 KB Output is correct
34 Correct 25 ms 3276 KB Output is correct
35 Correct 29 ms 3276 KB Output is correct
36 Correct 843 ms 3276 KB Output is correct
37 Correct 761 ms 3276 KB Output is correct
38 Correct 404 ms 3276 KB Output is correct
39 Correct 1084 ms 3200 KB Output is correct
40 Correct 433 ms 3276 KB Output is correct
41 Correct 1541 ms 3276 KB Output is correct
42 Correct 1183 ms 3288 KB Output is correct
43 Correct 1022 ms 3204 KB Output is correct
44 Correct 538 ms 3276 KB Output is correct
45 Correct 887 ms 3276 KB Output is correct
46 Correct 768 ms 3320 KB Output is correct
47 Correct 364 ms 3276 KB Output is correct
48 Correct 2165 ms 3188 KB Output is correct
49 Correct 581 ms 3240 KB Output is correct
50 Correct 1368 ms 3276 KB Output is correct
51 Correct 1656 ms 3252 KB Output is correct
52 Correct 1200 ms 3260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 22 ms 3276 KB Output is correct
3 Correct 25 ms 3276 KB Output is correct
4 Correct 26 ms 3276 KB Output is correct
5 Correct 25 ms 3276 KB Output is correct
6 Correct 24 ms 3276 KB Output is correct
7 Correct 27 ms 3276 KB Output is correct
8 Correct 30 ms 3276 KB Output is correct
9 Correct 19 ms 3244 KB Output is correct
10 Correct 26 ms 3200 KB Output is correct
11 Correct 18 ms 3276 KB Output is correct
12 Correct 21 ms 3240 KB Output is correct
13 Correct 22 ms 3276 KB Output is correct
14 Correct 25 ms 3276 KB Output is correct
15 Correct 29 ms 3276 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Execution timed out 4086 ms 3288 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 22 ms 3276 KB Output is correct
3 Correct 25 ms 3276 KB Output is correct
4 Correct 26 ms 3276 KB Output is correct
5 Correct 25 ms 3276 KB Output is correct
6 Correct 24 ms 3276 KB Output is correct
7 Correct 27 ms 3276 KB Output is correct
8 Correct 30 ms 3276 KB Output is correct
9 Correct 19 ms 3244 KB Output is correct
10 Correct 26 ms 3200 KB Output is correct
11 Correct 18 ms 3276 KB Output is correct
12 Correct 21 ms 3240 KB Output is correct
13 Correct 22 ms 3276 KB Output is correct
14 Correct 25 ms 3276 KB Output is correct
15 Correct 29 ms 3276 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Execution timed out 4094 ms 3232 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 4 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 4 ms 340 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 3 ms 340 KB Output is correct
10 Correct 5 ms 336 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 7 ms 340 KB Output is correct
13 Correct 3 ms 340 KB Output is correct
14 Correct 4 ms 212 KB Output is correct
15 Correct 5 ms 340 KB Output is correct
16 Correct 2 ms 212 KB Output is correct
17 Correct 5 ms 212 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 4 ms 340 KB Output is correct
20 Correct 3 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 22 ms 3276 KB Output is correct
23 Correct 25 ms 3276 KB Output is correct
24 Correct 26 ms 3276 KB Output is correct
25 Correct 25 ms 3276 KB Output is correct
26 Correct 24 ms 3276 KB Output is correct
27 Correct 27 ms 3276 KB Output is correct
28 Correct 30 ms 3276 KB Output is correct
29 Correct 19 ms 3244 KB Output is correct
30 Correct 26 ms 3200 KB Output is correct
31 Correct 18 ms 3276 KB Output is correct
32 Correct 21 ms 3240 KB Output is correct
33 Correct 22 ms 3276 KB Output is correct
34 Correct 25 ms 3276 KB Output is correct
35 Correct 29 ms 3276 KB Output is correct
36 Correct 843 ms 3276 KB Output is correct
37 Correct 761 ms 3276 KB Output is correct
38 Correct 404 ms 3276 KB Output is correct
39 Correct 1084 ms 3200 KB Output is correct
40 Correct 433 ms 3276 KB Output is correct
41 Correct 1541 ms 3276 KB Output is correct
42 Correct 1183 ms 3288 KB Output is correct
43 Correct 1022 ms 3204 KB Output is correct
44 Correct 538 ms 3276 KB Output is correct
45 Correct 887 ms 3276 KB Output is correct
46 Correct 768 ms 3320 KB Output is correct
47 Correct 364 ms 3276 KB Output is correct
48 Correct 2165 ms 3188 KB Output is correct
49 Correct 581 ms 3240 KB Output is correct
50 Correct 1368 ms 3276 KB Output is correct
51 Correct 1656 ms 3252 KB Output is correct
52 Correct 1200 ms 3260 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Execution timed out 4086 ms 3288 KB Time limit exceeded
55 Halted 0 ms 0 KB -