teamnote

created : 2020-08-07T15:10:21+00:00
modified : 2021-02-08T01:53:13+00:00
algorithm teamnote

C++ IO

#define FASTIO() do{ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); } while(0)
int k = 0;
for (int i = n / 2; i >= 1; i /= 2) {
  while (k + i < n && array[k +i] <= x) k += i;
}

if (array[k] == x) {
  // 위치 k 에서 x를 찾음.
}
int x = -1;
for (int b = z; b >= 1; b /= 2) {
  while (!valid(x + b)) x += b;
}
int k = x + 1;
/* valid(x) : true when x >= k, false when x < k */

좌표 압축

vector<int> C; /* C 에다가 좌표들 넣기 */
sort(C.begin(), c.end());
C.erase(unique(C.begin(), C.end()), C.end());

/* 오리지널 좌표(P) -> 압축된 좌표(D) */
int d = distance(C.begin(), lower_bound(C.begin(), C.end(), P));
/* 압축된 좌표(D) -> 오리지널 좌표(P) */
int P = C[D];

Index Tree

#include <iostream>
#include <vector>
using namespace std;
#define IDX_SIZE (1 << 21)
#define IDX_BASE (IDX_SIZE >> 1)
/* min tree */
struct index_tree {
  int sz = IDX_SIZE, bs = IDX_BASE;
  int node[IDX_SIZE];

  void update(int x, int v) {
    x |= bs;
    node[x] = v;
    while (x > 1) {
      x >>= 1;
      node[x] = min(node[x * 2], node[x * 2 + 1]);
    }
  }

  int query(int s, int e) {
    s |= bs;
    e |= bs;
    int retval = 1e9;
    while (s < e) {
      if (s % 2 == 1) retval = min(retval, node[s++]);
      if (e % 2 == 0) retval = min(retval, node[e--]);
      s >>= 1;
      e >>= 1;
    }
    if (s == e) retval = min(retval, node[s]);
    return retval;
  }
};


Segment Tree (Range Update)

#include <iostream>
#include <vector>
using namespace std;
using lld = long long;
#define SEG_SIZE 1 << 18
struct node_t {
  lld value, lazy;
};

/* sum tree */
struct seg_tree {
  node_t node[4 * SEG_SIZE];

  lld build(lld* d, lld idx, lld s, lld e) {
    if (s == e) return node[idx].value = d[s];
    return node[idx].value = build(d, idx * 2, s, (s + e) / 2) + build(d, idx * 2 + 1, (s + e) / 2 + 1, e);
  }

  void update_lazy(lld idx, lld s, lld e) {
    if (node[idx].lazy != 0) {
      node[idx].value += (e - s + 1) * node[idx].lazy;
      if (s != e) {
        node[idx * 2].lazy += node[idx].lazy;
        node[idx * 2 + 1].lazy += node[idx].lazy;
      }
      node[idx].lazy = 0;
    }
  }

  lld update_range(lld idx, lld diff, lld s, lld e, lld l, lld r) {
    update_lazy(idx, s, e);
    if (r < s || l > e) return node[idx].value;
    if (l <= s && e <= r) {
      node[idx].lazy += diff;
      update_lazy(idx, s, e);
      return node[idx].value;
    }
    return node[idx].value =
      update_range(idx * 2, diff ,s, (s + e) / 2, l, r) + update_range(idx * 2 + 1, diff, (s + e) / 2 + 1, e, l, r);
  }

  lld query(lld idx, lld s, lld e, lld l, lld r) {
    update_lazy(idx, s, e);
    if (r < s || l > e) return 0;
    if (l <= s && e <= r) return node[idx].value;
    return query(idx * 2, s, (s + e)/ 2, l, r) + query(idx * 2 + 1, (s + e) / 2 + 1, e, l, r);
  }
}seg;

#define scl(N) scanf("%lld", &(N))

int main () {
  lld N, M, K;
  scl(N), scl(M), scl(K);
  for (lld i = 1; i <= N; i++) {
    lld a;
    scl(a);
    seg.update_range(1, a, 1, N, i, i);
  }

  for (lld i = 0; i < M + K; i ++) {
    lld a, b, c;
    lld d;
    scl(a);
    if (a == 1) {
      scl(b), scl(c), scl(d);
      seg.update_range(1, d, 1, N, b, c);
    } else {
      scl(b), scl(c);
      printf("%lld\n", seg.query(1, 1, N, b, c));
    }
  }
  return 0;
}

Segment Tree - Coloring Version

#include <iostream>
#include <vector>
#include <algorithm>
#define SIZE 200000
#define sci(N) scanf("%d", &(N))
using namespace std;
using lld = long long;
struct rect {
    int x1, x2, y1, y2;
}data[SIZE], data2[SIZE];

vector<int> cx;
vector<int> cy;
struct seg_tree {
  struct node_t {
    lld sum, cnt;
  } node[SIZE * 8];

  void update(int idx, int val, int s, int e, int l, int r) {
    if (s > r || e < l) return;
    if (l <= s && e <= r) {
      node[idx].cnt += val;
      if (node[idx].cnt > 0) node[idx].sum = (cy[e + 1] - cy[s]);
        else {
          if (idx >= SIZE * 4) node[idx].sum = 0;
          else node[idx].sum = node[idx * 2].sum + node[idx * 2 + 1].sum;
        }
      } else {
        update(idx * 2, val, s, (s + e)/ 2, l, r);
        update(idx * 2 + 1, val, (s + e)/ 2 + 1, e, l, r);
        if (idx >= SIZE * 4) node[idx].sum = 0;
        else node[idx].sum = node[idx * 2].sum + node[idx * 2 + 1].sum;

        if (node[idx].cnt > 0) node[idx].sum = (cy[e + 1] - cy[s]);
      }
    }

  lld value() {
    return node[1].sum;
  }
} seg;
int N;
int main () {
  sci(N);
  cx.resize(2 * N);
  cy.resize(2 * N);
  for (int i = 0; i < N; ++ i) {
    sci(data[i].x1), sci(data[i].x2), sci(data[i].y1), sci(data[i].y2);
    cx[i * 2] = data[i].x1;
    cx[i * 2 + 1] = data[i].x2;
    cy[i * 2] = data[i].y1;
    cy[i * 2 + 1] = data[i].y2;
    data2[i].x1 = data[i].x1;
    data2[i].x2 = data[i].x2;
    data2[i].y1 = data[i].y1;
    data2[i].y2 = data[i].y2;
  }
  sort(cy.begin(), cy.end());
  cy.erase(unique(cy.begin(), cy.end()), cy.end());
  sort(cx.begin(), cx.end());
  cx.erase(unique(cx.begin(), cx.end()), cx.end());
  sort(data, data + N, [](const rect& a, const rect& b)-> bool {
    return a.x1 == b.x1 ? a.x2 < b.x2 : a.x1 < b.x1;
  });
  sort(data2, data2 + N, [](const rect& a, const rect& b)-> bool {
    return a.x2 == b.x2 ? a.x1 < b.x1 : a.x2 < b.x2;
  });

  int acur = 0, scur = 0;
  int length = cx.size();
  int maxCuttingLine = cy.size();
  lld result = 0;
  for (int i = 0; i < length - 1; ++ i) {
    while (acur < N && data[acur].x1 <= cx[i]) {
      int cy1 = distance(cy.begin(), lower_bound(cy.begin(), cy.end(), data[acur].y1));
      int cy2 = distance(cy.begin(), lower_bound(cy.begin(), cy.end(), data[acur].y2));
      seg.update(1, 1, 0, maxCuttingLine - 1, cy1, cy2-1);
      acur ++;
    }

    while (scur < N && data2[scur].x2 <= cx[i]) {
      int cy1 = distance(cy.begin(), lower_bound(cy.begin(), cy.end(), data2[scur].y1));
      int cy2 = distance(cy.begin(), lower_bound(cy.begin(), cy.end(), data2[scur].y2));
      seg.update(1, -1, 0, maxCuttingLine - 1, cy1, cy2-1);
      scur ++;
    }
    result += seg.value() * (cx[i + 1] - cx[i]);
  }
  printf("%lld", result);
  return 0;
}

KMP

#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
#define SIZE 1000010

char T[SIZE], P[SIZE];
void kmp(char* h, int n, char* p, int* pi, int* retval) {
    int matched = 0;
    for(int i = 0; i < n; i ++) {
        while (matched > 0 && h[i] != p[matched])
            matched = pi[matched - 1];
        if (h[i] == p[matched]) {
            matched ++;
            retval[i] = matched;
        }
    }
}
int PI[SIZE], result[SIZE];
int main () {
    cin.getline(T, SIZE);
    cin.getline(P, SIZE);
    int n = strlen(T), m = strlen(P);

    kmp(P+1, m - 1, P, PI, PI + 1);
    kmp(T, n, P, PI, result);
    int c = 0;
    for (int i = 0; i < n; i ++)
        if (result[i] == m) c++;
    printf("%d\n", c);
    for (int i = 0; i < n; i ++) {
        if (result[i] == m) printf("%d ", i - m + 2);
    }

    return 0;
}

Suffix Array

#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

void suffix(const char* s, int *sa) {
  int n = strlen (s);
  vector<int> g(n+1), nextg(n+1);
  int base;

  auto cmp = [&s, &n, &g, &base](const int& a, const int& b) -> bool {
    return g[a] == g[b] ? g[a + base] < g[b + base] : g[a] < g[b];
  };

  for (int i = 0; i < n; i ++) {
    sa[i] = i;
    g[i] = s[i] - 'A';
  }

  g[n] = -1;
  for (base = 1; base <= n; base <<= 1) {
    sort (sa, sa + n, cmp);

    nextg[sa[0]] = 0;
    for (int i = 1; i < n; i ++) {
      if (cmp(sa[i - 1], sa[i])) {
        nextg[sa[i]] = nextg[sa[i-1]] + 1;
      } else {
        nextg[sa[i]] = nextg[sa[i-1]];
      }
    }

    for (int i = 0 ;i < n; i ++) {
      g[i] = nextg[i];
    }
  }
}

void lcp(const char* s, const int *sa, int* l) {
  int n = strlen(s);
  vector<int> r(n + 1);
  for (int i = 0; i < n; i ++)
    r[sa[i]] = i;

  for (int i = 0, k = 0; i < n; i++, k = max(k - 1, 0)) {
    if (r[i] == n -1) continue;
    for (int j = sa[r[i] + 1]; s[i + k] == s[j + k]; k ++)
      ;
    l[r[i]] = k;
  }
}

#define SIZE 1000010

char S[SIZE];
int SA[SIZE], L[SIZE];

int main () {
  scanf(" %s", S);
  int n = strlen(S);
  suffix(S, SA);
  lcp(S, SA, L);
  for (int i = 0; i < n; i ++) {
    printf("%d ", SA[i] + 1);
  }
  printf("\nx ");
  for (int i = 0; i < n -1; i ++) {
    printf("%d ", L[i]);
  }
  return 0;
}

FFT

#include <bits/stdc++.h>
#define sci(n) scanf("%d", &(n))
#define scl(n) scanf("%lld", &(n))
#define pri(n) printf("%d ", (n))
#define prl(n) printf("%lld ", (n))
using namespace std;
using lld = long long;
using pii = pair<lld, lld>;
using vi = vector<lld>;
using vvi = vector<vi>;
using vpii = vector<pii>;
using base = complex<double>;

/* Fast Fourier transform */
void fft(vector<base> &a, bool invert) {
    int n = a.size();
    for (int i = 1, j = 0; i < n; ++ i) {
        int bit = n >> 1;
        for (; j >= bit; bit >>= 1) j -= bit;
        j += bit;
        if (i < j) swap(a[i], a[j]);
    }
    for (int len = 2; len <= n; len <<= 1){
        double ang = 2 * M_PI / len * (invert ? -1 : 1);
        base wlen(cos(ang), sin(ang));
        for (int i = 0; i < n; i += len){
            base w(1);
            for (int j = 0; j < len / 2; ++ j){
                base u = a[i + j], v = a[i + j + len / 2] * w;
                a[i + j] = u + v;
                a[i + j + len / 2] = u - v;
                w *= wlen;
            }
        }
    }
    if (invert) {
        for (int i = 0; i < n; ++ i) a[i] /= n;
    }
}
/* Fast Multiply Using FFT */
void multiply(const vector<int> &a, const vector<int> &b, vector<int> &res) {
    vector<base> fa(a.begin(), a.end()), fb(b.begin(), b.end());
    int n = 1;
    while (n < max(a.size(),b.size()))
      n <<= 1;
    n <<= 1;
    fa.resize(n);
    fb.resize(n);
    fft(fa, false);
    fft(fb, false);
    for (int i = 0; i < n; ++ i)
      fa[i] *= fb[i];
    fft(fa,true);
    res.resize(n);
    for (int i = 0; i < n; ++ i)
      res[i] = int(fa[i].real() + (fa[i].real() > 0 ? 0.5 :-0.5));
}

LCA (Untested)

#include <iostream>
#include <algorithm>
using namespace std;
#define SIZE (1 << 17)

int depth[SIZE];
int parent[17][SIZE];

int lca(int s, int e){
  if(depth[s] > depth[e]) swap(s, e);
  int dx = depth[e] - depth[s];
  for(int i = 0; i < 17; i++){
    if((dx >> i) & 1) e = parent[i][e];
  }
  for(int i = 16; i >= 0; i--){
    if(parent[i][s] != parent[i][e]){
      s = parent[i][s];
      e = parent[i][e];
    }
  }
  if(s == e) return s;
  return parent[0][s];
}

/* Must fill parent[0][idx] before use*/
void lca_build(int N) {
  for(int i = 1; i <= 16; i ++){
    for(int j = 1; j<= N; j ++){
      parent[i][j] = parent[i-1][parent[i-1][j]];
    }
  }
}

Network Flow

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#define sci(N) scanf("%d", &(N))
#define INF 987654321
using namespace std;
class NetworkFlow {
  public:
    struct edge {
      int dest, invi, fl;
    };
    int vn;
    vector<int> lv, work;
    vector<vector<edge>> edges;

  public:
    NetworkFlow (int n)
      : vn(n), lv(vn), edges(vn), work(vn)
    {}

    void addEdge(int s, int d, int f) {
      edge x{ d, (int)edges[d].size(), f};
      edge y{ s, (int)edges[s].size(), 0};

      edges[s].push_back(x);
      edges[d].push_back(y);
    }

    bool bfsv(int s, int d)
    {
      int i;
      int nv, nlv;

      fill(lv.begin(), lv.end(), 0);

      lv[s] = 1;
      queue<int> q;
      q.push(s);

      while (!q.empty()){
        nv = q.front();
        q.pop();
        nlv = lv[nv];

        for (const auto& e: edges[nv]) {
          if (e.fl > 0 && lv[e.dest] == 0) {
            lv[e.dest] = nlv + 1;
            q.push(e.dest);
            if (e.dest == d) return true;
          }
        }
      }
      return false;
    }

    int flowing(int s, int d, int f) {
      if (s == d) return f;
      int nf;

      for (int &i = work[s]; i < edges[s].size(); ++ i) {
        auto& e = edges[s][i];
        if (e.fl > 0 && lv[e.dest] == lv[s] +1) {
          nf = flowing(e.dest, d, min(f, e.fl));
          if (nf > 0) {
            edge &ei = edges[e.dest][e.invi];
            e.fl -= nf;
            ei.fl += nf;
            return nf;
          }
        }
      }
      return 0;
    }

    int solve (int s, int d) {
      int res = 0;
      int nres;
      while (bfsv(s, d)) {
        fill(work.begin(), work.end(), 0);
        while (true) {
          nres = flowing(s, d, INF);
          if (nres == 0) break;
          res += nres;
        }
      }

      return res;
    }
};

MCMF

#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>

#define INF 987654321
using namespace std;
struct MCMF {
  struct edge_t {
    int t, inv, fl, dist;
  };

  int vn;
  vector<int> lv, work, dst, h;
  vector<bool> inq, chk;
  vector<vector<edge_t>> edges;

  MCMF(int n): vn(n), edges(vn), work(vn), dst(vn), inq(vn), chk(vn), h(vn) {}

  void addEdge(int s, int d, int dist, int c) {
    edge_t x { d, (int) edges[d].size(), c, dist};
    edge_t y { s, (int) edges[s].size(), 0, -dist};

    edges[s].push_back(x);
    edges[d].push_back(y);
  }

  void init(int s, int d) {
    dst.assign(vn, INF);
    h.assign(vn, INF);
    queue<int> q;
    q.push(s);

    while (!q.empty()) {
      int f = q.front();
      q.pop();
      inq[f] = false;

      for (const edge_t& e: edges[f]) {
        if (e.fl && h[e.t] > h[f] + e.dist) {
          h[e.t] = h[f] + e.dist;
          if (!inq[e.t]) {
            inq[e.t] = true;
            q.push(e.t);
          }
        }
      }
    }

    for (int i = 0; i < vn; ++ i) {
      for (auto& e: edges[i]) {
        if (e.fl) e.dist += h[i] - h[e.t];
      }
    }
    priority_queue<pair<int, int>> pq;
    pq.push({0, s});
    dst[s] = 0;

    while (!pq.empty()) {
      pair<int, int> f = pq.top();
      pq.pop();
      int cost = - f.first;
      int cur = f.second;

      if (dst[cur] - cost) continue;
      for (const auto& e: edges[cur]) {
        if (e.fl && dst[e.t] > dst[cur] + e.dist) {
          dst[e.t] = dst[cur] + e.dist;
          pq.push({- dst[e.t], e.t});
        }
      }
    }

    for (int i = 0; i < vn; ++ i) {
      dst[i] += h[d] - h[s];
    }
  }

  bool update() {
    int min_d = INF;
    for (int i = 0; i < vn; ++ i) {
      if (!chk[i]) continue;
      for (const edge_t& e : edges[i]) {
        if (e.fl && !chk[e.t])
          min_d = min(min_d, dst[i] + e.dist - dst[e.t]);
      }
    }
    if (min_d >= INF) return false;
    for (int i = 0; i < vn; ++ i) {
      if (!chk[i]) dst[i] += min_d;
    }
    return true;
  }

  int flowing(int s, int d, int fl) {
    chk[s] = true;
    if (s == d) return fl;

    int nf;
    for (int &i = work[s]; i < edges[s].size(); ++ i) {
      edge_t& e = edges[s][i];
      if (!chk[e.t] && dst[e.t] == dst[s] + e.dist && e.fl) {
        int ret = flowing(e.t, d, min(fl, e.fl));
        if (ret) {
          e.fl -= ret;
          edges[e.t][e.inv].fl += ret;
          return ret;
        }
      }
    }

    return 0;
  }

  pair<int, int> solve(int s, int e) {
    int cost = 0, dist = 0;
    init(s, e);
    do {
      int tmp;
      work.assign(vn, 0);
      chk.assign(vn, false);
      while (true) {
        tmp = flowing(s, e, INF);
        if (tmp == 0) break;

        cost += dst[e] * tmp;
        dist += tmp;
        chk.assign(vn, false);
      }
    } while (update());
    return {cost, dist};
  }
};

소수 판별(Miller-Rabin)

Aho-Corasick

#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <cstring>
#define FASTIO() do{cin.tie(0);cout.tie(0); ios_base::sync_with_stdio(false);}while(0)

using namespace std;

struct AhoCorasick {
  struct node_t {
    int fail;
    int end;
    map<char, int> children;
    char debug;
    node_t() : fail(0), end(0) {};
  };

  vector<node_t> nodes;
  AhoCorasick() :nodes(1){ }

  void appendToTrie(const char* data) {
    int cur = 0;
    int length = strlen(data);
    for (int i = 0; i < length; ++ i) {
      auto it = nodes[cur].children.find(data[i]);
      if (it != nodes[cur].children.end()) {
        cur = nodes[cur].children[data[i]];
      } else {
        nodes[cur].children[data[i]] = nodes.size();
        cur = nodes.size();
        node_t t;
        t.debug = data[i];
        nodes.push_back(t);
      }
    }
    nodes[cur].end ++;
  }

  void appendToTrie(string data) {
    appendToTrie(data.c_str());
  }

  void build() {
    queue<int> q;
    q.push(0);

    while (!q.empty()) {
      int f = q.front(); q.pop();
      node_t& parent = nodes[f];
      for (const auto& e: parent.children) {
        node_t& child = nodes[e.second];
        int fail = parent.fail;

        q.push(e.second);
        if (f == 0) continue;
        while (fail != 0 && nodes[fail].children.find(e.first) == nodes[fail].children.end())
          fail = nodes[fail].fail;
        if (nodes[fail].children.find(e.first) != nodes[fail].children.end())
          fail = nodes[fail].children[e.first];
        child.fail = fail;

         if (nodes[child.fail].end > 0)
         child.end ++;
      }
    }
  }

  bool contain(const char* heystack) {
    int matched = 0, leng = strlen(heystack);
    for (int i = 0; i < leng; ++ i) {
      while (matched > 0 && nodes[matched].children.find(heystack[i]) == nodes[matched].children.end())
        matched = nodes[matched].fail;
      if (nodes[matched].children.find(heystack[i]) != nodes[matched].children.end()) {
        matched = nodes[matched].children[heystack[i]];
        if (nodes[matched].end > 0)
          return true;
      }
    }
    return false;
  }

  void printTrie() {
    printTrie(nodes[0], 1);
  }
  void printTrie(const node_t& node, int depth) {
    for (const auto& e : node.children) {
      cout << e.second;
      for (int i = 0 ; i < depth; ++ i) cout << "-";
      cout << e.first <<  "\t\t fail : " << nodes[e.second].fail << "end : " << nodes[e.second].end << "\n";
      printTrie(nodes[e.second], depth + 1);
    } } void printNodes() {
      for (int i = 0; i < nodes.size(); ++ i) {
        cout << i << " - " << nodes[i].debug << "\n";
        for (const auto& e : nodes[i].children) {
          cout << "-----"<< e.first << ", " << e.second << "\n";
        }
      }
    }
};

int N, M;
string data;
int main () {
  FASTIO();
  AhoCorasick ah;
  cin >> N;
  for (int i = 0; i < N; ++ i) {
    cin >> data;
    ah.appendToTrie(data);
  }
  ah.build();
  //ah.printTrie();
  ///ah.printNodes();
  cin >> M;
  for (int i = 0; i < M; ++ i) {
    cin >> data;
    cout << (ah.contain(data.c_str()) ? "YES\n" : "NO\n");
  }

  return 0;
}

Offline Query (Mo’s Algorithm)

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
#define SIZE 100100
#define NUM_SIZE 1000100
int N, M, sn;
using lld = long long;
lld data[SIZE];
lld cnt[NUM_SIZE];
struct query_t {
  int s, e, i;
} query[SIZE];
lld result[SIZE];
int main () {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> N;
  cin >> M;
  for (int i = 0; i < N; ++ i)
    cin >> data[i];
  for (int i = 0; i < M; ++ i) {
    cin >> query[i].s >> query[i].e;
    query[i].s --;
    query[i].e --;
    query[i].i = i;
  }
  sn = sqrt(N);
  sort(query, query + M, [](const query_t& a, const query_t& b) -> bool {
    return a.s / sn == b.s / sn ? a.e  < b.e : a.s / sn < b.s / sn;
  });

  int scur = 1, ecur = 0;
  lld cur_result = 0;
  for (int i = 0; i < M; ++ i) {
    int s = query[i].s, e = query[i].e;
    while (scur > s) {
      int idx = data[-- scur];
      cur_result -= cnt[idx] * cnt[idx] * idx;
      cnt[idx] ++;
      cur_result += cnt[idx] * cnt[idx] * idx;
    }
    while (scur < s) {
      int idx = data[scur ++];
      cur_result -= cnt[idx] * cnt[idx] * idx;
      cnt[idx] --;
      cur_result += cnt[idx] * cnt[idx] * idx;
    }

    while (ecur < e) {
      int idx = data[++ ecur];
      cur_result -= cnt[idx] * cnt[idx] * idx;
      cnt[idx] ++;
      cur_result += cnt[idx] * cnt[idx] * idx;
    }

    while (ecur > e) {
      int idx = data[ecur --];
      cur_result -= cnt[idx] * cnt[idx] * idx;
      cnt[idx] --;
      cur_result += cnt[idx] * cnt[idx] * idx;
    }
    result[query[i].i] = cur_result;
  }
  for (int i = 0; i < M; ++ i) {
    cout << result[i] << "\n";
  }
  return 0;
}

Sliding Window DP

#include <iostream>
#include <deque>
#define SIZE 100100
#define FASTIO() do{ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); } while(0)
using namespace std;
using lld = long long;
deque<pair<lld, int>> dq;
lld dp[SIZE];
lld N, D;
lld data[SIZE];
int main () {
  FASTIO();
  cin >> N >> D;
  for (int i = 0; i < N; ++ i)
    cin >> data[i];

  lld result = data[0];
  for (int i = 1; i < N; ++ i) {
    result = max(data[i], result);
  }
  dp[0] = data[0];
  dq.push_back({dp[0], 0});
  for (int i = 1; i < N; ++ i) {
    while (!dq.empty() && dq.front().second < i - D) dq.pop_front();
    while (!dq.empty() && dq.back().first < dp[i - 1]) dq.pop_back();
    dq.push_back({dp[i - 1], i - 1});
    //cout << i << " : " << dq.front().first << "\n";
    dp[i] = max(0LL, dq.front().first) + data[i];
  }
  for (int i = 0; i < N; ++ i) {
    result = max(dp[i], result);
  }
  cout << result;
  return 0;
}

ConvexhullTrick DP (cht)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define FASTIO() do{ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); } while(0)
#define SIZE 100100
using lld = long long;
using pii = pair<lld, lld>;
int N;
lld A[SIZE], B[SIZE];
lld dp[SIZE];
struct line_t {
  lld a, b;
};
// dp[i] = min_{1 <= j < i} (a[i]b[j] + dp[j])

struct cht_t {
  int s = 0, e = 0;
  int idx[SIZE];
  line_t deq[SIZE];

  double cross(int a, int b) {
    return 1.0 * (deq[a].b - deq[b].b) / (deq[b].a - deq[a].a);
  }

  void insert(line_t v, int line_idx) {
    deq[e] = v;
    idx[e] = line_idx;
    while (s + 1 < e && cross(e - 2, e- 1) > cross (e - 1, e)) {
      deq[e - 1] = deq[e];
      idx[e - 1] = idx[e];
      e --;
    }
    e ++;
  }

  pii query(lld x) {
    int l = 0, r = e - 1;
    while (l < r) {
      int m = (l + r) / 2;
      if (cross(m, m + 1) <= x) l = m + 1;
      else r = m;
    }
    return {deq[l].a * x + deq[l].b, idx[l]};
  }
} cht;

int main () {
  FASTIO();
  cin >> N;
  for (int i = 0; i < N; ++ i) cin >> A[i];
  for (int i = 0; i < N; ++ i) cin >> B[i];

  dp[0] = 0;
  cht.insert(line_t{B[0], dp[0]}, 0);
  for (int i = 1; i < N; ++ i) {
    dp[i] = cht.query(A[i]).first;
    cht.insert(line_t{B[i], dp[i]}, i);
  }
  cout << dp[N - 1];
  return 0;
}

기하 알고리즘

교차점 검출

#include <iostream>
#include <vector>
#define FASTIO() do{cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);}while(0)
using lld = long long;
using namespace std;
struct vector_t : public pair<lld, lld> {
    lld& x;
    lld& y;
    vector_t() : x{first}, y{second}
    {};
    vector_t(lld f, lld s) : x{first}, y{second}
    {first = f; second = s;};
};

lld crossValue(const vector_t& a, const vector_t& b) {
  return a.x * b.y - b.x * a.y;
}

lld ccw(const vector_t& p1, const vector_t& p2, const vector_t& p3) {
  lld cv = crossValue({p1.x - p2.x, p1.y - p2.y}, {p3.x - p2.x, p3.y - p2.y});
  if (cv > 0) return 1;
  if (cv == 0) return 0;
  return -1;
}

bool isIntersect(vector_t p1, vector_t p2,
    vector_t q1, vector_t q2) {
    lld pq = ccw (p1, p2, q1) * ccw(p1, p2, q2);
    lld qp = ccw (q1, q2, p1) * ccw(q1, q2, p2);
    if (p1 > p2) swap(p1, p2);
    if (q1 > q2) swap(q1, q2);
    return (!pq && !qp) ? q1 <= p2 && p1 <= q2 : pq <= 0 && qp <= 0;
}
int main () {
  vector_t p1, p2, p3, p4;
  cin >> p1.x >> p1.y; cin >> p2.x >> p2.y;
  cin >> p3.x >> p3.y; cin >> p4.x >> p4.y;
  if (p1 > p2) swap(p1, p2);
  if (p3 > p4) swap(p3, p4);
  if (isIntersect(p1, p2, p3, p4)) {
    cout << "1\n";
    if (p1 == p3 && p2 != p4) {
      cout << p1.x << " " << p1.y;
    } else if (p1 == p4) {
      cout << p1.x << " " << p1.y;
    } else if (p2 == p3) {
      cout << p2.x << " " << p2.y;
    } else {
      lld A = (p2.y - p1.y);
      lld B = (p1.x - p2.x);
      lld E = (A * p1.x) + (B * p1.y);
      lld C = (p4.y - p3.y);
      lld D = (p3.x - p4.x);
      lld F = (C * p3.x) + (D * p3.y);
      lld DE = A * D - B * C;
      //printf("%lld %lld %lld %lld %lld %lld \n", A, B, C, D, E, F);
      if (DE == 0) return 0;
      long double x = (long double)((E * D) - (B * F)) / DE;
      long double y = (long double)((A * F) - (E * C)) / DE;
      cout.precision(20);
      cout << x << " " << y;
    }
  } else {
    cout << "0";
  }
  return 0;
}