Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- //#define int long long
- using ll = long long;
- const int N = 2e5 + 10;
- const ll INF = 0x3f3f3f3f;
- // 动态dp
- class Segment {
- private:
- struct Node {
- ll dp[2][2][2]; // 是否选择了最大值,左端点,右端点
- void init () {
- for (int i = 0; i < 2; i ++) {
- for (int j = 0; j < 2; j ++) {
- for (int k = 0; k < 2; k ++) {
- dp[i][j][k] = -INF;
- }
- }
- }
- dp[0][0][0] = 0;
- }
- void init (int v) {
- for (int i = 0; i < 2; i ++) {
- for (int j = 0; j < 2; j ++) {
- for (int k = 0; k < 2; k ++) {
- dp[i][j][k] = -INF;
- }
- }
- }
- dp[0][0][0] = 0;
- dp[0][1][1] = 1;
- if (v) dp[1][1][1] = v + 1;
- }
- } node[N << 2];
- public:
- #define ls(p) node[p << 1]
- #define rs(p) node[p << 1 | 1]
- void push_up (int p) {
- node[p].init(0);
- for (int a = 0; a < 2; a ++) {
- for (int b = 0; b < 2; b ++) {
- int cur = max(a, b);
- for (int i = 0; i < 2; i ++) {
- for (int j = 0; j < 2; j ++) {
- for (int x = 0; j + x < 2; x ++) {
- for (int y = 0; y < 2; y ++) {
- node[p].dp[cur][i][y] = max(node[p].dp[cur][i][y],
- ls(p).dp[a][i][j] + rs(p).dp[b][x][y]);
- }
- }
- }
- }
- }
- }
- }
- void build (int s, int t, int p = 1) {
- if (s == t) {
- node[p].init();
- return;
- }
- int mid = (s + t) >> 1;
- build(s, mid, p << 1);
- build(mid + 1, t, p << 1 | 1);
- push_up(p);
- }
- void update (int x, int val, int s, int t, int p = 1) {
- cout << "upd: " << x << ' ' << val << ' ' << s << ' ' << t << endl;
- if (s == t) {
- node[p].init(val);
- return;
- }
- int mid = (s + t) >> 1;
- if (x <= mid) update(x, val, s, mid, p << 1);
- else update(x, val, mid + 1, t, p << 1 | 1);
- push_up(p);
- }
- ll query () {
- ll res = 0;
- for (int i = 0; i < 2; i ++) {
- for (int j = 0; j < 2; j ++) {
- res = max(res, node[1].dp[1][i][j]);
- }
- }
- return res;
- }
- };
- Segment seg;
- int n;
- ll mx;
- pair<ll, int> a[N];
- void init () {
- cin >> n;
- mx = 0;
- for (int i = 1; i <= n; i ++) {
- ll cur;
- cin >> cur;
- mx = max(mx, cur);
- a[i] = {cur, i};
- }
- }
- void solve () {
- init();
- seg.build(1, n);
- cout << seg.query() << endl;
- sort(a + 1, a + 1 + n, [&](auto x, auto y){
- return x.first > y.first;
- });
- ll ans = 0;
- for (int i = 1; i <= n; i ++) {
- auto [val, id] = a[i];
- seg.update(id, (val == mx ? val : 0), 1, n);
- ans = max(ans, val + seg.query());
- cout << id << ' ' << val << ' ' << seg.query() << endl;
- }
- cout << ans << endl;
- }
- signed main () {
- ios::sync_with_stdio(0);
- cin.tie(0);
- int _ = 1;
- cin >> _;
- while (_ --) {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement