This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <cstdio>
#include <vector>
#include <algorithm>
#include <deque>
using namespace std;
typedef long long lint;
lint l;
int q;
int ants_kth[200005];
struct node{
node *ls, *rs;
int v;
node(){
ls = rs = NULL;
v = 0;
}
};
void add(lint pos, lint ps, lint pe, node* p){
p->v++;
if(ps == pe) return;
lint 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(lint s, lint e, lint ps, lint pe, node* p){
if(e < ps || pe < s) return 0;
if(s <= ps && pe <= e) return p->v;;
lint pm = (ps + pe) / 2;
int ret = 0;
if(p->ls) ret += sum(s,e,ps,pm,p->ls);
if(p->rs) ret += sum(s,e,pm+1,pe,p->rs);
return ret;
}
node *root1, *root2;
inline int get_sum(lint s, lint 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);
}
inline lint norm(lint x){
x %= 2 * l;
x += 2 * l;
x %= 2 * l;
return x;
}
int piv = 1;
int get_low(lint p, lint t){
if(p == l) return piv-1;
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 v[705][605], sz[705];
int map_int[200005];
int stk[200005], szz;
void remake(){
for(int i=0; i<=piv / 300; i++){
for(int j=0; j<sz[i]; j++){
stk[szz++] = v[i][j];
}
}
for(int i=0; i<szz; i++){
v[i / 300][i % 300] = stk[i];
map_int[stk[i]] = i / 300;
}
}
int main(){
sz[0] = 1;
root1 = new node();
root2 = new node();
scanf("%lld %d",&l,&q);
for(int i=0; i<q; i++){
lint t, p;
scanf("%lld %lld",&t,&p);
if(i % 300 == 299){
remake();
}
if(p == 1){
lint pos, dx;
scanf("%lld %lld",&pos, &dx);
int k = 1 + get_low(pos,t);
map_int[piv] = k / 300;
sz[k/300]++;
for(int i=sz[k/300] - 1; i > k % 300; i--){
v[k/300][i] = v[k/300][i-1];
}
v[k/300][k%300] = piv;
piv++;
lint p = 1ll * pos - (1ll * dx * t % (2 * l)) + 2 * l;
p %= (2 * l);
p += 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);
int pt = map_int[x];
for(int j=0; j<sz[pt]; j++){
if(v[pt][j] == x){
x = pt * 300 + j;
break;
}
}
int s = 0, e = l;
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |