Submission #154690

#TimeUsernameProblemLanguageResultExecution timeMemory
154690junodeveloper허수아비 (JOI14_scarecrows)C++14
100 / 100
691 ms69240 KiB
#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
int n;
pii p[200010];
vector<pii> tree[4*200010];
void update(int h,int tl,int tr,int x,int y) {
	if(tr<y||y<tl)return;
	while(!tree[h].empty()&&tree[h].back().se<y)
		tree[h].pop_back();
	tree[h].push_back({x,y});
	if(tl==tr)return;
	int mid=(tl+tr)/2;
	update(h*2,tl,mid,x,y);
	update(h*2+1,mid+1,tr,x,y);
}
pii query(int h,int tl,int tr,int l,int r,pii c) {
	if(r<tl||tr<l) return c;
	if(l<=tl&&tr<=r) {
		if(tree[h].empty()) return c;
		int i=lower_bound(all(tree[h]),make_pair(c.fi+1,-1))-tree[h].begin();
		return {max(tree[h].back().fi,c.fi), c.se+sz(tree[h])-i};
	}
	int mid=(tl+tr)/2;
	pii qr=query(h*2+1,mid+1,tr,l,r,c);
	return query(h*2,tl,mid,l,r,qr);
}
int main() {
	scanf("%d",&n);
	int i;
	vector<int> vx,vy;
	for(i=1;i<=n;i++){ 
		scanf("%d%d",&p[i].fi,&p[i].se);
		vx.push_back(p[i].fi);
		vy.push_back(p[i].se);
	}
	sort(all(vx));
	vx.erase(unique(all(vx)),vx.end());
	sort(all(vy));
	vy.erase(unique(all(vy)),vy.end());
	for(i=1;i<=n;i++) {
		p[i].fi=lower_bound(all(vx),p[i].fi)-vx.begin();
		p[i].se=lower_bound(all(vy),p[i].se)-vy.begin();
	}
	sort(p+1,p+n+1,[&](const pii& a,const pii& b) {
		return a.fi==b.fi?a.se>b.se:a.fi<b.fi;
	});
	ll res=0;
	for(i=1;i<=n;i++) {
		pii r=query(1,0,sz(vy),0,p[i].se-1,make_pair(-1,0));
		res+=r.se;
		//printf("(%d,%d):%d\n",vx[p[i].fi],vy[p[i].se],r.se);
		update(1,0,sz(vy),p[i].fi,p[i].se);
	}
	printf("%lld",res);
	return 0;
}

Compilation message (stderr)

scarecrows.cpp: In function 'int main()':
scarecrows.cpp:36:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
scarecrows.cpp:40:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&p[i].fi,&p[i].se);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...