Submission #864814

# Submission time Handle Problem Language Result Execution time Memory
864814 2023-10-23T16:32:15 Z danikoynov New Home (APIO18_new_home) C++14
5 / 100
5000 ms 984724 KB
#include<bits/stdc++.h>
#define endl '\n'
 
using namespace std;
typedef long long ll;
 
const int maxn = 6e5 + 10, inf = 1e9;
 
struct store
{
    int x, t, a, b;
}s[maxn];
 
struct query
{
    int l, y, idx;
}task[maxn];
 
int n, k, q;
 
void input()
{
    cin >> n >> k >> q;
    for (int i = 1; i <= n; i ++)
        cin >> s[i].x >> s[i].t >> s[i].a >> s[i].b;
 
    for (int i = 1; i <= q; i ++)
        cin >> task[i].l >> task[i].y, task[i].idx = i;
}
 
unordered_map < int, int > rev;
int dif, back_to[2 * maxn];
 
int get_mid(int left, int right)
{
    if (left == right)
        return rev[left];
    
    int lf = rev[left], rf = rev[right];
    while(lf <= rf)
    {
        int mf = (lf + rf) / 2;
        if (abs(left - back_to[mf]) <= abs(right - back_to[mf]))
            lf = mf + 1;
        else
            rf = mf - 1;
    }
 
    return rf;
}
void compress_data()
{
    vector < int > cor;
    for (int i = 1; i <= n; i ++)
        cor.push_back(s[i].x);
    for (int i = 1; i <= q; i ++)
        cor.push_back(task[i].l);
 
    sort(cor.begin(), cor.end());
    int sz = cor.size();
 
    for (int i = 0; i < cor.size(); i ++)
    {
        if (i != 0 || cor[i - 1] != cor[i])
        {
            dif ++;
            rev[cor[i]] = dif;
            back_to[dif] = cor[i];
        }
    }
}

 
bool cmp_query(query t1, query t2)
{
    return t1.l < t2.l;
}
 
struct event
{
    int type, cor, add, arrive;
 
    event(int _type, int _cor, int _add, int _arrive)
    {
        type = _type;
        cor = _cor;
        add = _add;
        arrive = _arrive;
    }
};
 
bool cmp_event(event e1, event e2)
{
    if (e1.arrive != e2.arrive)
        return e1.arrive < e2.arrive;
 
    if (e1.add != e2.add)
        return e1.add < e2.add;
 
    return e1.cor < e2.cor; /// could have dublicates
}
 
 
 
multiset < int > act[maxn];

struct interval_ray
{
    int s, e;
    pair < int, int > ray;

    interval_ray(int _s, int _e, pair < int, int > _ray)
    {
        s = _s;
        e = _e;
        ray = _ray;
    }
};

vector < interval_ray > seg_left, seg_right;

map < pair < int, int >, int > ray_right[maxn], ray_left[maxn];

void make_left_segment(int start, int finish, int timer, int type)
{
    
    ///cout << "left " << start << " " << finish << " " << timer << endl;
    seg_left.push_back(interval_ray(ray_left[type][{start, finish}], timer - 1, {start, finish}));
    ray_left[type][{start, finish}] = 0;
}

void make_right_segment(int start, int finish, int timer, int type)
{

    seg_right.push_back(interval_ray(ray_right[type][{start, finish}], timer - 1, {start, finish}));
    ray_right[type][{start, finish}] = 0;
}

void add_event(int type, int cor, int timer)
{
    multiset < int > :: iterator it = act[type].upper_bound(cor);
    int aft = *it;
    int bef = *prev(it);
    
    if (bef == -inf && aft == inf)
    {
       
        make_right_segment(-inf, inf, timer, type);
        ray_left[type][{cor, -inf}] = timer;
        ray_right[type][{cor, +inf}] = timer;
    }
    else
    if (bef == - inf)
    {
        make_left_segment(aft, -inf, timer, type);
        int mid = (cor + aft) / 2;
        ray_right[type][{cor, mid}] = timer;
        ray_left[type][{aft, mid + 1}] = timer;
        ray_left[type][{cor, -inf}] = timer;
    }
    else
    if (aft == inf)
    {
        make_right_segment(bef, inf, timer, type);
        int mid = (bef + cor) / 2;
        ray_left[type][{cor, mid + 1}] = timer;
        ray_right[type][{bef, mid}] = timer;
        ray_right[type][{cor, inf}] = timer;
    }
    else
    {
        int mid = (bef + aft) / 2;
        make_right_segment(bef, mid, timer, type);
        make_left_segment(aft, mid + 1, timer, type);
        int mid_left = (bef + cor) / 2;
        ray_right[type][{bef, mid_left}] = timer;
        ray_left[type][{cor, mid_left + 1}] = timer;
        int mid_right = (cor + aft) / 2;
        ray_right[type][{cor, mid_right}] = timer;
        ray_left[type][{aft, mid_right + 1}] = timer;
    }

    act[type].insert(cor);
}


void remove_event(int type, int cor, int timer)
{
    multiset < int > :: iterator it = act[type].find(cor);
    int aft = *next(it);
    int bef = *prev(it);
    
    if (bef == -inf && aft == inf)
    {
        ///cout << "reverse " << timer << endl;

        make_left_segment(cor, -inf, timer, type);
        make_right_segment(cor, +inf, timer, type);
        ray_right[type][{-inf, inf}] = timer;

    }
    else
    if (bef == -inf)
    {

        ///cout << "step " << timer << endl;
        make_left_segment(cor, -inf, timer, type);
        int mid = (cor + aft) / 2;
        make_right_segment(cor, mid, timer, type);
        make_left_segment(aft, mid + 1, timer, type);
                ray_left[type][{aft, -inf}] = timer;


    }
    else
    if (aft == inf)
    {

        make_right_segment(cor, inf, timer, type);
        int mid = (bef + cor) / 2;
        make_left_segment(cor, mid + 1, timer, type);
        make_right_segment(bef, mid, timer, type);
                ray_right[type][{bef, inf}] = timer;
    }
    else
    {
        int mid = (bef + aft) / 2;
        ///assert((ray_right[type][{bef, mid}]) == 0);
        ///assert((ray_left[type][{aft, mid + 1}]) == 0);

        int mid_left = (bef + cor) / 2;
        make_right_segment(bef, mid_left, timer, type);
        make_left_segment(cor, mid_left + 1, timer, type);
        int mid_right = (aft + cor) / 2;
        make_right_segment(cor, mid_right, timer, type);
        make_left_segment(aft, mid_right + 1, timer, type);

                ray_right[type][{bef, mid}] = timer;
        ray_left[type][{aft, mid + 1}] = timer;

    }

    act[type].erase(it);
}
 
int ans[maxn];
 
vector < interval_ray > tree_left[maxn * 4], tree_right[maxn * 4];
int pt_lf[4 * maxn], bs_lf[4 * maxn];
void update_range(int root, int left, int right, int qleft, int qright, interval_ray ray, int type)
{
    if (left > qright || right < qleft)
        return;

    if (left >= qleft && right <= qright)
    {
        if (type == -1)
            tree_left[root].push_back(ray);
        else
            tree_right[root].push_back(ray);
        return;
    }

    int mid = (left + right) / 2;
    update_range(root * 2, left, mid, qleft, qright, ray, type);
    update_range(root * 2 + 1, mid + 1, right, qleft, qright, ray, type);

}

unordered_map < int, int > event_times;
void answer_queries()
{
    sort(task + 1, task + q + 1, cmp_query);
    
    vector < event > events;
    for (int i = 1; i <= n; i ++)
    {
        events.push_back(event(s[i].t, s[i].x, 1, s[i].a));
        events.push_back(event(s[i].t, s[i].x, -1, s[i].b + 1));
    }
 
    sort(events.begin(), events.end(), cmp_event);
 
    for (int i = 1; i <= k; i ++)
    {
        act[i].insert(-inf);
        act[i].insert(inf);
        ray_right[i][{-inf, inf}] = 1;
    }
 
    
    int cnt = 0;
    vector < int > data;
    data.push_back(0);
    data.push_back(1);

    for (event cur : events)
    {
        data.push_back(cur.arrive - 1);
        data.push_back(cur.arrive);

        ///cout << "event " << cur.arrive << " " << cur.add << " " << cur.cor << " " << cur.type << endl;
        if (cur.add == 1)
            add_event(cur.type, cur.cor, cur.arrive);
        else
            remove_event(cur.type, cur.cor, cur.arrive);
    }

    data.push_back(inf - 1);
    data.push_back(inf);

    for (int i = 1; i <= q; i ++)
        data.push_back(task[i].y);

    sort(data.begin(), data.end());
    cnt ++;
    event_times[data[0]] = cnt;
    for (int i = 1; i < data.size(); i ++)
    {
        if (data[i] == data[i - 1])
            continue;
        cnt ++;
        event_times[data[i]] = cnt;
    }

    map < pair < int, int >, int > :: iterator it;
    for (int i = 1; i <= k; i ++)
        for (it = ray_right[i].begin(); it != ray_right[i].end(); it ++)
        {
            ///cout << it -> first.first << " :: " << it -> first.second << " " << it -> second << endl;
            if (it -> second != 0)
                make_right_segment(it -> first.first, it -> first.second, inf, i);
        }


    for (int i = 1; i <= k; i ++)
        for (it = ray_left[i].begin(); it != ray_left[i].end(); it ++)
        {
            if (it -> second != 0)
            {
            ///cout << "here " << endl;
                make_left_segment(it -> first.first, it -> first.second, inf, i);
            }
        }

    
    for (interval_ray cur : seg_left)
    {
        update_range(1, 1, cnt, event_times[cur.s], event_times[cur.e], cur, -1);
    ///    cout << "left ray " << cur.s << " " << cur.e << " " << cur.ray.first << " " << cur.ray.second << endl;
    }

    for (interval_ray cur : seg_right)
    {
        update_range(1, 1, cnt, event_times[cur.s], event_times[cur.e], cur, 1);
        ///cout << "right ray " << cur.s << " " << cur.e << " " << cur.ray.first << " " << cur.ray.second << endl;
    }

    
    for (int i = 1; i <= q; i ++)
    {
        int longest = 0;
        int pos = event_times[task[i].y];
        int root = 1, left = 1, right = cnt;
        while(true)
        {
            ///cout << "step " << root << " " << left << " " << right << endl;

            for (interval_ray cur : tree_right[root])
            {
                if (task[i].l <= cur.ray.second)
                    longest = max(longest, task[i].l - cur.ray.first);
            }

            if (left == right)
                break;

            int mid = (left + right) / 2;
            if (pos <= mid)
            {
                root *= 2;
                right = mid;
            }
            else
            {
                root = root * 2 + 1;
                left = mid + 1;
            }
        }

   ans[task[i].idx] = max(ans[task[i].idx], longest);
    }

    for (int i = q; i > 0; i --)
    {
        int longest = 0;
        int pos = event_times[task[i].y];
        int root = 1, left = 1, right = cnt;
        while(true)
        {
            ///cout << "step " << root << " " << left << " " << right << endl;
            for (interval_ray cur : tree_left[root])
            {
                if (task[i].l >= cur.ray.second)
                    longest = max(longest, cur.ray.first - task[i].l);
            }


            if (left == right)
                break;

            int mid = (left + right) / 2;
            if (pos <= mid)
            {
                root *= 2;
                right = mid;
            }
            else
            {
                root = root * 2 + 1;
                left = mid + 1;
            }
        }

        ans[task[i].idx] = max(ans[task[i].idx], longest);
    }
 
    for (int i = 1; i <= q; i ++)
    {
        if (ans[i] > 2e8)
            cout << -1 << endl;
        else
            cout << ans[i] << endl;
    }
}
void solve()
{
    input();
    compress_data();
    answer_queries();
}
 
void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
int main()
{
    ///speed();
    solve();
    return 0;
}
 
/**
2 1 2
3 1 1 3
5 1 3 4
3 3
3 4
 
 
 
 
4 2 4
3 1 1 10
9 2 2 4
7 2 5 7
4 1 8 10
5 3
5 6
5 9
1 10
 
2 1 3
1 1 1 4
1 1 2 6
1 3
1 5
1 7
 
1 1 1
100000000 1 1 1
1 1
 
 
 
*/

Compilation message

new_home.cpp: In function 'void compress_data()':
new_home.cpp:62:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |     for (int i = 0; i < cor.size(); i ++)
      |                     ~~^~~~~~~~~~~~
new_home.cpp:60:9: warning: unused variable 'sz' [-Wunused-variable]
   60 |     int sz = cor.size();
      |         ^~
new_home.cpp: In function 'void answer_queries()':
new_home.cpp:318:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  318 |     for (int i = 1; i < data.size(); i ++)
      |                     ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 43 ms 205612 KB Output is correct
2 Correct 41 ms 205640 KB Output is correct
3 Correct 41 ms 205640 KB Output is correct
4 Correct 40 ms 205660 KB Output is correct
5 Correct 41 ms 205724 KB Output is correct
6 Correct 44 ms 206160 KB Output is correct
7 Correct 46 ms 206168 KB Output is correct
8 Correct 45 ms 206332 KB Output is correct
9 Correct 43 ms 206160 KB Output is correct
10 Correct 48 ms 206176 KB Output is correct
11 Correct 42 ms 205920 KB Output is correct
12 Correct 43 ms 206164 KB Output is correct
13 Correct 43 ms 205912 KB Output is correct
14 Correct 43 ms 205904 KB Output is correct
15 Correct 44 ms 206424 KB Output is correct
16 Correct 43 ms 206172 KB Output is correct
17 Correct 42 ms 206172 KB Output is correct
18 Correct 43 ms 206172 KB Output is correct
19 Correct 42 ms 206168 KB Output is correct
20 Correct 43 ms 206172 KB Output is correct
21 Correct 42 ms 205896 KB Output is correct
22 Correct 43 ms 206172 KB Output is correct
23 Correct 43 ms 206172 KB Output is correct
24 Correct 43 ms 206244 KB Output is correct
25 Correct 43 ms 206168 KB Output is correct
26 Correct 43 ms 205916 KB Output is correct
27 Correct 46 ms 205916 KB Output is correct
28 Correct 43 ms 205916 KB Output is correct
29 Correct 43 ms 205872 KB Output is correct
30 Correct 42 ms 205908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 205612 KB Output is correct
2 Correct 41 ms 205640 KB Output is correct
3 Correct 41 ms 205640 KB Output is correct
4 Correct 40 ms 205660 KB Output is correct
5 Correct 41 ms 205724 KB Output is correct
6 Correct 44 ms 206160 KB Output is correct
7 Correct 46 ms 206168 KB Output is correct
8 Correct 45 ms 206332 KB Output is correct
9 Correct 43 ms 206160 KB Output is correct
10 Correct 48 ms 206176 KB Output is correct
11 Correct 42 ms 205920 KB Output is correct
12 Correct 43 ms 206164 KB Output is correct
13 Correct 43 ms 205912 KB Output is correct
14 Correct 43 ms 205904 KB Output is correct
15 Correct 44 ms 206424 KB Output is correct
16 Correct 43 ms 206172 KB Output is correct
17 Correct 42 ms 206172 KB Output is correct
18 Correct 43 ms 206172 KB Output is correct
19 Correct 42 ms 206168 KB Output is correct
20 Correct 43 ms 206172 KB Output is correct
21 Correct 42 ms 205896 KB Output is correct
22 Correct 43 ms 206172 KB Output is correct
23 Correct 43 ms 206172 KB Output is correct
24 Correct 43 ms 206244 KB Output is correct
25 Correct 43 ms 206168 KB Output is correct
26 Correct 43 ms 205916 KB Output is correct
27 Correct 46 ms 205916 KB Output is correct
28 Correct 43 ms 205916 KB Output is correct
29 Correct 43 ms 205872 KB Output is correct
30 Correct 42 ms 205908 KB Output is correct
31 Execution timed out 5039 ms 358108 KB Time limit exceeded
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5034 ms 884716 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5056 ms 984724 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 43 ms 205612 KB Output is correct
2 Correct 41 ms 205640 KB Output is correct
3 Correct 41 ms 205640 KB Output is correct
4 Correct 40 ms 205660 KB Output is correct
5 Correct 41 ms 205724 KB Output is correct
6 Correct 44 ms 206160 KB Output is correct
7 Correct 46 ms 206168 KB Output is correct
8 Correct 45 ms 206332 KB Output is correct
9 Correct 43 ms 206160 KB Output is correct
10 Correct 48 ms 206176 KB Output is correct
11 Correct 42 ms 205920 KB Output is correct
12 Correct 43 ms 206164 KB Output is correct
13 Correct 43 ms 205912 KB Output is correct
14 Correct 43 ms 205904 KB Output is correct
15 Correct 44 ms 206424 KB Output is correct
16 Correct 43 ms 206172 KB Output is correct
17 Correct 42 ms 206172 KB Output is correct
18 Correct 43 ms 206172 KB Output is correct
19 Correct 42 ms 206168 KB Output is correct
20 Correct 43 ms 206172 KB Output is correct
21 Correct 42 ms 205896 KB Output is correct
22 Correct 43 ms 206172 KB Output is correct
23 Correct 43 ms 206172 KB Output is correct
24 Correct 43 ms 206244 KB Output is correct
25 Correct 43 ms 206168 KB Output is correct
26 Correct 43 ms 205916 KB Output is correct
27 Correct 46 ms 205916 KB Output is correct
28 Correct 43 ms 205916 KB Output is correct
29 Correct 43 ms 205872 KB Output is correct
30 Correct 42 ms 205908 KB Output is correct
31 Execution timed out 5039 ms 358108 KB Time limit exceeded
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 43 ms 205612 KB Output is correct
2 Correct 41 ms 205640 KB Output is correct
3 Correct 41 ms 205640 KB Output is correct
4 Correct 40 ms 205660 KB Output is correct
5 Correct 41 ms 205724 KB Output is correct
6 Correct 44 ms 206160 KB Output is correct
7 Correct 46 ms 206168 KB Output is correct
8 Correct 45 ms 206332 KB Output is correct
9 Correct 43 ms 206160 KB Output is correct
10 Correct 48 ms 206176 KB Output is correct
11 Correct 42 ms 205920 KB Output is correct
12 Correct 43 ms 206164 KB Output is correct
13 Correct 43 ms 205912 KB Output is correct
14 Correct 43 ms 205904 KB Output is correct
15 Correct 44 ms 206424 KB Output is correct
16 Correct 43 ms 206172 KB Output is correct
17 Correct 42 ms 206172 KB Output is correct
18 Correct 43 ms 206172 KB Output is correct
19 Correct 42 ms 206168 KB Output is correct
20 Correct 43 ms 206172 KB Output is correct
21 Correct 42 ms 205896 KB Output is correct
22 Correct 43 ms 206172 KB Output is correct
23 Correct 43 ms 206172 KB Output is correct
24 Correct 43 ms 206244 KB Output is correct
25 Correct 43 ms 206168 KB Output is correct
26 Correct 43 ms 205916 KB Output is correct
27 Correct 46 ms 205916 KB Output is correct
28 Correct 43 ms 205916 KB Output is correct
29 Correct 43 ms 205872 KB Output is correct
30 Correct 42 ms 205908 KB Output is correct
31 Execution timed out 5039 ms 358108 KB Time limit exceeded
32 Halted 0 ms 0 KB -