关于测试数据的疑问
查看原帖
关于测试数据的疑问
419473
丿青衣挽红袖楼主2021/10/8 17:52

下面会贴一下自己的ac代码,其实ac的挺莫名其妙的。。。因为改的几个点思考过之后都觉得是没错的点。

1.首先是n的范围的问题,开的是maxn*8,但是会re,按常理来说应该即使pushup叶节点maxn*8应该也是足够的。。。

2.自己的ac代码里build函数里的k改成a也可以ac,数据的x是不是一个重复的也没有。。。按常理来说只能是去重之后的数组大小k,a代表的是所有x的个数(包括了重复)。

3.最让人不解的是tree里的v改成longlong就过了,x最大距离也是1e9,v相当于题解中的length,不应该int也是可以的吗?这个改了过了实在是太懵了,一下子都没反应过来就ac了。

向各位大佬们求解。

ac代码:


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<iomanip>
#include<utility>
#include<stack>
#include<list>
#include<fstream>
#define SYNC ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define PI acos(-1.0)
#define inf 0x3f3f3f3f
#define F(i,m,n) for(int i=m;i<=n;i++)
#define f(i,m,n) for(int i=m;i>=n;i--)
#define debug(n) if(deb==1) cout<<#n<<"="<<n<<endl
#define deb(n) if(deb==1) cout<<#n<<"="<<n<<"   "
#define div(ch) if(deb==1) { for(int i=1;i<=40;i++) cout<<#ch; cout<<endl; }
#define mm(A,n) memset(A,n,sizeof(A))
#define lowbit(x) (x&(-x))
#define PII pair<int,int>
typedef long long LL;
const int maxn = 1e5 + 10;
const int maxm = 3e7 + 10;
const int mod = 571373;
const double eps = 1e-9;
const bool deb = 0;
using namespace std;

struct type {
	int l, r, cnt;
	LL v;
}tree[maxn * 8];
struct type2 {
	int l, r, h, v;
	type2() {}
	type2(int x1, int x2, int hi, int val) {
		l = x1, r = x2, h = hi, v = val;
	}
	bool operator <(const type2& a)const {
		return h < a.h;
	}
}line[maxn * 2];
int x[maxn * 2];

void spepushup(int rt) {
	tree[rt].l = tree[rt * 2].l;
	tree[rt].r = tree[rt * 2 + 1].r;
	tree[rt].v = 0;
	tree[rt].cnt = 0;
}

void pushup(int rt) {
	if (tree[rt].cnt) tree[rt].v = x[tree[rt].r + 1] - x[tree[rt].l];
	else if (tree[rt].l == tree[rt].r) tree[rt].v = 0;
	else tree[rt].v = tree[rt * 2].v + tree[rt * 2 + 1].v;
}

void build(int l, int r, int rt) {
	if (l == r) {
		tree[rt].l = tree[rt].r = l;
		tree[rt].cnt = 0;
		tree[rt].v = 0;
		return;
	}
	int mid = (l + r) / 2;
	build(l, mid, rt * 2);
	build(mid + 1, r, rt * 2 + 1);
	spepushup(rt);
}

void update(int l, int r, int rt, int v) {
	if (tree[rt].l >= l && tree[rt].r <= r) {
		tree[rt].cnt += v;
		pushup(rt);
		return;
	}
	if (tree[rt * 2].r >= l) update(l, r, rt * 2, v);
	if (tree[rt * 2 + 1].l <= r) update(l, r, rt * 2 + 1, v);
	pushup(rt);
}

void oper() {
	int n;
	cin >> n;
	int x1, y1, x2, y2;
	int a = 0, b = 0;
	F(i, 1, n) {
		cin >> x1 >> y1 >> x2 >> y2;
		x[++a] = x1; line[++b] = type2(x1, x2, y1, -1);
		x[++a] = x2; line[++b] = type2(x1, x2, y2, 1);
	}
	sort(x + 1, x + a + 1);
	sort(line + 1, line + b + 1);
	int k = unique(x + 1, x + a + 1) - x - 1;
	LL ans = 0;
	build(1, a - 1, 1);
	F(i, 1, b - 1) {
		div(__);
		debug(i);
		deb(line[i].l); deb(line[i].r); deb(line[i].v); debug(line[i].h);
		int l = lower_bound(x + 1, x + k + 1, line[i].l) - x;
		int r = lower_bound(x + 1, x + k + 1, line[i].r) - x;
		r--;
		deb(l); debug(r);
		update(l, r, 1, -line[i].v);
		if (i < b) ans += tree[1].v * (line[i + 1].h - line[i].h);
	}
	cout << ans << endl;
}

int main() {
	SYNC;
	int t = 1;
	//cin >> t;
	while (t--)
		oper();
	return 0;
}
/*Avoid Stereotypes, Think More, and Code Less.*/
/*Instead of focusing on troublesome old ideas, it's
better to learn or think about efficient new methods*/
2021/10/8 17:52
加载中...