Submission #318487

# Submission time Handle Problem Language Result Execution time Memory
318487 2020-11-02T05:45:09 Z jhnah917 Alternating Current (BOI18_alternating) C++14
19 / 100
162 ms 12892 KB
#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define compress(v) sort(all(v)), v.erase(unique(all(v)), v.end())
using namespace std;

typedef long long ll;

int par[101010];
vector<int> g[101010];
int find(int v){ return v == par[v] ? v : par[v] = find(par[v]); }
void merge(int pa, int ch){ pa = find(pa); ch = find(ch); if(pa != ch) par[ch] = pa; }

struct Seg{
    int tree[1 << 18], tmp[1 << 18], n;
    void init(int _n){ n = _n; memset(tree, 0, sizeof tree); memset(tmp, 0, sizeof tree); }
    void push(int node, int s, int e){
        if(!tmp[node]) return;
        tree[node] += tmp[node];
        if(s != e){
            tmp[node << 1] += tmp[node];
            tmp[node << 1 | 1] += tmp[node];
        }
        tmp[node] = 0;
    }
    void update(int node, int s, int e, int l, int r, int v){
        push(node, s, e);
        if(r < s || e < l) return;
        if(l <= s && e <= r){
            tmp[node] += v; push(node, s, e);
            return;
        }
        int m = s + e >> 1;
        update(node << 1, s, m, l, r, v);
        update(node << 1 | 1, m+1, e, l, r, v);
        tree[node] = min(tree[node << 1], tree[node << 1 | 1]);
    }
    int query(int node, int s, int e, int l, int r){
        push(node, s, e);
        if(r < s || e < l) return 1e9;
        if(l <= s && e <= r) return tree[node];
        int m = s + e >> 1;
        int t1 = query(node << 1, s, m, l, r);
        int t2 = query(node << 1 | 1, m+1, e, l, r);
        return min(t1, t2);
    }
    void update(int s, int e, int x){
        if(e >= n) e -= n;
        if(s <= e) update(1, 0, n-1, s, e, x);
        else update(1, 0, n-1, 0, e, x), update(1, 0, n-1, s, n-1, x);
    }
    int query(){ return query(1, 0, n-1, 0, n-1); }
} seg[2];

struct Info{
    int s, e, x, eli, color;
    Info() : Info(0, 0, 0) {}
    Info(int s, int e, int x) : s(s), e(e), x(x), eli(-1), color(-1) {}
    bool operator < (const Info &t) const { return make_pair(s, -e) < make_pair(t.s, -t.e); }
};

int n, m, ed, ed_idx;
Info a[101010];
vector<Info> v;

void calc_elimination(){
    deque<Info> dq;
    sort(a+1, a+m+1);
    for(int i=1; i<=m; i++){
        if(dq.empty() || dq.back().e < a[i].e) dq.push_back(a[i]);
        else a[i].eli = dq.back().x;
    }
    while(dq.size() && dq.front().e < ed) a[dq[0].x].eli = ed_idx, dq.pop_front();
    for(auto i : dq) v.push_back(i);
    
    iota(par, par+101010, 0);
    for(int i=1; i<=m; i++) if(a[i].eli != -1) merge(a[i].eli, a[i].x);
    for(int i=1; i<=m; i++){
        if(a[i].eli != -1) a[i].eli = find(a[i].x), g[a[i].eli].push_back(a[i].x);
        else g[a[i].x].push_back(a[i].x);
    }
}

inline bool eval(){ return seg[0].query() > 0 && seg[1].query() > 0; }

void even(){
    seg[0].init(n); seg[1].init(n);
    
    for(int i=0; i<v.size(); i++) a[v[i].x].color = i % 2;
    for(int i=1; i<=m; i++) if(a[i].color == -1) a[i].color = !a[a[i].eli].color;
    
    for(int i=1; i<=m; i++){
        seg[a[i].color].update(a[i].s, a[i].e, 1);
    }
    if(eval()){
        for(int i=1; i<=m; i++) cout << a[i].color;
    }
    else cout << "impossible";
}

void odd(){
    seg[0].init(n); seg[1].init(n);
    for(int i=0; i<v.size(); i++) a[v[i].x].color = i % 2;
    for(int i=1; i<=m; i++) if(a[i].color == -1) a[i].color = !a[a[i].eli].color;
    for(int i=1; i<=m; i++){
        seg[a[i].color].update(a[i].s, a[i].e, 1);
    }
    if(eval()){ for(int j=1; j<=m; j++) cout << a[j].color; return; }
    
    for(int i=0; i<v.size(); i++){
        for(auto j : g[v[i].x]){
            seg[a[j].color].update(a[j].s, a[j].e, -1);
            a[j].color ^= 1;
            seg[a[j].color].update(a[j].s, a[j].e, 1);
        }
        if(eval()){ for(int j=1; j<=m; j++) cout << a[j].color; return; }
    }
    cout << "impossible";
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    cin >> n >> m;
    for(int i=1; i<=m; i++){
        int s, e; cin >> s >> e; s--; e--;
        if(s > e){
            if(ed < e) ed = e, ed_idx = i;
            e += n;
        }
        a[i] = {s, e, i};
    }
    calc_elimination();
    sort(a+1, a+m+1, [](Info a, Info b){ return a.x < b.x; });
    if(v.size() % 2 == 0) even();
    else odd();
}

Compilation message

alternating.cpp: In member function 'void Seg::update(int, int, int, int, int, int)':
alternating.cpp:34:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   34 |         int m = s + e >> 1;
      |                 ~~^~~
alternating.cpp: In member function 'int Seg::query(int, int, int, int, int)':
alternating.cpp:43:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   43 |         int m = s + e >> 1;
      |                 ~~^~~
alternating.cpp: In function 'void even()':
alternating.cpp:90:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Info>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |     for(int i=0; i<v.size(); i++) a[v[i].x].color = i % 2;
      |                  ~^~~~~~~~~
alternating.cpp: In function 'void odd()':
alternating.cpp:104:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Info>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |     for(int i=0; i<v.size(); i++) a[v[i].x].color = i % 2;
      |                  ~^~~~~~~~~
alternating.cpp:111:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Info>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |     for(int i=0; i<v.size(); i++){
      |                  ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9196 KB Output is correct
2 Correct 6 ms 9196 KB Output is correct
3 Correct 6 ms 9196 KB Output is correct
4 Correct 6 ms 9196 KB Output is correct
5 Correct 7 ms 9196 KB Output is correct
6 Correct 6 ms 9196 KB Output is correct
7 Correct 7 ms 9196 KB Output is correct
8 Correct 6 ms 9196 KB Output is correct
9 Correct 6 ms 9196 KB Output is correct
10 Correct 6 ms 9196 KB Output is correct
11 Correct 6 ms 9196 KB Output is correct
12 Incorrect 6 ms 9196 KB 'impossible' claimed, but there is a solution
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9196 KB Output is correct
2 Correct 6 ms 9196 KB Output is correct
3 Correct 6 ms 9196 KB Output is correct
4 Correct 6 ms 9196 KB Output is correct
5 Correct 7 ms 9196 KB Output is correct
6 Correct 6 ms 9196 KB Output is correct
7 Correct 7 ms 9196 KB Output is correct
8 Correct 6 ms 9196 KB Output is correct
9 Correct 6 ms 9196 KB Output is correct
10 Correct 6 ms 9196 KB Output is correct
11 Correct 6 ms 9196 KB Output is correct
12 Incorrect 6 ms 9196 KB 'impossible' claimed, but there is a solution
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9196 KB Output is correct
2 Correct 6 ms 9196 KB Output is correct
3 Correct 6 ms 9196 KB Output is correct
4 Correct 6 ms 9196 KB Output is correct
5 Correct 7 ms 9196 KB Output is correct
6 Correct 6 ms 9196 KB Output is correct
7 Correct 7 ms 9196 KB Output is correct
8 Correct 6 ms 9196 KB Output is correct
9 Correct 6 ms 9196 KB Output is correct
10 Correct 6 ms 9196 KB Output is correct
11 Correct 6 ms 9196 KB Output is correct
12 Incorrect 6 ms 9196 KB 'impossible' claimed, but there is a solution
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 43 ms 9828 KB Output is correct
2 Correct 6 ms 9196 KB Output is correct
3 Correct 104 ms 9584 KB Output is correct
4 Correct 47 ms 9584 KB Output is correct
5 Correct 95 ms 12124 KB Output is correct
6 Correct 162 ms 11484 KB Output is correct
7 Correct 87 ms 10660 KB Output is correct
8 Correct 8 ms 9324 KB Output is correct
9 Correct 7 ms 9196 KB Output is correct
10 Correct 101 ms 12252 KB Output is correct
11 Correct 72 ms 10976 KB Output is correct
12 Correct 75 ms 9700 KB Output is correct
13 Correct 6 ms 9196 KB Output is correct
14 Correct 6 ms 9196 KB Output is correct
15 Correct 83 ms 9828 KB Output is correct
16 Correct 108 ms 12772 KB Output is correct
17 Correct 145 ms 9824 KB Output is correct
18 Correct 104 ms 9820 KB Output is correct
19 Correct 13 ms 9196 KB Output is correct
20 Correct 79 ms 12892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9196 KB Output is correct
2 Correct 6 ms 9196 KB Output is correct
3 Correct 6 ms 9196 KB Output is correct
4 Correct 6 ms 9196 KB Output is correct
5 Correct 7 ms 9196 KB Output is correct
6 Correct 6 ms 9196 KB Output is correct
7 Correct 7 ms 9196 KB Output is correct
8 Correct 6 ms 9196 KB Output is correct
9 Correct 6 ms 9196 KB Output is correct
10 Correct 6 ms 9196 KB Output is correct
11 Correct 6 ms 9196 KB Output is correct
12 Incorrect 6 ms 9196 KB 'impossible' claimed, but there is a solution
13 Halted 0 ms 0 KB -