Submission #208917

# Submission time Handle Problem Language Result Execution time Memory
208917 2020-03-12T12:43:48 Z ryansee Jousting tournament (IOI12_tournament) C++14
100 / 100
536 ms 39288 KB
#include "bits/stdc++.h"
using namespace std;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }

typedef long long ll; 
typedef long double ld;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
typedef pair<ll,ll>pi; typedef pair<ll,pi>spi; typedef pair<pi,pi>dpi;

#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (100006)
ll n, c, r, A[MAXN];
struct node {
	int s,e,m;
	ll v;
	node *l,*r;
	node(int S,int E){
		s=S,e=E,m=(s+e)>>1;
		v=0;
		if(s^e){
			l=new node(s,m);
			r=new node(m+1,e);
			v=max(l->v,r->v);
		} else v=A[s];
	}
	ll rmq(int x,int y){
		if(s==x&&e==y) return v;
		if(x>m) return r->rmq(x,y);
		else if(y<=m) return l->rmq(x,y);
		else return max(l->rmq(x,m),r->rmq(m+1,y));
	}
} *seg;
struct fen {
	ll fw[MAXN];
	fen () { mmst(fw,0); }
	void update(int x,int y,ll nval) {
		pupdate(x,nval), pupdate(y+1,-nval);
	}
	void pupdate(int x,ll nval) {
		++ x;
		for(;x<MAXN;x+=x&(-x)) fw[x]+=nval;
	}
	ll sum(int x) { // point sum btw
		++ x;
		ll res=0; for(;x;x-=x&(-x)) res+=fw[x]; return res;
	}
} fen;
struct node2 {
	int s,e,m;
	ll v,add;
	node2 *l,*r;
	node2(int S,int E){
		s=S,e=E,m=(s+e)>>1;
		v=-1; add=LLINF;
		if(s^e){
			l=new node2(s,m);
			r=new node2(m+1,e);
		}
	}
	void update(int x,int y,ll nval) {
		value();
		if(s==x&&e==y) {
			add=nval;
			return;
		}
		if(x>m) r->update(x,y,nval);
		else if(y<=m) l->update(x,y,nval);
		else l->update(x,m,nval),r->update(m+1,y,nval);
	}
	ll rmq(int x) {
		value();
		if(s==e) return v;
		if(x>m) return r->rmq(x); else return l->rmq(x);
	}
	ll value() {
		if(add==LLINF) return v;
		v=add;
		if(s^e)l->add=add,r->add=add;
		add=LLINF;
		return v;
	}
} *link;
struct node3 {
	int s,e,m;
	ll v,add;
	node3*l,*r;
	node3(int S,int E) {
		s=S,e=E,m=(s+e)>>1;
		v=e-s+1; add=LLINF;
		if(s^e) {
			l=new node3(s,m);
			r=new node3(m+1,e);
		}
	}
	void update(int x,int y) {
		value();
		if(s==x&&e==y) {
			add=0;
			return;
		}
		if(x>m) r->update(x,y);
		else if(y<=m) l->update(x,y);
		else l->update(x,m),r->update(m+1,y);
		v=l->value()+r->value();
	}
	ll value() {
		if(add==LLINF) return v;
		v=0;
		if(s^e) l->add=add,r->add=add;
		add=LLINF;
		return v;
	}
	ll rmq(int x,int y) {
		value();
		if(s==x&&e==y) {
			return v;
		}
		if(x>m) return r->rmq(x,y);
		else if(y<=m) return l->rmq(x,y);
		else return l->rmq(x,m)+r->rmq(m+1,y);
	}
} *have;
int GetBestPosition(int N, int C, int R, int *K, int *S, int *E) {
	n=N,c=C,r=R;
	FOR(i,0,n-2)A[i]=K[i];
	seg=new node(0,n-2);
	have=new node3(0,n-1);
	link=new node2(0,n-1);
	FOR(i,0,c-1) {
		ll s=S[i],e=E[i];
		// ll co = 0; bool done=0; FOR(i,0,n-1) { co += have[i]; if(co==s+1) { s=i; done=1; break; } if(~link[i]){assert(have[link[i]]==0); i=link[i]-1; continue; }  } assert(done);
		// co=0; done=0; FOR(i,0,n-1) { co+=have[i]; if(~link[i]){assert(have[link[i]]==0); i=link[i]-1; continue; } if(co==e+1) { e=i; done=1; break; } } assert(done);
		// binary search for first s, and e, for the e connect to link
		ll st=-1, en=n-1, mid=0;
		while(en-st>1) {
			mid=(st+en)>>1;
			if(have->rmq(0,mid) >= s+1) en=mid;
			else st=mid;
		}
		s=en;
		st=-1, en=n-1, mid=0;
		while(en-st>1) {
			mid=(st+en)>>1;
			if(have->rmq(0,mid) >= e+1) en=mid;
			else st=mid;
		}
		e=link->rmq(en)==-1?en:link->rmq(en);
		assert(s+1<=e), have->update(s+1,e);
		assert(s<=e-1), link->update(s,e-1,e);
		tie(S[i],E[i])=tie(s,e);
	}
	FOR(i,0,c-1) {
		if(seg->rmq(S[i], E[i]-1) < r) {
			fen.update(S[i], E[i], 1);
		}
	}
	ll ans=-1,pos=-1;
	FOR(i,0,n-1) {
		if(fen.sum(i)>ans)ans=fen.sum(i),pos=i;
	} assert(~pos);
	return pos;
}
/*
5 3 3
1 0 2 4
1 3
0 1
0 1
*/
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1144 KB Output is correct
2 Correct 5 ms 1144 KB Output is correct
3 Correct 6 ms 1272 KB Output is correct
4 Correct 7 ms 1272 KB Output is correct
5 Correct 6 ms 1272 KB Output is correct
6 Correct 6 ms 1272 KB Output is correct
7 Correct 6 ms 1276 KB Output is correct
8 Correct 7 ms 1272 KB Output is correct
9 Correct 6 ms 1272 KB Output is correct
10 Correct 6 ms 1272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1532 KB Output is correct
2 Correct 21 ms 2936 KB Output is correct
3 Correct 9 ms 2936 KB Output is correct
4 Correct 21 ms 2936 KB Output is correct
5 Correct 19 ms 2936 KB Output is correct
6 Correct 15 ms 2936 KB Output is correct
7 Correct 19 ms 2936 KB Output is correct
8 Correct 19 ms 2936 KB Output is correct
9 Correct 8 ms 2936 KB Output is correct
10 Correct 22 ms 3192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 191 ms 19976 KB Output is correct
2 Correct 426 ms 39160 KB Output is correct
3 Correct 73 ms 37456 KB Output is correct
4 Correct 414 ms 39288 KB Output is correct
5 Correct 536 ms 37880 KB Output is correct
6 Correct 399 ms 38520 KB Output is correct
7 Correct 446 ms 39288 KB Output is correct
8 Correct 420 ms 39160 KB Output is correct
9 Correct 54 ms 37240 KB Output is correct
10 Correct 91 ms 37424 KB Output is correct