请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏 中的下划线 _
表示实际输入文件中的空格)
操作编码 | 基本操作中的格式 | 说明 |
---|---|---|
1. 插入 | INSERT posi tot c1 c2 ... ctot | 在当前数组的位置 posi 插入总共 tot 个数字:c1, c2, ..., ctot; 若在数组末尾插入,则 posi 为 0 |
2. 删除 | DELETE posi tot | 从当前数组的位置 posi 删除共计 tot 个数字 |
3. 修改 | MAKE-SAME posi tot c | 将当前数组的位置 posi 开始的 tot 个数字均修改为 c |
4. 翻转 | REVERSE posi tot | 反转从当前数组的位置 posi 开始的 tot 个数字,重新排序后原先的位置变为末尾,末尾变为位置 |
5. 求和 | GET-SUM posi tot | 计算从当前数组的位置 posi 开始的 tot 个数字的和 |
6. 求和最大的子序列 | MAX-SUM | 求出当前数组中和最大的一段子序列,并输出最大和 |
注意:对于操作6,要求子序列是非空的。
输入格式
第 1 行包含两个数 N 和 M,N 表示初始时数列中数的个数,M 表示要进行的操作数目。
第 2 行包含 N 个数字,描述初始时的数列。
以下 M 行,每行一条命令,格式参见问题描述中的表格。
输出格式
对于输入数据中的 GET-SUM
和 MAX-SUM
操作,向输出文件依次打印结果,每个答案(数字)占一行。
数据范围与约定
你可以认为在任何时刻,数列中至少有 1 个数。
输入数据一定是正确的,即指定位置的数在数列中一定存在。
50% 的数据中,任何时刻数列中最多含有 30000 个数;100% 的数据中,任何时刻数列中最多含有 500000 个数。
100% 的数据中,任何时刻数列中任何一个数字均在 [−1000,1000] 内。
100% 的数据中,M≤20000,插入的数字总数不超过 4000000 个,输入文件大小不超过 20 MBytes。
输入样例:
9 8
2 -6 3 5 1 -5 -3 6 3
GET-SUM 5 4
MAX-SUM
INSERT 8 3 -5 7 2
DELETE 12 1
MAKE-SAME 3 3 2
REVERSE 3 6
GET-SUM 5 4
MAX-SUM
输出样例:
-1
10
1
10
解析:
splay综合模板
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <math.h>
#include <map>
#include <sstream>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <stdio.h>
#include <tuple>
using namespace std;
/*
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
*/
typedef long long LL;
//#define int long long
#define ld long double
//#define INT __int128
const LL INF = 0x3f3f3f3f3f3f3f3f;
typedef unsigned long long ULL;
typedef pair<long long, long long> PLL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
const int inf = 0x3f3f3f3f;
const LL mod = 998244353;
const ld eps = 1e-12;
const int N = 5e5 + 10, M = 5e5 + 10;
int n, m;
int A[N];
int node[N], idx, root;
struct Node {
int s[2];
int pos, v, p, size;
int sum, mx, ls, rs;
int rev, sam;
void init(int _v, int _p) {
v = _v, p = _p;
sum = v, mx = v, ls = rs = max(0, v);
size = 1;
rev = sam = 0;
s[0] = s[1] = 0;
}
}tr[N];
void up(int x) {
auto& u = tr[x], & lu = tr[tr[x].s[0]], & ru = tr[tr[x].s[1]];
u.mx = max({ lu.mx, ru.mx, lu.rs + ru.ls + u.v });
u.ls = max(lu.sum + ru.ls + u.v, lu.ls);
u.rs = max(ru.sum + lu.rs + u.v, ru.rs);
u.size = lu.size + ru.size + 1;
u.sum = u.v + lu.sum + ru.sum;
}
void down(int x) {
auto& ls = tr[tr[x].s[0]], & rs = tr[tr[x].s[1]], & u = tr[x];
if (u.rev) {
ls.rev ^= 1;
rs.rev ^= 1;
u.rev = 0;
swap(ls.s[0], ls.s[1]), swap(rs.s[0], rs.s[1]);
swap(ls.ls, ls.rs), swap(rs.ls, rs.rs);
}
if (u.sam) {
u.sam = u.rev = 0;
if (u.s[0]) {
ls.sam = 1;
ls.v = u.v, ls.sum = u.v * ls.size;
}
if (u.s[1]) {
rs.sam = 1;
rs.v = u.v, rs.sum = u.v * rs.size;
}
if (u.v > 0) {
if (u.s[0])ls.mx = ls.ls = ls.rs=ls.sum;
if (u.s[1])rs.mx = rs.ls = rs.rs = rs.sum;
}
else {
if (u.s[0])ls.mx =ls.v, ls.ls = ls.rs = 0;
if (u.s[1])rs.mx =rs.v, rs.ls = rs.rs = 0;
}
}
}
void rotate(int x) {
int y = tr[x].p, z = tr[y].p;
int k = tr[y].s[1] == x;
tr[z].s[tr[z].s[1] == y] = x, tr[x].p = z;
tr[y].s[k] = tr[x].s[k ^ 1], tr[tr[x].s[k ^ 1]].p = y;
tr[x].s[k ^ 1] = y, tr[y].p = x;
up(y), up(x);
}
void splay(int x, int r) {
while (tr[x].p != r) {
int y = tr[x].p, z = tr[y].p;
if (z != r) {
if ((tr[z].s[1] == y) ^ (tr[y].s[1] == x))rotate(x);
else rotate(y);
}
rotate(x);
}
if (!r)root = x;
}
int build(int l, int r, int p) {
int mid = l + r >> 1;
int u = node[idx--];
tr[u].init(A[mid], p);
if (l < mid)tr[u].s[0] = build(l, mid - 1, u);
if (r > mid)tr[u].s[1] = build(mid + 1, r, u);
up(u);
return u;
}
int get(int k) {
int u = root;
while (u) {
down(u);
if (tr[tr[u].s[0]].size + 1 > k)u = tr[u].s[0];
else if (tr[tr[u].s[0]].size + 1 == k)return u;
else if (tr[tr[u].s[0]].size + 1 < k)k -= tr[tr[u].s[0]].size+1, u = tr[u].s[1];
}
return -1;
}
void dfs(int u) {
if (tr[u].s[0])dfs(tr[u].s[0]);
if (tr[u].s[1])dfs(tr[u].s[1]);
node[++idx] = u;
}
void out(int u) {
down(u);
if (tr[u].s[0])out(tr[u].s[0]);
cout << tr[u].v << " ";;
if (tr[u].s[1])out(tr[u].s[1]);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <N; i++)node[++idx] = i;
for (int i = 1; i <= n; i++) {
cin >> A[i];
}
tr[0].mx = A[0] = A[n + 1] = -inf;
root = build(0, n + 1, 0);
/* out(root);
cout << endl;*/
char op[20];
int pos, tot;
while (m--) {
cin >> op;
if (!strcmp(op, "INSERT")) {
cin >> pos >> tot;
for (int i = 0; i < tot; i++) {
cin >> A[i];
}
int l = get(pos + 1), r = get(pos + 2);
splay(l, 0), splay(r, l);
int u = build(0, tot - 1, r);
tr[r].s[0] = u;
up(r), up(l);
/*cout << "=============" << endl;
{
out(root);
cout << endl;
}*/
}
else if (!strcmp(op, "DELETE")) {
cin >> pos >> tot;
//cout << "+++++++++++++" << endl;
int l = get(pos), r = get(pos + tot + 1);
//cout << "++++++++++++ " << tr[l].v << " " << tr[r].v << endl;
splay(l, 0), splay(r, l);
//cout << "___________" << endl;
dfs(tr[r].s[0]);
tr[r].s[0] = 0;
up(r), up(l);
//cout << "__________" << endl;
}
else if (!strcmp(op, "MAKE-SAME")) {
int c;
cin >> pos >> tot >> c;
int l = get(pos), r = get(pos + tot + 1);
splay(l, 0), splay(r, l);
int u = tr[r].s[0];
tr[u].v = c; tr[u].sum = c * tr[u].size; tr[u].sam = 1;
if (c > 0) {
tr[u].ls = tr[u].rs = tr[u].mx = tr[u].sum;
}
else {
tr[u].mx = c, tr[u].ls = tr[u].rs = 0;
}
up(r), up(l);
}
else if (!strcmp(op, "REVERSE")) {
cin >> pos >> tot;
int l = get(pos), r = get(pos + tot + 1);
splay(l, 0), splay(r, l);
auto& son = tr[tr[r].s[0]];
son.rev ^= 1;
swap(son.s[0], son.s[1]);
swap(son.ls, son.rs);
up(r), up(l);
}
else if (!strcmp(op, "GET-SUM")) {
cin >> pos >> tot;
int l = get(pos), r = get(pos + tot + 1);
//cout << "________________" << l << " " << r << endl;
splay(l, 0), splay(r, l);
/*{
out(tr[r].s[0]);
cout << endl;
}*/
auto& son = tr[tr[r].s[0]];
cout << son.sum << endl;
up(r), up(l);
}
else {
cout << tr[root].mx << endl;
}
/*cout << "=============" << endl;
{
out(root);
cout << endl;
}*/
}
return 0;
}
/*
10 20
-231 259 -231 -919 -736 609 241 907 -676 -978
INSERT 0 4 987 348 686 -575
MAKE-SAME 7 2 -841
GET-SUM 5 1
MAKE-SAME 8 4 -742
MAX-SUM
MAKE-SAME 7 4 386
GET-SUM 1 5
MAKE-SAME 2 2 -36
MAX-SUM
GET-SUM 5 3
MAKE-SAME 10 2 579
MAX-SUM
INSERT 9 3 -822 -949 -505
GET-SUM 12 5
INSERT 5 4 934 -427 -839 660
DELETE 3 4
DELETE 5 4
REVERSE 2 8
MAKE-SAME 5 3 772
REVERSE 7 6
9 8
2 -6 3 5 1 -5 -3 6 3
GET-SUM 5 4
MAX-SUM
INSERT 8 3 -5 7 2
DELETE 12 1
MAKE-SAME 3 3 2
REVERSE 3 6
GET-SUM 5 4
MAX-SUM
*/