寝癖頭の解法

学習中の覚え書きを投稿、更新していきます。

AtCoder Problems in C++ #sengoku - 戦国時代 (Sengoku)

2010年 日本情報オリンピック春合宿OJから、その提出コードの解答例です。
AtCoderとは、コンテストを通じて、プログラミングやアルゴリズムを学習するサービスです。
atcoder.jp
プログラミングコンテストとは、「与えられた問題をいかに素早く、正確に」解くことができるかを競うものです。
競技プログラミング」を略して、「競プロ」などと呼ばれています。

#sengoku - 戦国時代 (Sengoku)

https://www.ioi-jp.org/camp/2010/2010-sp-tasks/2010-sp-day1_20.pdf

僕が作成、提出したコードは、以下のとおりです。

/*
AtCoder Problems in C++
#sengoku - 戦国時代 (Sengoku)
https://www.ioi-jp.org/camp/2010/2010-sp-tasks/2010-sp-day1_20.pdf
提出コードの解答例
https://neguse-atama.hatenablog.com
*/
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
set<ll> s,t;
vector<ll> v[2],w[2];
int main(void){
  ll l,n;
  cin>>l>>n;
  for(ll i=0;i<n;i++){
    ll x,y;
    cin>>x>>y;
    s.insert(x-y);
    t.insert(x+y);
  }
  for(ll x : s){
    v[x&1].push_back(x);
  }
  for(ll x : t){
    w[x&1].push_back(x);
  }
  ll ans=0;
  for(ll x : s){
    ans+=max(0LL,min(l-1,l-1-x)-max(0LL,-x)+1);
  }
  for(ll x : t){
    ans+=max(0LL,min(l-1,x)-max(0LL,x-l+1)+1);
  }
  for(ll i=0;i<2;i++){
    for(ll x : w[i]){
      auto it1=lower_bound(v[i].begin(),v[i].end(),max(-x,x-2*l+2));
      auto it2=upper_bound(v[i].begin(),v[i].end(),min(2*l-2-x,x));
      ans-=max(0LL,((ll)(it2-it1)));
    }
  }
  cout<<ans<<endl;
  return 0;
}