답안 #596858

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
596858 2022-07-15T08:06:34 Z 조영욱(#8449) Fish 2 (JOI22_fish2) C++17
13 / 100
4000 ms 3296 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) {
            vec.clear();
            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--) {
                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());
        }
        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&&!win[le[now]]) continue;
if (rig!=r&&!win[ri[now]]) continue;
                if ((le[now]==-1||le[now]<l)&&(ri[now]==-1||ri[now]>r)) {
                    win[now]=true;
                    ret++;
                }
                else if (le[now]!=-1&&le[now]>=l&&arr[le[now]]<=val) {
                    win[now]=true;
                    ret++;
                }
                else if (ri[now]!=-1&&ri[now]<=r&&arr[ri[now]]<=val) {
                    win[now]=true;
                    ret++;
                }
            }
            printf("%d\n",ret);
        }
    }
}

Compilation message

fish2.cpp: In function 'int main()':
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: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 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 12 ms 332 KB Output is correct
6 Correct 5 ms 340 KB Output is correct
7 Correct 17 ms 212 KB Output is correct
8 Correct 8 ms 340 KB Output is correct
9 Correct 6 ms 340 KB Output is correct
10 Correct 13 ms 336 KB Output is correct
11 Correct 5 ms 340 KB Output is correct
12 Correct 16 ms 336 KB Output is correct
13 Correct 5 ms 340 KB Output is correct
14 Correct 9 ms 212 KB Output is correct
15 Correct 11 ms 332 KB Output is correct
16 Correct 4 ms 340 KB Output is correct
17 Correct 15 ms 336 KB Output is correct
18 Correct 3 ms 340 KB Output is correct
19 Correct 14 ms 316 KB Output is correct
20 Correct 9 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 24 ms 3276 KB Output is correct
3 Correct 26 ms 3184 KB Output is correct
4 Correct 27 ms 3276 KB Output is correct
5 Correct 24 ms 3212 KB Output is correct
6 Correct 23 ms 3264 KB Output is correct
7 Correct 18 ms 3276 KB Output is correct
8 Correct 23 ms 3232 KB Output is correct
9 Correct 19 ms 3276 KB Output is correct
10 Correct 22 ms 3296 KB Output is correct
11 Correct 19 ms 3272 KB Output is correct
12 Correct 20 ms 3276 KB Output is correct
13 Correct 21 ms 3192 KB Output is correct
14 Correct 25 ms 3280 KB Output is correct
15 Correct 33 ms 3164 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 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 12 ms 332 KB Output is correct
6 Correct 5 ms 340 KB Output is correct
7 Correct 17 ms 212 KB Output is correct
8 Correct 8 ms 340 KB Output is correct
9 Correct 6 ms 340 KB Output is correct
10 Correct 13 ms 336 KB Output is correct
11 Correct 5 ms 340 KB Output is correct
12 Correct 16 ms 336 KB Output is correct
13 Correct 5 ms 340 KB Output is correct
14 Correct 9 ms 212 KB Output is correct
15 Correct 11 ms 332 KB Output is correct
16 Correct 4 ms 340 KB Output is correct
17 Correct 15 ms 336 KB Output is correct
18 Correct 3 ms 340 KB Output is correct
19 Correct 14 ms 316 KB Output is correct
20 Correct 9 ms 340 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 24 ms 3276 KB Output is correct
23 Correct 26 ms 3184 KB Output is correct
24 Correct 27 ms 3276 KB Output is correct
25 Correct 24 ms 3212 KB Output is correct
26 Correct 23 ms 3264 KB Output is correct
27 Correct 18 ms 3276 KB Output is correct
28 Correct 23 ms 3232 KB Output is correct
29 Correct 19 ms 3276 KB Output is correct
30 Correct 22 ms 3296 KB Output is correct
31 Correct 19 ms 3272 KB Output is correct
32 Correct 20 ms 3276 KB Output is correct
33 Correct 21 ms 3192 KB Output is correct
34 Correct 25 ms 3280 KB Output is correct
35 Correct 33 ms 3164 KB Output is correct
36 Execution timed out 4043 ms 3216 KB Time limit exceeded
37 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 24 ms 3276 KB Output is correct
3 Correct 26 ms 3184 KB Output is correct
4 Correct 27 ms 3276 KB Output is correct
5 Correct 24 ms 3212 KB Output is correct
6 Correct 23 ms 3264 KB Output is correct
7 Correct 18 ms 3276 KB Output is correct
8 Correct 23 ms 3232 KB Output is correct
9 Correct 19 ms 3276 KB Output is correct
10 Correct 22 ms 3296 KB Output is correct
11 Correct 19 ms 3272 KB Output is correct
12 Correct 20 ms 3276 KB Output is correct
13 Correct 21 ms 3192 KB Output is correct
14 Correct 25 ms 3280 KB Output is correct
15 Correct 33 ms 3164 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Execution timed out 4080 ms 3292 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 24 ms 3276 KB Output is correct
3 Correct 26 ms 3184 KB Output is correct
4 Correct 27 ms 3276 KB Output is correct
5 Correct 24 ms 3212 KB Output is correct
6 Correct 23 ms 3264 KB Output is correct
7 Correct 18 ms 3276 KB Output is correct
8 Correct 23 ms 3232 KB Output is correct
9 Correct 19 ms 3276 KB Output is correct
10 Correct 22 ms 3296 KB Output is correct
11 Correct 19 ms 3272 KB Output is correct
12 Correct 20 ms 3276 KB Output is correct
13 Correct 21 ms 3192 KB Output is correct
14 Correct 25 ms 3280 KB Output is correct
15 Correct 33 ms 3164 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Execution timed out 4077 ms 3276 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 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 12 ms 332 KB Output is correct
6 Correct 5 ms 340 KB Output is correct
7 Correct 17 ms 212 KB Output is correct
8 Correct 8 ms 340 KB Output is correct
9 Correct 6 ms 340 KB Output is correct
10 Correct 13 ms 336 KB Output is correct
11 Correct 5 ms 340 KB Output is correct
12 Correct 16 ms 336 KB Output is correct
13 Correct 5 ms 340 KB Output is correct
14 Correct 9 ms 212 KB Output is correct
15 Correct 11 ms 332 KB Output is correct
16 Correct 4 ms 340 KB Output is correct
17 Correct 15 ms 336 KB Output is correct
18 Correct 3 ms 340 KB Output is correct
19 Correct 14 ms 316 KB Output is correct
20 Correct 9 ms 340 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 24 ms 3276 KB Output is correct
23 Correct 26 ms 3184 KB Output is correct
24 Correct 27 ms 3276 KB Output is correct
25 Correct 24 ms 3212 KB Output is correct
26 Correct 23 ms 3264 KB Output is correct
27 Correct 18 ms 3276 KB Output is correct
28 Correct 23 ms 3232 KB Output is correct
29 Correct 19 ms 3276 KB Output is correct
30 Correct 22 ms 3296 KB Output is correct
31 Correct 19 ms 3272 KB Output is correct
32 Correct 20 ms 3276 KB Output is correct
33 Correct 21 ms 3192 KB Output is correct
34 Correct 25 ms 3280 KB Output is correct
35 Correct 33 ms 3164 KB Output is correct
36 Execution timed out 4043 ms 3216 KB Time limit exceeded
37 Halted 0 ms 0 KB -