社区讨论

震惊!洛谷评测机竟出现……

灌水区参与者 5已保存回复 5

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
5 条
当前快照
1 份
快照标识符
@lqi23rgr
此快照首次捕获于
2023/12/23 20:48
2 年前
此快照最后确认于
2023/12/23 23:05
2 年前
查看原帖
这是我做 P2042 同样一份代码在洛谷 IDE 与 本地 IDE (用了 freopen 读入)输出的结果。
为什么会出现不同的输出???
以下为代码:
CPP
#include<bits/stdc++.h>
#define rll register int
#define F(i,a,b) for(rll i=a;i<=b;i++)
#define Fdn(i,a,b) for(rll i=a;i>=b;i--)
#define R(n) n = read()

using namespace std;

const int inf = 0x3f3f3f3f,mod = 1e9 + 7;
const int maxn = 5e5 + 7;

#define gc getchar()

inline int read(){
	int x=0,sgn=0; char s=gc;
	while(!isdigit(s))sgn|=s=='-',s=gc;
	while(isdigit(s))x=x*10+s-'0',s=gc;
	return sgn?-x:x;
}

struct data{
	int sum, pre, suc, mx;
	inline void Modify(int x, int sz){
		sum = x * sz, pre = suc = mx = x < 0 ? x : sum;
	}
	data operator + (const data &x) const {
		data y;
		y.sum = sum + x.sum, y.pre = max(pre, sum + x.pre),
		y.suc = max(x.suc, x.sum + suc),
		y.mx = max(max(mx, x.mx), suc + x.pre);
		return y;
	}
}d[maxn], emp;

int root, tot, siz[maxn], val[maxn], rnd[maxn], ls[maxn], rs[maxn], tag[maxn];
int top, stk[maxn], md[maxn];

inline void PushUp(int p){
	siz[p] = siz[ls[p]] + 1 + siz[rs[p]],
	d[p] = d[ls[p]] + (data){val[p], val[p], val[p], val[p]} + d[rs[p]];
}

inline void PushDown(int p){
	if(tag[p]){
		tag[ls[p]] ^= 1, tag[rs[p]] ^= 1;
		swap(d[ls[p]].pre, d[ls[p]].suc); 
		swap(d[rs[p]].pre, d[rs[p]].suc);
		swap(ls[p], rs[p]);
		tag[p] = 0;
	}
	if(md[p] != inf){
		val[ls[p]] = val[rs[p]] = md[ls[p]] = md[rs[p]] = md[p],
		d[ls[p]].Modify(md[p], siz[ls[p]]),
		d[rs[p]].Modify(md[p], siz[rs[p]]);
		md[p] = inf;
	}
}

inline int New(int v){
	int x = top ? stk[top--] : ++tot;
	siz[x] = 1, val[x] = v, rnd[x] = rand(),
	d[x] = (data){v, v, v, v}, md[x] = inf;
	return x;
}

inline void DeleteNode(int x){
	siz[x] = val[x] = rnd[x] = ls[x] = rs[x] = tag[x] = 0,
	d[x] = emp, stk[++top] = x;
}

inline void Split(int p, int v, int &x, int &y){
	if(!p){
		x = y = 0;
		return;
	}
	PushDown(p);
	if(siz[ls[p]] >= v) y = p, Split(ls[p], v, x, ls[y]);
	else x = p, Split(rs[p], v - siz[ls[p]] - 1, rs[x], y);
	PushUp(p);
}

inline int Merge(int x, int y){
	if(!x || !y) return x | y;
	PushDown(x), PushDown(y);
	if(rnd[x] > rnd[y]) {
		rs[x] = Merge(rs[x], y), PushUp(x);
		return x;
	} else {
		ls[y] = Merge(x, ls[y]), PushUp(y);
		return y;
	}
}

inline void Empty(int p){
	if(!p) return;
	Empty(ls[p]), Empty(rs[p]), DeleteNode(p);
} 

inline void Insert(){
	int pos, n, x = 0, y = 0, rt = 0;
	R(pos), R(n);
	F(i, 1, n) 
		rt = Merge(rt, New(read()));
	
	Split(root, pos, x, y), Merge(Merge(x, rt), y);
}

inline void YOASOBI(int &x, int &y, int &z){
	int pos, n; R(pos), R(n);
	pos--;
	Split(root, pos + n, x, z), Split(x, pos, x, y);
}

inline void Delete(){
	int x, y, z;
	YOASOBI(x, y, z);
	Empty(y), root = Merge(x, z);
}

inline void Update(){
	int x, y, z;
	YOASOBI(x, y, z);
	int v; R(v);
	val[y] = md[y] = v, d[y].Modify(v, siz[y]), root = Merge(Merge(x, y), z);
}

inline void Reverse() {
	int x, y, z;
	YOASOBI(x, y, z);
	tag[y] ^= 1, root = Merge(Merge(x, y), z);
}

inline void GetSum() {
	int x, y, z;
	YOASOBI(x, y, z);
	cout << d[y].sum << '\n';
	root = Merge(Merge(x, y), z);
}

int yousa, _;

signed main(){
	freopen("P2042_1.in", "r", stdin);
	
	d[0] = emp = {0, -inf, -inf, -inf};
	cin >> yousa >> _;
	int num;
	F(i, 1, yousa) 
		root = Merge(root, New(read()));
	string s;
	while(_--){
		cin >> s;
		if(s == "INSERT") Insert();
		else if(s == "DELETE") Delete();
		else if(s == "MAKE-SAME") Update();
		else if(s == "REVERSE") Reverse();
		else if(s == "GET-SUM") GetSum();
		else cout << d[root].mx << '\n';
	}
	
	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
*/

回复

5 条回复,欢迎继续交流。

正在加载回复...