답안 #403447

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
403447 2021-05-13T07:47:15 Z tqbfjotld 조개 줍기 (KOI17_shell) C++14
0 / 100
733 ms 157684 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

set<int> ups[1505];
int fenwick[1505][1505];

void upd(int pos, int val, int num){
    while (pos<1505){
        fenwick[pos][num]+=val;
        pos+=pos&-pos;
    }
}
int qu(int pos, int num){
    int ans = 0;
    while (pos>0){
        ans += fenwick[pos][num];
        pos-=pos&-pos;
    }
    return ans;
}

int initarr[1505][1505];
int v2[1505][1505];
int n;
int curans = 0;
vector<pair<int,int> > to_up;
vector<pair<int,int> > to_left;

void incr(int x, int y){
    int curval = qu(x,y);
    if (x!=1 && y!=n){
        int t1 = qu(x-1,y+1);
        if (t1==curval+1){
            to_up.push_back({x,y+1});
        }
    }
    if (x!=n && y!=1){
        int t2 = qu(x+1,y-1);
        if (t2==curval){
            to_left.push_back({x+1,y});
            incr(x+1,y);
        }
    }
}

void decr(int x, int y){
    int curval = qu(x,y);
    if (x!=1 && y!=n){
        int t1 = qu(x-1,y+1);
        if (t1==curval){
            to_left.push_back({x,y+1});
        }
    }
    if (x!=n && y!=1){
        int t2 = qu(x+1,y-1);
        if (t2==curval-1){
            to_up.push_back({x+1,y});
            decr(x+1,y);
        }
    }
}

void set_up(int x, int y){
    ups[y].insert(x);
}
void set_left(int x, int y){
    ups[y].erase(ups[y].lower_bound(x));
}

void incr2(int x, int y){
    auto it = ups[y].lower_bound(x+1);
    int bound;
    if (it==ups[y].end()){
        bound = n;
    }
    else bound = (*it)-1;
    curans += bound-x+1;
    upd(x,1,y);
    upd(bound+1,-1,y);
    if (y!=n){
        auto it2 = ups[y+1].lower_bound(x);
        if (it2==ups[y+1].end() || (*it2)>bound) return;
        incr2((*it2),y+1);
    }
}
void decr2(int x, int y){
    auto it = ups[y].lower_bound(x+1);
    int bound;
    if (it==ups[y].end()){
        bound = n;
    }
    else bound = (*it)-1;
    curans -= bound-x+1;
    upd(x,-1,y);
    upd(bound+1,1,y);
    if (y!=n){
        auto it2 = ups[y+1].lower_bound(x);
        if (it2==ups[y+1].end() || (*it2)>bound) return;
        decr2((*it2),y+1);
    }
}

main(){
    scanf("%lld",&n);
    for (int x = 1; x<=n; x++){
        for (int y = 1; y<=n; y++){
            scanf("%lld",&initarr[x][y]);
        }
    }
    for (int x = 1; x<=n; x++){
        for (int y = 1; y<=n; y++){
            if (x==1&&y==1){
                v2[x][y] = initarr[x][y];
            }
            else if (x==1){
                v2[x][y] = v2[x][y-1]+initarr[x][y];
                ups[y].insert(x);
            }
            else if (y==1){
                v2[x][y] = v2[x-1][y]+initarr[x][y];
            }
            else if (v2[x-1][y]>v2[x][y-1]){
                v2[x][y] = v2[x-1][y]+initarr[x][y];
            }
            else{
                v2[x][y] = v2[x][y-1]+initarr[x][y];
                ups[y].insert(x);
            }
            curans += v2[x][y];
            upd(x,v2[x][y],y);
            upd(x+1,-v2[x][y],y);
        }
    }
    printf("%lld\n",curans);
    for (int q = 0; q<n; q++){
        char c;
        int a,b;
        scanf(" %c%lld%lld",&c,&a,&b);
        if (c=='U'){
            incr(a,b);
            for (auto x : to_left){
                set_left(x.first,x.second);
            }
            incr2(a,b);
            for (auto x : to_up){
                set_up(x.first,x.second);
            }
        }
        else{
            decr(a,b);
            for (auto x : to_left){
                set_left(x.first,x.second);
            }
            decr2(a,b);
            for (auto x : to_up){
                set_up(x.first,x.second);
            }
        }
        to_left.clear();
        to_up.clear();
        printf("%lld\n",curans);
    }
}

Compilation message

shell.cpp:104:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  104 | main(){
      | ^~~~
shell.cpp: In function 'int main()':
shell.cpp:105:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |     scanf("%lld",&n);
      |     ~~~~~^~~~~~~~~~~
shell.cpp:108:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |             scanf("%lld",&initarr[x][y]);
      |             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
shell.cpp:139:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |         scanf(" %c%lld%lld",&c,&a,&b);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 2124 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 733 ms 157684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 2124 KB Output isn't correct
2 Halted 0 ms 0 KB -