Submission #1228942

#TimeUsernameProblemLanguageResultExecution timeMemory
1228942mactoddloverNew Home (APIO18_new_home)C++17
Compilation error
0 ms0 KiB
#include<bits/stdc++.h>

#define MASK(i) (1 << (i))
#define pub push_back
#define all(v) v.begin(), v.end()
#define compact(v) v.erase(unique(all(v)), end(v))
#define pii pair<int,int>
#define fi first
#define se second
#define endl "\n"
#define sz(v) (int)(v).size()
#define dbg(x) "[" #x " = " << (x) << "]"

using namespace std;

template<class T> bool minimize(T& a, T b){if(a > b) return a = b, true;return false;}
template<class T> bool maximize(T& a, T b){if(a < b) return a = b, true;return false;}

typedef long long ll;
typedef long double ld;

mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);}

const int N = 1e6 + 15;
const int limit = 1e8;
const int INF = 1e9;

struct node{
    int type, x, time, id;
    node() {}
    node(int type, int x, int time, int id) : type(type), x(x), time(time), id(id) {}
};

int n, k, q, m;
vector<node> evt;
vector<int> compress;
multiset<int> ms[N];//current max for position l
multiset<int> coord[N];//lists of coordinates of type t

namespace SMT{
    int st[N << 2];
    int idNode[N];

    void build(int l = 0, int r = m, int id = 1){

        if(l == r) return void(idNode[l] = id);
        int mid = (l+r) >> 1;
        build(l, mid, id<<1);
        build(mid+1, r, id<<1|1);
    }

    void update(int pos){
        int id = idNode[pos];
        st[id] = (ms[pos].empty()) ? 0 : *ms[pos].rbegin();
        while(id >>= 1){
            st[id] = max(st[id<<1], st[id<<1|1]);
        }
    }

    int get(int pos, int l = 0, int r = m, int id = 1){
        if(l == r) return st[id];
        int mid = (l+r) >> 1;
        if(pos <= mid) return get(pos, l, mid, id<<1);
        return max(st[id<<1], get(pos, mid+1, r, id<<1|1));
    }
}

int ans[N];

void solve(){
    cin >> n >> k >> q;

    compress.push_back(-INF); compress.push_back(INF);
    for(int i = 1; i <= n; i++){
        int x, t, l, r; cin >> x >> t >> l >> r;
        evt.emplace_back(0, x, l, t);
        evt.emplace_back(1, x, r+1, t);
        compress.push_back(x);
    }

    for(int i = 1; i <= q; i++){
        int x, y; cin >> x >> y;
        evt.emplace_back(2, x, y, i);
    }

    sort(all(compress)); compact(compress);
    sort(all(evt), [&](node a, node b){
        return a.time == b.time ? a.type < b.type : a.time < b.time;
    });

    m = sz(compress);

    for(int i = 1; i <= k; i++){
        coord[i].insert(-INF); coord[i].insert(INF);
        ms[0].insert(INF);
    }

    SMT::build();
    SMT::update(0);

    auto add = [&](int l, int r) -> void{
        l = lower_bound(all(compress), l) - compress.begin();
        ms[l].insert(r);
        SMT::update(l);
    };

    auto del = [&](int l, int r) -> void{
        l = lower_bound(all(compress), l) - compress.begin();
        ms[l].erase(ms[l].find(r));
        SMT::update(l);
    };

    auto update = [&](bool act, int x, int type) -> void{
        if(act){
            coord[type].insert(x);
            auto id = coord[type].lower_bound(x);
            int l = -INF, r = INF;
            if(id != coord[type].begin()) l = *prev(id);
            if(next(id) != coord[type].end()) r = *next(id);
            del(l, r);
            add(l, x);
            add(x, r);
        }
        else{
            auto id = coord[type].lower_bound(x);
            int l = -INF, r = INF;
            if(id != coord[type].begin()) l = *prev(id);
            if(next(id) != coord[type].end()) r = *next(id);
            coord[type].erase(id);
            del(l, x);
            del(x, r);
            add(l, r);
        }
    };

    auto query = [&](int x) -> int{
        int l = 0, r = limit, res = -1;
        while(l <= r){
            int mid = (l+r) >> 1;
            int boundL = max(1, x - mid), boundR = min(limit, x + mid);
            boundL = lower_bound(all(compress), boundL) - compress.begin() - 1;
            if(SMT::get(boundL) <= boundR){
                res = mid;
                r = mid -1;
            }
            else l = mid + 1;
        }
        return res;
    };

    for(auto [type, x, time, id] : evt){
        if(type < 2){
            update(type ^ 1, x, id);
        }
        else{
            ans[id] = query(x);
        }
    }

    for(int i = 1; i <= q; i++) cout << ans[i] << endl;
}

signed main(){
    ios_base::sync_with_stdio(NULL);
    cin.tie(0); cout.tie(0);

    #define task "task"

    if(fopen(task".INP", "r")){
        freopen(task".INP", "r", stdin);
        freopen(task".OUT", "w", stdout);
    }

    int t; t = 1; //cin >> t;
    while(t--) solve();
}
#include<bits/stdc++.h>

#define MASK(i) (1 << (i))
#define pub push_back
#define all(v) v.begin(), v.end()
#define compact(v) v.erase(unique(all(v)), end(v))
#define pii pair<int,int>
#define fi first
#define se second
#define endl "\n"
#define sz(v) (int)(v).size()
#define dbg(x) "[" #x " = " << (x) << "]"

using namespace std;

template<class T> bool minimize(T& a, T b){if(a > b) return a = b, true;return false;}
template<class T> bool maximize(T& a, T b){if(a < b) return a = b, true;return false;}

typedef long long ll;
typedef long double ld;

mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);}

const int N = 1e6 + 15;
const int limit = 1e8;
const int INF = 1e9;

struct node{
    int type, x, time, id;
    node() {}
    node(int type, int x, int time, int id) : type(type), x(x), time(time), id(id) {}
};

int n, k, q, m;
vector<node> evt;
vector<int> compress;
multiset<int> ms[N];//current max for position l
multiset<int> coord[N];//lists of coordinates of type t

namespace SMT{
    int st[N << 2];
    int idNode[N];

    void build(int l = 0, int r = m, int id = 1){

        if(l == r) return void(idNode[l] = id);
        int mid = (l+r) >> 1;
        build(l, mid, id<<1);
        build(mid+1, r, id<<1|1);
    }

    void update(int pos){
        int id = idNode[pos];
        st[id] = (ms[pos].empty()) ? 0 : *ms[pos].rbegin();
        while(id >>= 1){
            st[id] = max(st[id<<1], st[id<<1|1]);
        }
    }

    int get(int pos, int l = 0, int r = m, int id = 1){
        if(l == r) return st[id];
        int mid = (l+r) >> 1;
        if(pos <= mid) return get(pos, l, mid, id<<1);
        return max(st[id<<1], get(pos, mid+1, r, id<<1|1));
    }
}

int ans[N];

void solve(){
    cin >> n >> k >> q;

    compress.push_back(-INF); compress.push_back(INF);
    for(int i = 1; i <= n; i++){
        int x, t, l, r; cin >> x >> t >> l >> r;
        evt.emplace_back(0, x, l, t);
        evt.emplace_back(1, x, r+1, t);
        compress.push_back(x);
    }

    for(int i = 1; i <= q; i++){
        int x, y; cin >> x >> y;
        evt.emplace_back(2, x, y, i);
    }

    sort(all(compress)); compact(compress);
    sort(all(evt), [&](node a, node b){
        return a.time == b.time ? a.type < b.type : a.time < b.time;
    });

    m = sz(compress);

    for(int i = 1; i <= k; i++){
        coord[i].insert(-INF); coord[i].insert(INF);
        ms[0].insert(INF);
    }

    SMT::build();
    SMT::update(0);

    auto add = [&](int l, int r) -> void{
        l = lower_bound(all(compress), l) - compress.begin();
        ms[l].insert(r);
        SMT::update(l);
    };

    auto del = [&](int l, int r) -> void{
        l = lower_bound(all(compress), l) - compress.begin();
        ms[l].erase(ms[l].find(r));
        SMT::update(l);
    };

    auto update = [&](bool act, int x, int type) -> void{
        if(act){
            coord[type].insert(x);
            auto id = coord[type].lower_bound(x);
            int l = -INF, r = INF;
            if(id != coord[type].begin()) l = *prev(id);
            if(next(id) != coord[type].end()) r = *next(id);
            del(l, r);
            add(l, x);
            add(x, r);
        }
        else{
            auto id = coord[type].lower_bound(x);
            int l = -INF, r = INF;
            if(id != coord[type].begin()) l = *prev(id);
            if(next(id) != coord[type].end()) r = *next(id);
            coord[type].erase(id);
            del(l, x);
            del(x, r);
            add(l, r);
        }
    };

    auto query = [&](int x) -> int{
        int l = 0, r = limit, res = -1;
        while(l <= r){
            int mid = (l+r) >> 1;
            int boundL = max(1, x - mid), boundR = min(limit, x + mid);
            boundL = lower_bound(all(compress), boundL) - compress.begin() - 1;
            if(SMT::get(boundL) <= boundR){
                res = mid;
                r = mid -1;
            }
            else l = mid + 1;
        }
        return res;
    };

    for(auto [type, x, time, id] : evt){
        if(type < 2){
            update(type ^ 1, x, id);
        }
        else{
            ans[id] = query(x);
        }
    }

    for(int i = 1; i <= q; i++) cout << ans[i] << endl;
}

signed main(){
    ios_base::sync_with_stdio(NULL);
    cin.tie(0); cout.tie(0);

    #define task "task"

    if(fopen(task".INP", "r")){
        freopen(task".INP", "r", stdin);
        freopen(task".OUT", "w", stdout);
    }

    int t; t = 1; //cin >> t;
    while(t--) solve();
}

Compilation message (stderr)

new_home.cpp:193:24: error: redefinition of 'template<class T> bool minimize(T&, T)'
  193 | template<class T> bool minimize(T& a, T b){if(a > b) return a = b, true;return false;}
      |                        ^~~~~~~~
new_home.cpp:16:24: note: 'template<class T> bool minimize(T&, T)' previously declared here
   16 | template<class T> bool minimize(T& a, T b){if(a > b) return a = b, true;return false;}
      |                        ^~~~~~~~
new_home.cpp:194:24: error: redefinition of 'template<class T> bool maximize(T&, T)'
  194 | template<class T> bool maximize(T& a, T b){if(a < b) return a = b, true;return false;}
      |                        ^~~~~~~~
new_home.cpp:17:24: note: 'template<class T> bool maximize(T&, T)' previously declared here
   17 | template<class T> bool maximize(T& a, T b){if(a < b) return a = b, true;return false;}
      |                        ^~~~~~~~
new_home.cpp:199:12: error: redefinition of 'std::mt19937_64 rng'
  199 | mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
      |            ^~~
new_home.cpp:22:12: note: 'std::mt19937_64 rng' previously declared here
   22 | mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
      |            ^~~
new_home.cpp:200:4: error: redefinition of 'll rand(ll, ll)'
  200 | ll rand(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);}
      |    ^~~~
new_home.cpp:23:4: note: 'll rand(ll, ll)' previously defined here
   23 | ll rand(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);}
      |    ^~~~
new_home.cpp:202:11: error: redefinition of 'const int N'
  202 | const int N = 1e6 + 15;
      |           ^
new_home.cpp:25:11: note: 'const int N' previously defined here
   25 | const int N = 1e6 + 15;
      |           ^
new_home.cpp:203:11: error: redefinition of 'const int limit'
  203 | const int limit = 1e8;
      |           ^~~~~
new_home.cpp:26:11: note: 'const int limit' previously defined here
   26 | const int limit = 1e8;
      |           ^~~~~
new_home.cpp:204:11: error: redefinition of 'const int INF'
  204 | const int INF = 1e9;
      |           ^~~
new_home.cpp:27:11: note: 'const int INF' previously defined here
   27 | const int INF = 1e9;
      |           ^~~
new_home.cpp:206:8: error: redefinition of 'struct node'
  206 | struct node{
      |        ^~~~
new_home.cpp:29:8: note: previous definition of 'struct node'
   29 | struct node{
      |        ^~~~
new_home.cpp:212:5: error: redefinition of 'int n'
  212 | int n, k, q, m;
      |     ^
new_home.cpp:35:5: note: 'int n' previously declared here
   35 | int n, k, q, m;
      |     ^
new_home.cpp:212:8: error: redefinition of 'int k'
  212 | int n, k, q, m;
      |        ^
new_home.cpp:35:8: note: 'int k' previously declared here
   35 | int n, k, q, m;
      |        ^
new_home.cpp:212:11: error: redefinition of 'int q'
  212 | int n, k, q, m;
      |           ^
new_home.cpp:35:11: note: 'int q' previously declared here
   35 | int n, k, q, m;
      |           ^
new_home.cpp:212:14: error: redefinition of 'int m'
  212 | int n, k, q, m;
      |              ^
new_home.cpp:35:14: note: 'int m' previously declared here
   35 | int n, k, q, m;
      |              ^
new_home.cpp:213:14: error: redefinition of 'std::vector<node> evt'
  213 | vector<node> evt;
      |              ^~~
new_home.cpp:36:14: note: 'std::vector<node> evt' previously declared here
   36 | vector<node> evt;
      |              ^~~
new_home.cpp:214:13: error: redefinition of 'std::vector<int> compress'
  214 | vector<int> compress;
      |             ^~~~~~~~
new_home.cpp:37:13: note: 'std::vector<int> compress' previously declared here
   37 | vector<int> compress;
      |             ^~~~~~~~
new_home.cpp:215:15: error: redefinition of 'std::multiset<int> ms [1000015]'
  215 | multiset<int> ms[N];//current max for position l
      |               ^~
new_home.cpp:38:15: note: 'std::multiset<int> ms [1000015]' previously declared here
   38 | multiset<int> ms[N];//current max for position l
      |               ^~
new_home.cpp:216:15: error: redefinition of 'std::multiset<int> coord [1000015]'
  216 | multiset<int> coord[N];//lists of coordinates of type t
      |               ^~~~~
new_home.cpp:39:15: note: 'std::multiset<int> coord [1000015]' previously declared here
   39 | multiset<int> coord[N];//lists of coordinates of type t
      |               ^~~~~
new_home.cpp:219:9: error: redefinition of 'int SMT::st [4000060]'
  219 |     int st[N << 2];
      |         ^~
new_home.cpp:42:9: note: 'int SMT::st [4000060]' previously declared here
   42 |     int st[N << 2];
      |         ^~
new_home.cpp:220:9: error: redefinition of 'int SMT::idNode [1000015]'
  220 |     int idNode[N];
      |         ^~~~~~
new_home.cpp:43:9: note: 'int SMT::idNode [1000015]' previously declared here
   43 |     int idNode[N];
      |         ^~~~~~
new_home.cpp:222:10: error: redefinition of 'void SMT::build(int, int, int)'
  222 |     void build(int l = 0, int r = m, int id = 1){
      |          ^~~~~
new_home.cpp:45:10: note: 'void SMT::build(int, int, int)' previously defined here
   45 |     void build(int l = 0, int r = m, int id = 1){
      |          ^~~~~
new_home.cpp:230:10: error: redefinition of 'void SMT::update(int)'
  230 |     void update(int pos){
      |          ^~~~~~
new_home.cpp:53:10: note: 'void SMT::update(int)' previously defined here
   53 |     void update(int pos){
      |          ^~~~~~
new_home.cpp:238:9: error: redefinition of 'int SMT::get(int, int, int, int)'
  238 |     int get(int pos, int l = 0, int r = m, int id = 1){
      |         ^~~
new_home.cpp:61:9: note: 'int SMT::get(int, int, int, int)' previously defined here
   61 |     int get(int pos, int l = 0, int r = m, int id = 1){
      |         ^~~
new_home.cpp:246:5: error: redefinition of 'int ans [1000015]'
  246 | int ans[N];
      |     ^~~
new_home.cpp:69:5: note: 'int ans [1000015]' previously declared here
   69 | int ans[N];
      |     ^~~
new_home.cpp:248:6: error: redefinition of 'void solve()'
  248 | void solve(){
      |      ^~~~~
new_home.cpp:71:6: note: 'void solve()' previously defined here
   71 | void solve(){
      |      ^~~~~
new_home.cpp:341:8: error: redefinition of 'int main()'
  341 | signed main(){
      |        ^~~~
new_home.cpp:164:8: note: 'int main()' previously defined here
  164 | signed main(){
      |        ^~~~
new_home.cpp: In function 'int main()':
new_home.cpp:171:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  171 |         freopen(task".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:172:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  172 |         freopen(task".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp: In function 'int main()':
new_home.cpp:348:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  348 |         freopen(task".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:349:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  349 |         freopen(task".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~