답안 #596862

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
596862 2022-07-15T08:13:50 Z 조영욱(#8449) Fish 2 (JOI22_fish2) C++17
8 / 100
4000 ms 1048576 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);
            }
            for(int i=0;i<vec.size();i++) {
                if (vec[i].second==l) {
                    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));
                }
                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:88: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]
   88 |             for(int i=0;i<vec.size();i++) {
      |                         ~^~~~~~~~~~~
fish2.cpp:94: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]
   94 |             for(int i=0;i<=vec.size();i++) {
      |                         ~^~~~~~~~~~~~
fish2.cpp:95: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]
   95 |                 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 Runtime error 1384 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 236 KB Output is correct
2 Correct 22 ms 3276 KB Output is correct
3 Correct 19 ms 3276 KB Output is correct
4 Correct 21 ms 3188 KB Output is correct
5 Correct 28 ms 3276 KB Output is correct
6 Correct 26 ms 3204 KB Output is correct
7 Correct 24 ms 3176 KB Output is correct
8 Correct 23 ms 3276 KB Output is correct
9 Correct 18 ms 3276 KB Output is correct
10 Correct 22 ms 3276 KB Output is correct
11 Correct 20 ms 3216 KB Output is correct
12 Correct 21 ms 3276 KB Output is correct
13 Correct 21 ms 3276 KB Output is correct
14 Correct 23 ms 3200 KB Output is correct
15 Correct 24 ms 3276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1384 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 236 KB Output is correct
2 Correct 22 ms 3276 KB Output is correct
3 Correct 19 ms 3276 KB Output is correct
4 Correct 21 ms 3188 KB Output is correct
5 Correct 28 ms 3276 KB Output is correct
6 Correct 26 ms 3204 KB Output is correct
7 Correct 24 ms 3176 KB Output is correct
8 Correct 23 ms 3276 KB Output is correct
9 Correct 18 ms 3276 KB Output is correct
10 Correct 22 ms 3276 KB Output is correct
11 Correct 20 ms 3216 KB Output is correct
12 Correct 21 ms 3276 KB Output is correct
13 Correct 21 ms 3276 KB Output is correct
14 Correct 23 ms 3200 KB Output is correct
15 Correct 24 ms 3276 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Execution timed out 4051 ms 3416 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 236 KB Output is correct
2 Correct 22 ms 3276 KB Output is correct
3 Correct 19 ms 3276 KB Output is correct
4 Correct 21 ms 3188 KB Output is correct
5 Correct 28 ms 3276 KB Output is correct
6 Correct 26 ms 3204 KB Output is correct
7 Correct 24 ms 3176 KB Output is correct
8 Correct 23 ms 3276 KB Output is correct
9 Correct 18 ms 3276 KB Output is correct
10 Correct 22 ms 3276 KB Output is correct
11 Correct 20 ms 3216 KB Output is correct
12 Correct 21 ms 3276 KB Output is correct
13 Correct 21 ms 3276 KB Output is correct
14 Correct 23 ms 3200 KB Output is correct
15 Correct 24 ms 3276 KB Output is correct
16 Incorrect 0 ms 212 KB Output isn't correct
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1384 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -