답안 #394061

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
394061 2021-04-25T09:50:43 Z 79brue 새 집 (APIO18_new_home) C++14
5 / 100
5000 ms 233416 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int*, int> INFO;

struct Mart{
    int x, y, t; bool on; /// location, type, time
    Mart(int x, int y, int t, bool on): x(x), y(y), t(t), on(on){}
    bool operator<(const Mart &r)const{
        return t<r.t;
    }
};

struct Query{
    int x, t, idx;
    Query(){}
    Query(int x, int t, int idx): x(x), t(t), idx(idx){}
    bool operator<(const Query &r)const{
        return t<r.t;
    }
};

struct Segment{
    int a, b, l, r, s, e;
    Segment(){}
    Segment(int a, int b, int l, int r, int s, int e): a(a), b(b), l(l), r(r), s(s), e(e){}
};
vector<Segment> segment;

struct maxSegmentTree{
    stack<INFO> vec;
    stack<int> sz;
    int tree[1200002], lazy[1200002];

    void init(int i, int l, int r){
        tree[i] = lazy[i] = -1e9;
        if(l==r) return;
        int m = (l+r)>>1;
        init(i*2, l, m), init(i*2+1, m+1, r);
    }

    inline void save(){
        sz.push((int)vec.size());
    }
    void undo(){
        while(vec.size() > sz.top()){
            INFO tmp = vec.top();
            *(tmp.first) = tmp.second;
            vec.pop();
        }
        sz.pop();
    }

    #define saveinfo(A) vec.push(INFO {&A, A})
    void propagate(int i, int l, int r){
        if(tree[i] < lazy[i]){
            saveinfo(tree[i]);
            tree[i] = lazy[i];
        }
        if(l!=r){
            if(lazy[i*2] < lazy[i]){
                saveinfo(lazy[i*2]);
                lazy[i*2] = lazy[i];
            }
            if(lazy[i*2+1] < lazy[i]){
                saveinfo(lazy[i*2+1]);
                lazy[i*2+1] = lazy[i];
            }
        }
    }

    void rangeMax(int i, int l, int r, int s, int e, int val){
        propagate(i, l, r);
        if(r<s || e<l) return;
        if(s<=l && r<=e){
            if(lazy[i] >= val) return;
            saveinfo(lazy[i]);
            lazy[i] = val;
            propagate(i, l, r);
            return;
        }

        int m = (l+r)>>1;
        rangeMax(i*2, l, m, s, e, val);
        rangeMax(i*2+1, m+1, r, s, e, val);
        saveinfo(tree[i]);
        tree[i] = max(tree[i*2], tree[i*2+1]);
    }

    int getVal(int i, int l, int r, int idx){
        propagate(i, l, r);
        if(l==r) return tree[i];
        int m = (l+r)>>1;
        if(idx <= m) return getVal(i*2, l, m, idx);
        return getVal(i*2+1, m+1, r, idx);
    }
} treeA, treeB;

int n, q, k;

void input();
void createQueries();
void renumber();
void ODCinit();
void ODC(int, int, vector<Segment>&);
void output();

int main(){
    input();
    createQueries();
    renumber();
    ODCinit();
    ODC(1, n+q+1, segment);
    output();
}

vector<Mart> vec;
vector<Query> qvec;
int ans[300002];
vector<int> tVec;

void input(){
    scanf("%d %d %d", &n, &k, &q);
    for(int i=1; i<=n; i++){
        int x, t, a, b;
        scanf("%d %d %d %d", &x, &t, &a, &b);
        vec.push_back(Mart(x, t, a, 1));
        vec.push_back(Mart(x, t, b+1, 0));
        tVec.push_back(t);
    }
    sort(vec.begin(), vec.end());

    for(int i=1; i<=q; i++){
        int l, y;
        scanf("%d %d", &l, &y);
        qvec.push_back(Query(l, y, i));
        tVec.push_back(y);
    }
    sort(qvec.begin(), qvec.end());

    sort(tVec.begin(), tVec.end());
    tVec.erase(unique(tVec.begin(), tVec.end()), tVec.end());
    for(auto &p: vec) p.t = lower_bound(tVec.begin(), tVec.end(), p.t) - tVec.begin() + 1;
    for(auto &p: qvec) p.t = lower_bound(tVec.begin(), tVec.end(), p.t) - tVec.begin() + 1;
}

multiset<int> mst[300002];
unordered_multimap<ll, int> mtm;

inline void addSegment(int l, int r, int t){
    mtm.insert(make_pair(100000000LL * l + r, t));
}

void delSegment(int l, int r, int e){
    auto it = mtm.find(100000000LL * l + r);
    int s = it->second;
    mtm.erase(it);
    if(s>=e) return;

    int mid = (l+r)/2;
    segment.push_back(Segment(1, -l, l, mid, s, e-1));
    segment.push_back(Segment(-1, r, mid+1, r, s, e-1));
}

void createQueries(){
    int pnt = 0;
    qvec.push_back(Query(0, 100000001, 0));
    for(int i=1; i<=k; i++){
        mst[i].insert(-300000000), mst[i].insert(400000000);
        addSegment(-300000000, 400000000, 1);
    }
    for(int i=0; i<q+1; i++){
        while(pnt < 2*n && vec[pnt].t <= qvec[i].t){
            Mart tmp = vec[pnt++];
            if(tmp.on){
                auto it = mst[tmp.y].lower_bound(tmp.x);
                int l = (it == mst[tmp.y].begin() ? -1 : *prev(it));
                int r = (it == mst[tmp.y].end() ? -1 : *it);
                mst[tmp.y].insert(tmp.x);

                if(l!=-1 && r!=-1) delSegment(l, r, tmp.t);
                if(l!=-1) addSegment(l, tmp.x, tmp.t);
                if(r!=-1) addSegment(tmp.x, r, tmp.t);
            }
            else{
                mst[tmp.y].erase(mst[tmp.y].find(tmp.x));

                auto it = mst[tmp.y].lower_bound(tmp.x);
                int l = (it == mst[tmp.y].begin() ? -1 : *prev(it));
                int r = (it == mst[tmp.y].end() ? -1 : *it);

                if(l!=-1 && r!=-1) addSegment(l, r, tmp.t);
                if(l!=-1) delSegment(l, tmp.x, tmp.t);
                if(r!=-1) delSegment(tmp.x, r, tmp.t);
            }
        }
    }
    for(int i=1; i<=k; i++){
        delSegment(-300000000, 400000000, n+q+1);
    }
    assert(mtm.empty());
}

vector<int> renumberVec; int z;
void renumber(){
    for(int i=0; i<q; i++) renumberVec.push_back(qvec[i].x);
    sort(renumberVec.begin(), renumberVec.end());
    renumberVec.erase(unique(renumberVec.begin(), renumberVec.end()), renumberVec.end());
    z = renumberVec.size();
}
inline int g(int x){
    return lower_bound(renumberVec.begin(), renumberVec.end(), x) - renumberVec.begin();
}
inline int g2(int x){
    return upper_bound(renumberVec.begin(), renumberVec.end(), x) - renumberVec.begin() - 1;
}

int pnt = 0;

void ODCinit(){
    treeA.init(1, 0, z-1);
    treeB.init(1, 0, z-1);
}

int pcnt = 0;
void ODC(int l, int r, vector<Segment>& vec){
    assert((int)vec.size() <= 480000);
    treeA.save(); treeB.save();
    vector<Segment> lv, rv;
    int m = (l+r)/2;
    for(Segment s: vec){
//        assert(pcnt++ <= 1000000);
        if(s.s <= l && r <= s.e){
            if(s.a == 1) treeA.rangeMax(1, 0, z-1, g(s.l), g2(s.r), s.b);
            else         treeB.rangeMax(1, 0, z-1, g(s.l), g2(s.r), s.b);
        }
        else{
            if(s.s <= m) lv.push_back(s);
            if(m < s.e)  rv.push_back(s);
        }
    }
    vec.clear();
    vec.shrink_to_fit();

    if(l==r){
        while(pnt < q && qvec[pnt].t == l){
            Query query = qvec[pnt++];
//            printf("%d: %d %d\n", pnt, treeA.getVal(1, 0, z-1, g(query.x)), treeB.getVal(1, 0, z-1, g(query.x)));
            ans[query.idx] = max(treeA.getVal(1, 0, z-1, g(query.x)) + query.x,
                                 treeB.getVal(1, 0, z-1, g(query.x)) - query.x);
        }
        treeA.undo(); treeB.undo();
        return;
    }

    if(pnt < q && qvec[pnt].t <= m) ODC(l, m, lv);
    if(pnt < q && qvec[pnt].t <= r) ODC(m+1, r, rv);
    treeA.undo(); treeB.undo();
}

void output(){
    for(int i=1; i<=q; i++){
        printf("%d\n", ans[i] > 100000000 ? -1 : ans[i]);
    }
}

Compilation message

new_home.cpp: In member function 'void maxSegmentTree::undo()':
new_home.cpp:48:26: warning: comparison of integer expressions of different signedness: 'std::stack<std::pair<int*, int> >::size_type' {aka 'long unsigned int'} and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} [-Wsign-compare]
   48 |         while(vec.size() > sz.top()){
      |               ~~~~~~~~~~~^~~~~~~~~~
new_home.cpp: In function 'void input()':
new_home.cpp:125:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  125 |     scanf("%d %d %d", &n, &k, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:128:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  128 |         scanf("%d %d %d %d", &x, &t, &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:137:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  137 |         scanf("%d %d", &l, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14412 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14400 KB Output is correct
4 Correct 9 ms 14416 KB Output is correct
5 Correct 11 ms 14540 KB Output is correct
6 Correct 18 ms 14764 KB Output is correct
7 Correct 16 ms 14892 KB Output is correct
8 Correct 17 ms 14796 KB Output is correct
9 Correct 17 ms 14924 KB Output is correct
10 Correct 19 ms 14668 KB Output is correct
11 Correct 13 ms 14672 KB Output is correct
12 Correct 13 ms 14668 KB Output is correct
13 Correct 13 ms 14540 KB Output is correct
14 Correct 12 ms 14540 KB Output is correct
15 Correct 17 ms 14668 KB Output is correct
16 Correct 16 ms 14796 KB Output is correct
17 Correct 17 ms 14684 KB Output is correct
18 Correct 16 ms 14668 KB Output is correct
19 Correct 17 ms 14828 KB Output is correct
20 Correct 16 ms 14672 KB Output is correct
21 Correct 10 ms 14668 KB Output is correct
22 Correct 17 ms 14932 KB Output is correct
23 Correct 17 ms 14800 KB Output is correct
24 Correct 16 ms 14820 KB Output is correct
25 Correct 17 ms 14784 KB Output is correct
26 Correct 14 ms 14668 KB Output is correct
27 Correct 12 ms 14668 KB Output is correct
28 Correct 13 ms 14700 KB Output is correct
29 Correct 12 ms 14664 KB Output is correct
30 Correct 12 ms 14540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14412 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14400 KB Output is correct
4 Correct 9 ms 14416 KB Output is correct
5 Correct 11 ms 14540 KB Output is correct
6 Correct 18 ms 14764 KB Output is correct
7 Correct 16 ms 14892 KB Output is correct
8 Correct 17 ms 14796 KB Output is correct
9 Correct 17 ms 14924 KB Output is correct
10 Correct 19 ms 14668 KB Output is correct
11 Correct 13 ms 14672 KB Output is correct
12 Correct 13 ms 14668 KB Output is correct
13 Correct 13 ms 14540 KB Output is correct
14 Correct 12 ms 14540 KB Output is correct
15 Correct 17 ms 14668 KB Output is correct
16 Correct 16 ms 14796 KB Output is correct
17 Correct 17 ms 14684 KB Output is correct
18 Correct 16 ms 14668 KB Output is correct
19 Correct 17 ms 14828 KB Output is correct
20 Correct 16 ms 14672 KB Output is correct
21 Correct 10 ms 14668 KB Output is correct
22 Correct 17 ms 14932 KB Output is correct
23 Correct 17 ms 14800 KB Output is correct
24 Correct 16 ms 14820 KB Output is correct
25 Correct 17 ms 14784 KB Output is correct
26 Correct 14 ms 14668 KB Output is correct
27 Correct 12 ms 14668 KB Output is correct
28 Correct 13 ms 14700 KB Output is correct
29 Correct 12 ms 14664 KB Output is correct
30 Correct 12 ms 14540 KB Output is correct
31 Execution timed out 5078 ms 92316 KB Time limit exceeded
32 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1477 ms 229352 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1655 ms 233416 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14412 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14400 KB Output is correct
4 Correct 9 ms 14416 KB Output is correct
5 Correct 11 ms 14540 KB Output is correct
6 Correct 18 ms 14764 KB Output is correct
7 Correct 16 ms 14892 KB Output is correct
8 Correct 17 ms 14796 KB Output is correct
9 Correct 17 ms 14924 KB Output is correct
10 Correct 19 ms 14668 KB Output is correct
11 Correct 13 ms 14672 KB Output is correct
12 Correct 13 ms 14668 KB Output is correct
13 Correct 13 ms 14540 KB Output is correct
14 Correct 12 ms 14540 KB Output is correct
15 Correct 17 ms 14668 KB Output is correct
16 Correct 16 ms 14796 KB Output is correct
17 Correct 17 ms 14684 KB Output is correct
18 Correct 16 ms 14668 KB Output is correct
19 Correct 17 ms 14828 KB Output is correct
20 Correct 16 ms 14672 KB Output is correct
21 Correct 10 ms 14668 KB Output is correct
22 Correct 17 ms 14932 KB Output is correct
23 Correct 17 ms 14800 KB Output is correct
24 Correct 16 ms 14820 KB Output is correct
25 Correct 17 ms 14784 KB Output is correct
26 Correct 14 ms 14668 KB Output is correct
27 Correct 12 ms 14668 KB Output is correct
28 Correct 13 ms 14700 KB Output is correct
29 Correct 12 ms 14664 KB Output is correct
30 Correct 12 ms 14540 KB Output is correct
31 Execution timed out 5078 ms 92316 KB Time limit exceeded
32 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14412 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14400 KB Output is correct
4 Correct 9 ms 14416 KB Output is correct
5 Correct 11 ms 14540 KB Output is correct
6 Correct 18 ms 14764 KB Output is correct
7 Correct 16 ms 14892 KB Output is correct
8 Correct 17 ms 14796 KB Output is correct
9 Correct 17 ms 14924 KB Output is correct
10 Correct 19 ms 14668 KB Output is correct
11 Correct 13 ms 14672 KB Output is correct
12 Correct 13 ms 14668 KB Output is correct
13 Correct 13 ms 14540 KB Output is correct
14 Correct 12 ms 14540 KB Output is correct
15 Correct 17 ms 14668 KB Output is correct
16 Correct 16 ms 14796 KB Output is correct
17 Correct 17 ms 14684 KB Output is correct
18 Correct 16 ms 14668 KB Output is correct
19 Correct 17 ms 14828 KB Output is correct
20 Correct 16 ms 14672 KB Output is correct
21 Correct 10 ms 14668 KB Output is correct
22 Correct 17 ms 14932 KB Output is correct
23 Correct 17 ms 14800 KB Output is correct
24 Correct 16 ms 14820 KB Output is correct
25 Correct 17 ms 14784 KB Output is correct
26 Correct 14 ms 14668 KB Output is correct
27 Correct 12 ms 14668 KB Output is correct
28 Correct 13 ms 14700 KB Output is correct
29 Correct 12 ms 14664 KB Output is correct
30 Correct 12 ms 14540 KB Output is correct
31 Execution timed out 5078 ms 92316 KB Time limit exceeded
32 Halted 0 ms 0 KB -