Submission #62809

# Submission time Handle Problem Language Result Execution time Memory
62809 2018-07-30T06:47:28 Z Benq Tram (CEOI13_tram) C++11
100 / 100
154 ms 13420 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 30001;

set<pair<ll,pi>> cans;
set<int> cur;
int n,m;
pi pos[MX];
bool in[150001][2];

void mod(pair<ll,pi> a, int x) {
    if (x == 1) cans.insert(a);
    else cans.erase(a);
}

void zz(int a, int b, int t, int x ) {
    F0R(i,2) {
        ll dis0 = (ll)(a-t)*(a-t)+(a!=-MOD&&!in[a][i]);
        ll dis1 = (ll)(b-t)*(b-t)+(b!=MOD&&!in[b][i]);
        mod({-min(dis0,dis1),{t,i}},x);
    }
}

void tri(int a, int b, int x, int z = 0) { // [a,b)
    int t = (a+b)/2;
    t = max(t,1); t = min(t,n);
    if (t > max(a,1)) zz(a,b,t-1,x);
    if (t < min(b,n)) zz(a,b,t+1,x);
    zz(a,b,t,x);
}

void ad(int ind) {
    pos[ind] = cans.begin()->s;
    
    auto it = cur.lb(pos[ind].f);
    
    if (*it == pos[ind].f) {
        tri(*prev(it),*it,-1), tri(*it,*next(it),-1);
    } else {
        tri(*prev(it),*it,-1);
    }
    
    in[pos[ind].f][pos[ind].s] = 1;
    cur.insert(pos[ind].f);
    it = cur.find(pos[ind].f);
    tri(*prev(it),*it,1), tri(*it,*next(it),1);
}

void del(int ind) {
    auto it = cur.find(pos[ind].f);
    tri(*prev(it),*it,-1);
    tri(*it,*next(it),-1);
    
    in[pos[ind].f][pos[ind].s] = 0;
    if (in[pos[ind].f][0] || in[pos[ind].f][1]) {
        tri(*prev(it),*it,1);
        tri(*it,*next(it),1);
    } else {
        tri(*prev(it),*next(it),1);
        cur.erase(it);
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> m;
    cur.insert(-MOD), cur.insert(MOD);
    tri(-MOD,MOD,1);
    
    FOR(i,1,m+1) {
        char c; cin >> c;
        if (c == 'E') {
            ad(i);
            cout << pos[i].f << " " << pos[i].s+1 << "\n";
        } else {
            int p; cin >> p;
            del(p);
        }
    }
}

/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( ) 
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# Verdict Execution time Memory Grader output
1 Correct 2 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 748 KB Output is correct
2 Correct 4 ms 364 KB Output is correct
3 Correct 5 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 620 KB Output is correct
2 Correct 4 ms 364 KB Output is correct
3 Correct 6 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 1292 KB Output is correct
2 Correct 4 ms 364 KB Output is correct
3 Correct 5 ms 876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 1260 KB Output is correct
2 Correct 4 ms 364 KB Output is correct
3 Correct 4 ms 876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 3372 KB Output is correct
2 Correct 102 ms 876 KB Output is correct
3 Correct 95 ms 3036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 154 ms 13420 KB Output is correct
2 Correct 74 ms 872 KB Output is correct
3 Correct 120 ms 4560 KB Output is correct