90pts WA#3 求调
查看原帖
90pts WA#3 求调
887476
12345xa楼主2024/3/31 21:26
#include<iostream>
#include<stack>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include <algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<iomanip>
#include<iterator>
#include <bitset>

using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ri register int 
//#define int long long
typedef long long ll;
typedef long double ld;
const int N = 5e5 + 5, M = 5e3 + 5;
const ll INF = 1e18;
const int MOD = 1000000;
const  double alpha = 0.75;
const ll mod = 1e9 + 7;

vector<int> res;
class FHQ {
public:
	int cnt, root;
	stack<int> st;

	struct Node {
		int ls, rs;
		int key, siz;
		int presum, sufsum, maxsum, sum;
		int tag1,tag2;
		int pri;
	}t[N];

	void push_up(int u) {
		t[u].siz = t[t[u].ls].siz + t[t[u].rs].siz + 1;
		t[u].sum = t[t[u].ls].sum + t[u].key + t[t[u].rs].sum;
		t[u].presum = max(max(t[t[u].ls].presum, t[t[u].ls].sum + t[u].key + t[t[u].rs].presum), 0);
		t[u].sufsum = max(max(t[t[u].rs].sufsum, t[t[u].rs].sum + t[u].key + t[t[u].ls].sufsum), 0);
		t[u].maxsum = max(t[t[u].ls].maxsum, t[t[u].rs].maxsum);
		t[u].maxsum = max(t[u].maxsum, max(t[t[u].ls].sufsum + t[t[u].rs].presum, 0) + t[u].key);
	}
	void addtag1(int u) {
		t[u].tag1 ^= 1;
		swap(t[u].presum, t[u].sufsum);
	}
	void addtag2(int u,int x) {
		t[u].tag2 = 1;
		t[u].key = x;
		t[u].sum = t[u].siz * x;
		t[u].presum = max(0, t[u].sum);
		t[u].sufsum = max(0, t[u].sum);
		t[u].maxsum = max(x, t[u].sum);
	}
	void push_down(int u) {
		if (t[u].tag1) {
			swap(t[u].ls, t[u].rs);
			addtag1(t[u].ls);
			addtag1(t[u].rs);
			t[u].tag1 = 0;
		}
		if (t[u].tag2) {
			addtag2(t[u].ls, t[u].key);
			addtag2(t[u].rs, t[u].key);
			t[u].tag2 = 0;
		}
	}
	int newNode(int x) {
		int idx;
		if (st.size()) idx = st.top(), st.pop();
		else idx = ++cnt;
		t[idx].ls = t[idx].rs = 0;
		t[idx].key = x;
		t[idx].tag1 = 0, t[idx].tag2 = -1e9;
		t[idx].siz = 1;
		t[idx].presum = t[idx].sufsum = max(0, x);
		t[idx].maxsum = t[idx].sum = x;
		t[idx].pri = rand();

		return idx;
	}
	void Split(int u, int x, int& L, int& R) {
		if (u == 0) { L = R = 0; return; }
		push_down(u);
		if (t[t[u].ls].siz + 1 <= x) {
			L = u;
			Split(t[u].rs, x - t[t[u].ls].siz - 1, t[u].rs, R);
		}
		else {
			R = u;
			Split(t[u].ls, x, L, t[u].ls);
		}
		push_up(u);
	}
	int Merge(int L, int R) {
		if (L == 0 || R == 0) {
			push_down(L + R);
			push_up(L + R);
			return L + R;
		}
		if (t[L].pri > t[R].pri) {
			push_down(L);
			t[L].rs = Merge(t[L].rs, R);
			push_up(L);
			return L;
		}
		else {
			push_down(R);
			t[R].ls = Merge(L, t[R].ls);
			push_up(R);
			return R;
		}
	}
	void Inorder(int u) {
		if (u == 0) return;
		Inorder(t[u].ls);
		st.push(u);
		Inorder(t[u].rs);
		
	}
	void Insert() {
		int pos, len;
		cin >> pos >> len;
		int L, R;
		Split(root, pos, L, R);
		for (int i = 1; i <= len; i++) {
			int x; cin >> x;
			L = Merge(L, newNode(x));
		}
		root = Merge(L, R);
	}
	void Del() {
		int pos, len;
		cin >> pos >> len;
		--pos;
		int L, R, p;
		Split(root, pos + len, L, R);
		Split(L, pos, L, p);
		Inorder(p);
		root = Merge(L, R);
	}
	void Update() {
		int pos, len, x;
		cin >> pos >> len >> x;
		--pos;
		int L, R, p;
		Split(root, pos + len, L, R);
		Split(L, pos, L, p);
		addtag2(p, x);
		root = Merge(Merge(L, p), R);
	}
	void Reverse() {
		int pos, len;
		cin >> pos >> len;
		--pos;
		int L, R, p;
		Split(root, pos + len, L, R);
		Split(L, pos, L, p);
		addtag1(p);
		root = Merge(Merge(L, p), R);
	}
	void Sum() {
		int pos, len;
		cin >> pos >> len;
		--pos;
		int L, R, p;
		Split(root, pos + len, L, R);
		Split(L, pos, L, p);
		res.push_back(t[p].sum);
		root = Merge(Merge(L, p), R);
	}
	void Max_sum() {
		res.push_back(t[root].maxsum);
	}
}t;

int main() {
	IOS;
	srand(time(NULL));
	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		int x; cin >> x;
		t.root = t.Merge(t.root, t.newNode(x));
	}
	for (int i = 1; i <= m; i++) {
		string s;
		cin >> s;
		if (s == "INSERT") {
			t.Insert();
		}
		else if (s == "DELETE") {
			t.Del();
		}
		else if (s == "MAKE-SAME") {
			t.Update();
		}
		else if (s == "REVERSE") {
			t.Reverse();
		}
		else if (s == "GET-SUM") {
			t.Sum();
		}
		else if (s == "MAX-SUM") {
			t.Max_sum();
		}
	}
	for (auto v : res) cout << v << endl;

	return 0;
}
2024/3/31 21:26
加载中...