QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#444131#8522. Waterfall Matrixucup-team1004#WA 1ms8240kbC++143.4kb2024-06-15 17:27:062024-06-15 17:27:07

Judging History

你现在查看的是最新测评结果

  • [2024-06-15 17:27:07]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:8240kb
  • [2024-06-15 17:27:06]
  • 提交

answer

#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
#define all(x) x.begin(),x.end()
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;
const int N=2e5+5,M=N*4+5,K=1000+5,mod=1e9+7,Mod=mod-1;const db eps=1e-9;const ll INF=1e18+7;mt19937 rnd(time(0));
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
namespace Debug{
	Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
	Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
	#ifdef LOCAL
	#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
	#else 
	#define gdb(...) void()
	#endif
}using namespace Debug;
int n;
struct node{int x,y,w;};
ll ans=0;
using info=pair<ll,int>;
namespace Tree{
	#define ls v<<1
	#define rs v<<1|1
	int flag[M];
	ll g[M];
	pair<ll,int> f[M];
	void up(int v){f[v]=min(f[ls],f[rs]);}
	void Pf(int v,int w){f[v].fi+=w;g[v]+=w;flag[v]=0;}
	void P(int v){if(g[v]) Pf(ls,g[v]),Pf(rs,g[v]),g[v]=0;}
	void add(int x,int y,int w,int l=1,int r=n+1,int v=1){
		if(x>y) return;
		flag[v]=0;if(x<=l&&r<=y) return Pf(v,w);int m=l+r>>1;P(v);
		x<=m&&(add(x,y,w,l,m,ls),0);y>m&&(add(x,y,w,m+1,r,rs),0);up(v);
	}
	info qry(int x,int y,int l=1,int r=n+1,int v=1){
		flag[v]=0;if(x<=l&&r<=y) return f[v];int m=l+r>>1;P(v);
		if(y<=m) return qry(x,y,l,m,ls);
		if(x>m) return qry(x,y,m+1,r,rs);
		return min(qry(x,y,l,m,ls),qry(x,y,m+1,r,rs));
	}
	void modify(int x,info w,int l=1,int r=n+1,int v=1){
		flag[v]=0;if(l==r){f[v]=min(f[v],w);return;}P(v);
		int m=l+r>>1;x<=m?modify(x,w,l,m,ls):modify(x,w,m+1,r,rs);up(v);
	}
	void clr(int l=1,int r=n+1,int v=1){
		if(flag[v]) return;
		f[v]=make_pair(INF,0);g[v]=0;flag[v]=1;
		if(l==r) return;
		int m=l+r>>1;clr(l,m,ls);clr(m+1,r,rs);
	}
	#undef ls
	#undef rs
}
int pre[N];
void calc(int l,int r,vector<node> A){
	if(A.empty()) return;
	if(l==r){
		for(auto i:A) ans+=abs(i.w-l),gdb(i.x,i.y,i.w,l);
		return;
	}
	int mid=l+r>>1;
	sort(all(A),[](node x,node y){return x.x<y.x;});
	A.push_back((node){n+1,0,0});
	int m=A.size()-1;
	Tree::clr();
	pre[m]=0;Tree::modify(1,make_pair(0ll,m));
	for(int l=m-1,r;l>=0;l=r-1){
		for(r=l;r>=0&&A[r].x==A[l].x;r--);r++;
		for(int i=r;i<=l;i++){
			info w=Tree::qry(1,A[i].y);
			pre[i]=w.se;
			Tree::modify(A[i].y+1,make_pair(w.fi,i));
		}
		for(int i=r;i<=l;i++){
			Tree::add(A[i].y+1,n+1,abs(mid+1-A[i].w));
			Tree::add(1,A[i].y,abs(mid-A[i].w));
		}
	}
	int La=Tree::qry(1,n+1).se;
	// gdb(mid,Tree::qry(1,n+1).fi);
	vector<node> A1,A2;
	for(int l=0,r;l<m;l=r+1){
		for(r=l;r<m&&A[l].x==A[r].x;r++);r--;
		for(int i=l;i<=r;i++){
			(A[i].y>=A[La].y+1?A1:A2).push_back(A[i]);
		}
		for(int i=l;i<=r;i++) if(i==La) La=pre[i];
	}
	// gdb(mid);
	calc(l,mid,A1);calc(mid+1,r,A2);
}
void Solve(){
	int i,j;scanf("%d",&n);
	vector<node> A(n);
	for(i=0;i<n;i++) scanf("%d%d%d",&A[i].x,&A[i].y,&A[i].w);
	calc(1,1e9,A);
	printf("%lld\n",ans);
}
int main(){
	int t=1;
	// scanf("%d",&t);
	while(t--) Solve();
	cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 6064kb

input:

5
1 3 5
3 2 1
3 3 3
4 4 1
3 5 4

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 1ms
memory: 5940kb

input:

1
1 1 58383964

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 0ms
memory: 8240kb

input:

2
1 1 204909414
2 2 807091086

output:

602181672

result:

ok 1 number(s): "602181672"

Test #4:

score: 0
Accepted
time: 0ms
memory: 6060kb

input:

4
2 4 107744934
3 2 143843719
4 4 908851567
2 2 307161330

output:

801106633

result:

ok 1 number(s): "801106633"

Test #5:

score: 0
Accepted
time: 1ms
memory: 6224kb

input:

7
1 2 140766572
5 3 795389698
3 6 279722536
2 6 442413348
4 2 475716910
5 4 704493410
5 2 423494885

output:

935621651

result:

ok 1 number(s): "935621651"

Test #6:

score: -100
Wrong Answer
time: 0ms
memory: 5952kb

input:

11
1 5 104443431
4 9 692130290
4 1 18812720
7 3 381505729
7 7 706418558
11 9 242808397
10 4 490383412
3 2 72839501
10 7 883914647
10 6 738589165
11 10 556539693

output:

3442866379

result:

wrong answer 1st numbers differ - expected: '2757182575', found: '3442866379'