답안 #1040508

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1040508 2024-08-01T06:35:51 Z Requiem Sjeckanje (COCI21_sjeckanje) C++17
55 / 110
66 ms 79836 KB
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 1000000007
#define inf 1e18
#define fi first
#define se second
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define sz(a) ((int)(a).size())
#define endl '\n'
#define pi 3.14159265359
#define TASKNAME "sjeckanje"
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
using namespace std;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;

int n, q;
int a[300005];

namespace subtask2{
    bool check(){
        return n <= 3000 and q <= 3000;
    }

    /**
       Nhan xet cua ta la ta se dung cai difference array roi dp tren do
       Khi chuyen sang difference array thi ta co cac luu y:
       - Cac doan duoc chon phai co cung dau.
       - Cac doan duoc chon phai cach nhau 1 phan tu.
    **/

    int differenceArray[3003], dp[3030][2];

    //dp[i][j]: cho biet day con co tong lon nhat khi xet den vi tri i va j la dang duong hay am
    void update(int l, int r, int x){
         differenceArray[l] += x;
         differenceArray[r + 1] -= x;
    }

   int get(){
         memset(dp, -0x3f, sizeof(dp));
         dp[1][0] = dp[1][1] = 0;

         for(int i = 1; i <= n; i++){
             for(int j = 0; j < 2; j++){
                 if (dp[i][j] > -inf){
//                     cout << i << ' ' << j <<' ' << dp[i][j] << endl;
                     if (j == 0) {
                         if (differenceArray[i + 1] > 0) maximize(dp[i + 1][0], dp[i][0] + differenceArray[i + 1]);
                     }
                     else {
                         if (differenceArray[i + 1] < 0) maximize(dp[i + 1][1], dp[i][j] - differenceArray[i + 1]);
                     }
                     maximize(dp[i + 1][0], dp[i][j]);
                     maximize(dp[i + 1][1], dp[i][j]);

                 }

             }
         }
         return max(dp[n][0], dp[n][1]);

    }
    void solve(){
        for(int i = 1; i <= n; i++){
            cin >> a[i];
            differenceArray[i] = a[i] - a[i - 1];
        }
//        cout << get();

        for(int i = 1; i <= q; i++){
            int l, r, x;
            cin >> l >> r >> x;
            update(l, r, x);
            cout << get() << endl;
        }
    }
}

namespace subtask3{
    bool check(){
         return true;
    }

    /**
    Ta se dua bai toan len cay segment tree
    Voi 1 nut segment tree, ta se luu 2 dau.
    st[nn].dp[i][j]:
    **/
    const int MAXN = 3e5 + 9;

    int differenceArray[MAXN], a[MAXN];

    struct Node{
        int dp[2][2];
        Node(){
            memset(dp, 0, sizeof(dp));
        }
    } st[MAXN << 2];

    Node Merge(Node a, Node b){
         Node res;
         for(int startLeft = 0; startLeft < 2; startLeft++){
             for(int endingLeft = 0; endingLeft < 2; endingLeft++){
                for(int startRight = 0; startRight < 2; startRight++){
                    for(int endingRight = 0; endingRight < 2; endingRight++){
                        maximize(res.dp[startLeft][endingRight], a.dp[startLeft][endingLeft]);
                        maximize(res.dp[startLeft][endingRight], b.dp[startRight][endingRight]);
                        if (endingLeft == startRight) maximize(res.dp[startLeft][endingRight],
                                                               a.dp[startLeft][endingLeft] + b.dp[startRight][endingRight]);
                    }
                }
             }
         }
         return res;
    }

    void update(int nn, int l, int r, int pos, int val){
        if (l == r){
            if (differenceArray[l] >= 0) st[nn].dp[0][0] = differenceArray[l];
            if (differenceArray[l] < 0) st[nn].dp[1][1] = -differenceArray[l];
            return;
        }
        else{
            int mid = (l + r) >> 1;
            if (pos <= mid) update(nn << 1, l, mid, pos, val);
            else update(nn << 1 | 1, mid + 1, r, pos, val);
            st[nn] = Merge(st[nn << 1], st[nn << 1 | 1]);
        }
    }

    void solve(){
        cin >> n >> q;
        for(int i = 1; i <= n; i++){
            cin >> a[i];
            differenceArray[i] = a[i] - a[i - 1];
            if (i > 1) update(1, 2, n, i, differenceArray[i]);
        }

        for(int i = 1; i <= q; i++){
            int l, r, x;
            cin >> l >> r >> x;
            differenceArray[l] += x;
            differenceArray[r + 1] -= x;
            if (l > 1) update(1, 2, n, l, differenceArray[l]);
            update(1, 2, n, r + 1, differenceArray[r + 1]);

            int mx = max({st[1].dp[0][0], st[1].dp[0][1], st[1].dp[1][0], st[1].dp[1][1]});
            cout << mx << endl;
        }


    }
}
main()
{
    fast;
    if (fopen(TASKNAME".inp","r")){
        freopen(TASKNAME".inp","r",stdin);
        freopen(TASKNAME".out","w",stdout);
    }
    cin >> n >> q;
    if (subtask2::check()) return subtask2::solve(), 0;
    if (subtask3::check()) return subtask3::solve(), 0;
}
/**
Warning:
- MLE / TLE?
- Gioi han mang?
- Gia tri max phai luon gan cho -INF
- long long co can thiet khong?
- tran mang.
- code can than hon
- Nho sinh test de tranh RTE / TLE

--> Coi lai truoc khi nop
**/

Compilation message

Main.cpp:160:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  160 | main()
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:164:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  164 |         freopen(TASKNAME".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:165:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  165 |         freopen(TASKNAME".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 41560 KB Output is correct
2 Correct 5 ms 41564 KB Output is correct
3 Correct 4 ms 41564 KB Output is correct
4 Correct 6 ms 41560 KB Output is correct
5 Correct 4 ms 41596 KB Output is correct
6 Correct 5 ms 41680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 41560 KB Output is correct
2 Correct 5 ms 41564 KB Output is correct
3 Correct 4 ms 41564 KB Output is correct
4 Correct 6 ms 41560 KB Output is correct
5 Correct 4 ms 41596 KB Output is correct
6 Correct 5 ms 41680 KB Output is correct
7 Correct 66 ms 41688 KB Output is correct
8 Correct 60 ms 41560 KB Output is correct
9 Correct 57 ms 41688 KB Output is correct
10 Correct 66 ms 41560 KB Output is correct
11 Correct 51 ms 41680 KB Output is correct
12 Correct 51 ms 41560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 41560 KB Output is correct
2 Correct 5 ms 41564 KB Output is correct
3 Correct 4 ms 41564 KB Output is correct
4 Correct 6 ms 41560 KB Output is correct
5 Correct 4 ms 41596 KB Output is correct
6 Correct 5 ms 41680 KB Output is correct
7 Correct 66 ms 41688 KB Output is correct
8 Correct 60 ms 41560 KB Output is correct
9 Correct 57 ms 41688 KB Output is correct
10 Correct 66 ms 41560 KB Output is correct
11 Correct 51 ms 41680 KB Output is correct
12 Correct 51 ms 41560 KB Output is correct
13 Runtime error 29 ms 79836 KB Execution killed with signal 11
14 Halted 0 ms 0 KB -