답안 #1040534

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1040534 2024-08-01T07:00:11 Z Requiem Sjeckanje (COCI21_sjeckanje) C++17
0 / 110
4 ms 41564 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 = Node();
         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){
            st[nn] = Node();
            if (differenceArray[l] >= 0) st[nn].dp[0][0] = differenceArray[l];
            if (differenceArray[l] <  0) st[nn].dp[1][1] = -differenceArray[l];
        }
        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]);
//            cout << "CURNODE: " << nn << ' ' << l << ' ' << r << endl;
//            for(int i = 0; i < 2; i++){
//                for(int j = 0; j < 2; j++){
//                    cout << st[nn].dp[i][j] << ' ';
//                }
//                cout << endl;
//            }
//            cout << endl;
        }
    }

    void solve(){
        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]);
        }
        cout << max({st[1].dp[0][0], st[1].dp[0][1], st[1].dp[1][0], st[1].dp[1][1]}) << endl;

        for(int i = 1; i <= q; i++){
            int l, r, x;
            cin >> l >> r >> x;
//            cerr << l << ' ' << r << ' ' << x << endl;
            differenceArray[l] += x;
            differenceArray[r + 1] -= x;

//            cout << l << ' ' << r + 1 << ' ' << differenceArray[l] << ' ' << differenceArray[r + 1] << endl;

            if (l >= 2) update(1, 2, n, l, differenceArray[l]);
            if (r + 1 <= n) 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:172:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  172 | main()
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:176:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  176 |         freopen(TASKNAME".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:177:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  177 |         freopen(TASKNAME".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 41564 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 41564 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 41564 KB Output isn't correct
2 Halted 0 ms 0 KB -