Language: C++

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair < ll , ll > Pi;
typedef pair < Pi , Pi > PPi;
typedef pair < ll , Pi > Ti;
const ll LLINF = 1LL << 60;
const int INF = (1 << 30) - 1;

char ch[] = "NESW";
ll dy[] = {-1, 0, 1, 0};
ll dx[] = {0, 1, 0, -1};

ll llabs(ll n) { return (n < 0 ? -n : n); }

ll N, K;
string S;
Pi route[200005];
ll vecy = 0, vecx = 0;
vector < PPi > data;
ll ans = 0;
ll sum[4][200005];
int main()
{
    cin >> N >> K >> S;
    for(int i = 0; i < N; i++) {
        int j = 0;
        while(S[i] != ch[j]) j++;
        vecy += dy[j], vecx += dx[j];
        route[i + 1] = Pi(vecy, vecx);
    }
    route[0] = Pi(INF, 2 * N);
    for(int i = 1; i <= N; i++) {
        if(vecy < 0) route[i].first = -route[i].first;
        if(vecx < 0) route[i].second = -route[i].second;
        if(vecx == 0) swap(route[i].first, route[i].second);
        route[i].first += INF, route[i].second += N * 2;
    }
    vecy = llabs(vecy); vecx = llabs(vecx);
    if(vecx == 0) vecx = vecy, vecy = 0;
    sort(route, route + N + 1);
    if(vecx == 0) {
        for(int i = 0; i <= N;) {
            ll y = route[i].first, x = route[i].second;
            ll *a = route, *b = route + N + 1;
            if(binary_search(a, b, Pi(y + 1, x)) && binary_search(a, b, Pi(y, x + 1)) && binary_search(a, b, Pi(y + 1, x + 1))) ans++;
            while(i <= N && route[i].first == y && route[i].second == x) i++;
        }
        cout << ans << endl;
        return (0);
    }
    for(int i = 0; i <= N; i++) {
        ll y = route[i].first, x = route[i].second;
        data.push_back(PPi(Pi(y - x / vecx * vecy, x % vecx), Pi(x / vecx, x / vecx + K)));
    }
    sort(data.begin(), data.end());
    data.erase(unique(data.begin(), data.end()), data.end());
    for(int i = 0; i < data.size();) {
        vector < ll > comp;
        vector < Ti > d;
        map < ll, ll > conv;
        
        ll now[4] = {};

        ll r, u, ru;
        ll iy = data[i].first.first, ix = data[i].first.second;
        ll plus = (ix + 1) / vecx;
        Pi pp[4] = {data[i].first, Pi(iy - plus * vecy, (ix + 1) % vecx), Pi(iy + 1, ix), Pi(iy + 1 - plus * vecy, (ix + 1) % vecx)};
        Pi nop = Pi(-LLINF, -LLINF);
        ll pos[4];
        pos[0] = i;
        for(int j = 1; j < 4; j++) pos[j] = lower_bound(data.begin(), data.end(), PPi(pp[j], nop)) - data.begin();
        for(int j = 0; j < 4; j++) {
            while(pos[j] < data.size() && data[pos[j]].first == pp[j]) {
                ll s = data[pos[j]].second.first - plus * (j % 2) , t = data[pos[j]].second.second - plus * (j % 2);
                comp.push_back(s); comp.push_back(t);
                d.push_back(Ti(1, Pi(j, s)));
                d.push_back(Ti(-1, Pi(j, t)));
                pos[j]++;
            }
        }
        sort(comp.begin(), comp.end());
        comp.erase(unique(comp.begin(), comp.end()), comp.end());
        for(int j = 0; j < comp.size(); j++) conv[comp[j]] = j;
        for(int p = 0; p < 4; p++) for(int j = 0; j < comp.size(); j++) sum[p][j] = 0;
        for(int j = 0; j < d.size(); j++) sum[d[j].second.first][conv[d[j].second.second]] += d[j].first;
        for(int j = 0; j < comp.size() - 1; j++) {
            bool flag = true;
            for(int k = 0; k < 4; k++) {
                now[k] += sum[k][j];
                if(now[k] <= 0) flag = false;
            }
            if(flag) ans += comp[j + 1] - comp[j];
        }
        i = pos[0];
    }
    cout << ans << endl;
    return (0);
}


Compiler output:
a.cc: In function 'int main()':
a.cc:46:21: error: cannot convert 'Pi*' {aka 'std::pair<long long int, long long int>*'} to 'll*' {aka 'long long int*'} in initialization
   46 |             ll *a = route, *b = route + N + 1;
      |                     ^~~~~
      |                     |
      |                     Pi* {aka std::pair<long long int, long long int>*}
a.cc:46:43: error: cannot convert 'Pi*' {aka 'std::pair<long long int, long long int>*'} to 'll*' {aka 'long long int*'} in initialization
   46 |             ll *a = route, *b = route + N + 1;
      |                                 ~~~~~~~~~~^~~
      |                                           |
      |                                           Pi* {aka std::pair<long long int, long long int>*}
a.cc:55:9: error: reference to 'data' is ambiguous
   55 |         data.push_back(PPi(Pi(y - x / vecx * vecy, x % vecx), Pi(x / vecx, x / vecx + K)));
      |         ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:16: note:                 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > > data'
   21 | vector < PPi > data;
      |                ^~~~
a.cc:57:10: error: reference to 'data' is ambiguous
   57 |     sort(data.begin(), data.end());
      |          ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:16: note:                 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > > data'
   21 | vector < PPi > data;
      |                ^~~~
a.cc:57:24: error: reference to 'data' is ambiguous
   57 |     sort(data.begin(), data.end());
      |                        ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:16: note:                 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > > data'
   21 | vector < PPi > data;
      |                ^~~~
a.cc:58:5: error: reference to 'data' is ambiguous
   58 |     data.erase(unique(data.begin(), data.end()), data.end());
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:16: note:                 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > > data'
   21 | vector < PPi > data;
      |                ^~~~
a.cc:58:23: error: reference to 'data' is ambiguous
   58 |     data.erase(unique(data.begin(), data.end()), data.end());
      |                       ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:16: note:                 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > > data'
   21 | vector < PPi > data;
      |                ^~~~
a.cc:58:37: error: reference to 'data' is ambiguous
   58 |     data.erase(unique(data.begin(), data.end()), data.end());
      |                                     ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:16: note:                 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > > data'
   21 | vector < PPi > data;
      |                ^~~~
a.cc:58:50: error: reference to 'data' is ambiguous
   58 |     data.erase(unique(data.begin(), data.end()), data.end());
      |                                                  ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:16: note:                 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > > data'
   21 | vector < PPi > data;
      |                ^~~~
a.cc:59:24: error: reference to 'data' is ambiguous
   59 |     for(int i = 0; i < data.size();) {
      |                        ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class