CSP认证模板

切换中英文键修改,语法风格vs,主题vs,输入内容才能保存生效,关闭保存自动格式化,改格式化快捷键shift+alt+f

编译选项

 -Wall -Wextra  -DLOCAL -std=c++17 -g -O2 
#include <bits/stdc++.h>
using i128 = __int128;
using namespace std;
#ifdef LOCAL
#define deb(x) cerr<<"L"<<__LINE__<<": "<<#x<<" = "<<(x)<<endl
#else
#define deb(x)
#endif
#define ll long long
//#define int long long
#define ull unsigned long long
#define pii pair<int, int>
#define db double
#define baoliu(x, y) cout << fixed << setprecision(y) << x
#define endl "\n"
#define alls(x) (x).begin(), (x).end()
#define fs first
#define sec second
#define bug(x) cerr << #x << " = " << x << endl
const int N = 2e5 + 10;
const int M = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-8;
const double PI = acos(-1.0);

void solve() {
    int n;
    cin >> n;
    vector<int>a(n + 1);
    deb(n);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++)
        deb(a[i]);
    int ans = accumulate(alls(a), 0);
    cerr << ans << endl;
    deb(ans);
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
#ifdef LOCAL
    double sttime = clock();
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
     int t = 1;
    //cin >> t;
    while (t--) solve();
#ifdef LOCAL
    double edtime = clock();
    cerr << "time: " << (double)(edtime - sttime) / CLOCKS_PER_SEC * 1000 << endl;
#endif
    return 0;
}

stringstream的基本用法

#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string input = "123 456 789";
    std::stringstream ss(input);

    int a, b, c;
    ss >> a >> b >> c; // 从字符串流中读取整数

    std::cout << "a: " << a << ", b: " << b << ", c: " << c << std::endl; // 输出: a: 123, b: 456, c: 789

    return 0;
}
#include <iostream>
#include <sstream>
#include <string>

int main() {
    // 整数转字符串
    int num = 42;
    std::stringstream ss;
    ss << num; // 将整数写入字符串流
    std::string str = ss.str(); // 获取字符串
    std::cout << "Integer to string: " << str << std::endl;

    // 字符串转整数
    std::string input = "12345";
    std::stringstream ss2(input);
    int convertedNum;
    ss2 >> convertedNum; // 从字符串流中读取整数
    std::cout << "String to integer: " << convertedNum << std::endl;

    return 0;
}

minp

vector<int> primes, minp;
void sieve(int n) {
    minp.assign(n + 1, 0);
    primes.clear();

    for (int i = 2; i <= n; i++) {
        if (minp[i] == 0) {
            minp[i] = i;
            primes.push_back(i);
        }

        for (auto p : primes) {
            if (i * p > n) {
                break;
            }
            minp[i * p] = p;
            if (p == minp[i]) {
                break;
            }
        }
    }
}
vector<pii> factor(int x) {
    assert(x != 0);
    vector<pii> t;
    while (x != 1) {
        int u = minp[x], v = 0;
        while (x % u == 0) {
            x /= u;
            v++;
        }
        if (v)
            t.push_back({u, v});
    }
    return t;
}

scc

struct SCC {
  // scc 数组逆序满足拓扑序
  int n;
  vector<vector<int>> &adj;
  vector<vector<int>> scc;
  vector<int> stk;
  vector<int> dfn, low, bel;
  int cur, cnt;

  SCC(int n_, auto &adj_) : adj(adj_), n(n_) { init(n); }

  void init(int n) {
    this->n = n;
    dfn.assign(n + 1, 0);
    low.resize(n + 1);
    bel.assign(n + 1, -1);
    stk.clear();
    cur = 0;
    work();
  }

  void dfs(int x) {
    dfn[x] = low[x] = ++cur;
    stk.push_back(x);

    for (auto y : adj[x]) {
      if (dfn[y] == -1) {
        dfs(y);
        low[x] = min(low[x], low[y]);
      } else if (bel[y] == -1) {
        low[x] = min(low[x], dfn[y]);
      }
    }

    if (dfn[x] == low[x]) {
      int cnt = scc.size();
      scc.push_back({});
      int y;
      do {
        y = stk.back();
        bel[y] = cnt;
        stk.pop_back();
        scc.back().push_back(y);
      } while (y != x);
    }
  }

  void work() {
    for (int i = 1; i <= n; i++) {
      if (dfn[i] == 0) {
        dfs(i);
      }
    }
  }
};