답안 #151757

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
151757 2019-09-04T14:57:14 Z arnold518 조개 줍기 (KOI17_shell) C++14
100 / 100
618 ms 62332 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1500;

int N;
ll A[MAXN+10][MAXN+10], B[MAXN+10][MAXN+10], ans;

struct BIT
{
    ll tree[MAXN+10];
    void update(int i, ll val) { for(; i<=N; i+=(i&-i)) tree[i]+=val; }
    ll query(int i) { ll ret=0; for(; i>0; i-=(i&-i)) ret+=tree[i]; return ret; }
};
BIT bit[MAXN+10];

ll query(int y, int x, int val)
{
    int i, j;

    int s=x, e=x;

    //printf("=========\n");
    //for(i=1; i<=N; i++) { for(j=1; j<=N; j++) printf("%d ", bit[i].query(j)); printf("\n"); }
    //printf("=========\n");

    bit[y].update(x, val);
    for(i=x+1; i<=N; i++)
    {
        ll t=max(bit[y].query(i-1), bit[y-1].query(i))+A[y][i];
        if(t==bit[y].query(i)-val) break;
    }
    e=i;
    bit[y].update(e, -val);
    ans+=val*(e-s);
    //printf("!%d %d\n", s, e);

    for(i=y+1; i<=N; i++)
    {
        while(s<e)
        {
            ll t=max(bit[i].query(s-1), bit[i-1].query(s))+A[i][s];
            if(t!=bit[i].query(s)) break;
            s++;
        }
        bit[i].update(s, val);
        while(e<=N)
        {
            ll t=max(bit[i].query(e-1), bit[i-1].query(e))+A[i][e];
            if(t==bit[i].query(e)-val) break;
            e++;
        }
        bit[i].update(e, -val);
        ans+=val*(e-s);
        //printf("!%d %d\n", s, e);
        if(s==e) break;
    }

    A[y][x]+=val;
    return ans;
}

int main()
{
    int i, j;

    scanf("%d", &N);
    for(i=1; i<=N; i++) for(j=1; j<=N; j++) scanf("%lld", &A[i][j]);
    for(i=1; i<=N; i++) for(j=1; j<=N; j++) B[i][j]=A[i][j]+max(B[i-1][j], B[i][j-1]);
    for(i=1; i<=N; i++) for(j=1; j<=N; j++) ans+=B[i][j];
    printf("%lld\n", ans);

    for(i=1; i<=N; i++) for(j=1; j<=N; j++) bit[i].update(j, B[i][j]-B[i][j-1]);
    for(i=1; i<=N; i++)
    {
        char t;
        int y, x;
        scanf("\n%c%d%d", &t, &y, &x);
        if(t=='U') printf("%lld\n", query(y, x, 1));
        else printf("%lld\n", query(y, x, -1));
    }
}

Compilation message

shell.cpp: In function 'll query(int, int, int)':
shell.cpp:23:12: warning: unused variable 'j' [-Wunused-variable]
     int i, j;
            ^
shell.cpp: In function 'int main()':
shell.cpp:71:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &N);
     ~~~~~^~~~~~~~~~
shell.cpp:72:50: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(i=1; i<=N; i++) for(j=1; j<=N; j++) scanf("%lld", &A[i][j]);
                                             ~~~~~^~~~~~~~~~~~~~~~~~
shell.cpp:82:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("\n%c%d%d", &t, &y, &x);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1784 KB Output is correct
2 Correct 5 ms 1784 KB Output is correct
3 Correct 5 ms 1784 KB Output is correct
4 Correct 5 ms 1784 KB Output is correct
5 Correct 5 ms 1784 KB Output is correct
6 Correct 5 ms 1784 KB Output is correct
7 Correct 5 ms 1880 KB Output is correct
8 Correct 5 ms 1784 KB Output is correct
9 Correct 5 ms 1784 KB Output is correct
10 Correct 4 ms 1784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 268 ms 55364 KB Output is correct
2 Correct 269 ms 57980 KB Output is correct
3 Correct 275 ms 57908 KB Output is correct
4 Correct 266 ms 57928 KB Output is correct
5 Correct 264 ms 58096 KB Output is correct
6 Correct 268 ms 57976 KB Output is correct
7 Correct 275 ms 57972 KB Output is correct
8 Correct 263 ms 57976 KB Output is correct
9 Correct 265 ms 58116 KB Output is correct
10 Correct 262 ms 57976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1784 KB Output is correct
2 Correct 5 ms 1784 KB Output is correct
3 Correct 5 ms 1784 KB Output is correct
4 Correct 5 ms 1784 KB Output is correct
5 Correct 5 ms 1784 KB Output is correct
6 Correct 5 ms 1784 KB Output is correct
7 Correct 5 ms 1880 KB Output is correct
8 Correct 5 ms 1784 KB Output is correct
9 Correct 5 ms 1784 KB Output is correct
10 Correct 268 ms 55364 KB Output is correct
11 Correct 269 ms 57980 KB Output is correct
12 Correct 275 ms 57908 KB Output is correct
13 Correct 266 ms 57928 KB Output is correct
14 Correct 264 ms 58096 KB Output is correct
15 Correct 268 ms 57976 KB Output is correct
16 Correct 275 ms 57972 KB Output is correct
17 Correct 263 ms 57976 KB Output is correct
18 Correct 265 ms 58116 KB Output is correct
19 Correct 262 ms 57976 KB Output is correct
20 Correct 341 ms 62156 KB Output is correct
21 Correct 341 ms 62092 KB Output is correct
22 Correct 343 ms 62232 KB Output is correct
23 Correct 614 ms 62284 KB Output is correct
24 Correct 589 ms 62192 KB Output is correct
25 Correct 593 ms 62332 KB Output is correct
26 Correct 268 ms 57976 KB Output is correct
27 Correct 269 ms 57948 KB Output is correct
28 Correct 344 ms 62140 KB Output is correct
29 Correct 341 ms 62288 KB Output is correct
30 Correct 601 ms 62200 KB Output is correct
31 Correct 618 ms 62132 KB Output is correct
32 Correct 269 ms 58072 KB Output is correct
33 Correct 267 ms 57992 KB Output is correct
34 Correct 4 ms 1784 KB Output is correct