Submission #15359

# Submission time Handle Problem Language Result Execution time Memory
15359 2015-07-12T06:42:28 Z gs14004 통로 위의 개미 (kriii3_X) C++14
0 / 85
661 ms 524288 KB
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long lint;

int l, q;

int ants_kth[100005];

struct node{
	node *ls, *rs;
	int v;
	node(){
		ls = rs = NULL;
		v = 0;
	}
};

void add(int pos, int ps, int pe, node* p){
	p->v++;
	if(ps == pe) return;
	int pm = (ps + pe) / 2;
	if(pos <= pm){
		if(!p->ls) p->ls = new node();
		add(pos, ps, pm, p->ls);
	}
	else{
		if(!p->rs) p->rs = new node();
		add(pos, pm + 1, pe, p->rs);
	}
}

int sum(int s, int e, int ps, int pe, node* p){
	if(p == NULL) return 0;
	if(e < ps || pe < s) return 0;
	if(s <= ps && pe <= e) return p->v;
	int pm = (ps+pe)/2;
	return sum(s,e,ps,pm,p->ls) + sum(s,e,pm+1,pe,p->rs);
}

node *root1, *root2;

int get_sum(int s, int e, node *r){
	if(e >= 2 * l){
		return sum(s, 2*l-1, 0, 2*l-1, r) + sum(0, e - 2*l, 0, 2*l-1, r);
	}
	return sum(s, e, 0, 2*l-1, r);
}

int norm(lint x){
	x %= 2 * l;
	x += 2 * l;
	x %= 2 * l;
	return x;
}
int get_low(int p, lint t){
	int ret = 0;
	ret += get_sum(norm(-t), norm(-t) + p, root1);
	ret += get_sum(norm(2 * l - p - t), norm(2 * l - p - t) + p - 1, root1);
	ret += get_sum(norm(t), norm(t) + p, root2);
	ret += get_sum(norm(2 * l - p + t), norm(2 * l - p + t) + p - 1, root2);
	return ret;
}

int main(){
	root1 = new node();
	root2 = new node();
	scanf("%d %d",&l,&q);
	int piv = 1;
	for(int i=0; i<q; i++){
		lint t;
		int p;
		scanf("%lld %d",&t,&p);
		if(p == 1){
			int pos, dx;
			scanf("%d %d",&pos, &dx);
			int k = 1 + get_low(pos,t);
			for(int i=1; i<piv; i++){
				if(ants_kth[i] >= k){
					ants_kth[i]++;
				}
			}
			ants_kth[piv++] = k;
			lint p = 1ll * pos - (1ll * dx * t % (2 * l)) + 2 * l;
			p %= (2 * l);
			if(dx < 0) add(p,0,2*l-1,root2);
			else add(p,0,2*l-1,root1);
		}
		else{
			int x;
			scanf("%d",&x);
			x = ants_kth[x];
			int s = 0, e = l-1;
			while(s != e){
				int m = (s+e)/2;
				if(get_low(m,t) < x) s = m+1;
				else e = m;
			}
			printf("%d\n",s);
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 2128 KB Output is correct
2 Correct 5 ms 1600 KB Output is correct
3 Correct 5 ms 1864 KB Output is correct
4 Correct 5 ms 1732 KB Output is correct
5 Correct 4 ms 1864 KB Output is correct
6 Correct 7 ms 1996 KB Output is correct
7 Memory limit exceeded 661 ms 524288 KB Memory limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Halted 0 ms 0 KB -