Submission #1095647

#TimeUsernameProblemLanguageResultExecution timeMemory
1095647vjudge1Naval battle (CEOI24_battle)C++17
46 / 100
2029 ms173308 KiB
#include <bits/stdc++.h>
using namespace std;

using pic = pair<int, char>;
using pii = pair<int, int>;
using node = tuple<int, int, int>;
const int N = 1e6 + 10;

int n, x[N], y[N];
char d[N];

bool crash[N];

int collide(int i, int j) {
    if(d[i] == d[j]) return -1;
    if(x[i] == x[j]) {
        if(y[i] < y[j] && d[i] == 'S' && d[j] == 'N') return (y[j] - y[i]) / 2;
        if(y[i] > y[j] && d[j] == 'S' && d[i] == 'N') return (y[i] - y[j]) / 2;
        return -1;
    }else if(y[i] == y[j]) {
        if(x[i] < x[j] && d[i] == 'E' && d[j] == 'W') return (x[j] - x[i]) / 2;
        if(x[j] < x[i] && d[j] == 'E' && d[i] == 'W') return (x[i] - x[j]) / 2;
        return -1;
    }else if(x[i] + y[i] == x[j] + y[j]) {
        if(x[i] > x[j] && d[i] == 'S' && d[j] == 'E') return x[i] - x[j];
        if(x[i] > x[j] && d[i] == 'W' && d[j] == 'N') return x[i] - x[j];
        if(x[j] > x[i] && d[j] == 'S' && d[i] == 'E') return x[j] - x[i];
        if(x[j] > x[i] && d[j] == 'W' && d[i] == 'N') return x[j] - x[i];
        return -1;
    }else if(x[i] - y[i] == x[j] - y[j]) {
        if(x[i] > x[j] && d[i] == 'N' && d[j] == 'E') return x[i] - x[j];
        if(x[i] > x[j] && d[i] == 'W' && d[j] == 'S') return x[i] - x[j];
        if(x[j] > x[i] && d[j] == 'N' && d[i] == 'E') return x[j] - x[i];
        if(x[j] > x[i] && d[j] == 'W' && d[i] == 'S') return x[j] - x[i];
        return -1;
    }
    return -1;
}

map<pic, set<pii> > mp[4];

priority_queue<node, vector<node>, greater<node> > pq;

void findCollide(int i) {
    if(crash[i]) return;
    int t = INT_MAX, tar = -1;
    for(char c : (d[i] == 'N' || d[i] == 'S' ? "WE"s : "NS"s)) {
        for(auto [op, tx, ty] : vector<node>{node{2, x[i] + y[i], x[i]}, node{3, x[i] - y[i], x[i]}}) {
            auto &s = mp[op][pic{tx, c}];
            auto it = s.lower_bound(pii{ty, N});
            if(it != s.end()) {
                int x = collide(i, it->second);
                if(x != -1 && x < t) {
                    t = x;
                    tar = it->second;
                }
            }
            if(it != s.begin()) {
                it --;
                int x = collide(i, it->second);
                if(x != -1 && x < t) {
                    t = x;
                    tar = it->second;
                }
            }
        }
    }
    {
        char c;
        int op, tx, ty;
        if(d[i] == 'N' || d[i] == 'S') {
            c = ('N' ^ 'S' ^ d[i]);
            op = 0;
            tx = x[i];
            ty = y[i];
        }else{
            c = ('W' ^ 'E' ^ d[i]);
            op = 1;
            tx = y[i];
            ty = x[i];
        }

        auto &s = mp[op][pic{tx, c}];
        auto it = s.lower_bound(pii{ty, N});
        if(it != s.end()) {
            int x = collide(i, it->second);
            if(x != -1 && x < t) {
                t = x;
                tar = it->second;
            }
        }
        if(it != s.begin()) {
            it --;
            int x = collide(i, it->second);
            if(x != -1 && x < t) {
                t = x;
                tar = it->second;
            }
        }
    }
    if(t < INT_MAX) pq.emplace(t, i, tar);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;
    if(n >= 200000) return 0;
    for(int i = 1; i <= n; i ++) {
        cin >> x[i] >> y[i] >> d[i];
        mp[0][pic{x[i], d[i]}].insert({y[i], i});
        mp[1][pic{y[i], d[i]}].insert({x[i], i});
        mp[2][pic{x[i] + y[i], d[i]}].insert({x[i], i});
        mp[3][pic{x[i] - y[i], d[i]}].insert({x[i], i});
    }

    for(int i = 1; i <= n; i ++)
        findCollide(i);
    
    while(pq.size()) {
        auto [t, u, v] = pq.top();
        pq.pop();
        if(crash[u] || crash[v]) {
            findCollide(u);
            continue;
        }
        vector<int> w{u};
        while(pq.size() && get<0>(pq.top()) == t) {
            auto [it, iu, iv] = pq.top();
            pq.pop();

            if(crash[iu] || crash[iv]) {
                findCollide(iu);
                continue;
            }
            w.emplace_back(iu);
        }

        for(int i : w) {
            crash[i] = true;
            mp[0][pic{x[i], d[i]}].erase({y[i], i});
            mp[1][pic{y[i], d[i]}].erase({x[i], i});
            mp[2][pic{x[i] + y[i], d[i]}].erase({x[i], i});
            mp[3][pic{x[i] - y[i], d[i]}].erase({x[i], i});
        }
    }

    for(int i = 1; i <= n; i ++)
        if(!crash[i]) cout << i << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...