malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int inf = 1 << 30;   const long long maxn = 110;   struct node {     int v, cost;   } a[1500];   int N, K, dp1[100 * 150], dp2[100 * 150];   int solve() {     memset(dp1, -0x3f3f3f3f, sizeof(dp1));     ;     memset(dp2, -0x3f3f3f3f, sizeof(dp2));     ;     dp1[1] = dp2[1] = 0;     for (int i = 1; i <= N; i++) {       if (a[i].cost >= 0) {         for (int j = 100 * 105; j >= a[i].cost; j--)           dp1[j] = max(dp1[j], dp1[j - a[i].cost] + a[i].v);       } else {         a[i].cost = -a[i].cost;         for (int j = 100 * 105; j >= a[i].cost; j--)           dp2[j] = max(dp2[j], dp2[j - a[i].cost] + a[i].v);       }     }     int ans = -1;     for (int i = 0; i <= 100 * 105; i++) {       if (dp1[i] + dp2[i] != 0 && dp1[i] + dp2[i] > ans) ans = dp1[i] + dp2[i];     }     return ans;   }   int main() {     cin >> N >> K;     for (int i = 1; i <= N; i++) cin >> a[i].v;     int b;     for (int i = 1; i <= N; i++) cin >> b, a[i].cost = a[i].v - K * b;     cout << solve() << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   const int N = 1e6 + 5;   const int inf = 1 << 30;   const long long llf = 9e18;   long long powmod(long long a, long long b) {     long long res = 1;     a %= mod;     assert(b >= 0);     for (; b; b >>= 1) {       if (b & 1) res = res * a % mod;       a = a * a % mod;     }     return res;   }   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   struct node {     int x, y, time;     friend bool operator<(const node &a, const node &b) {       return a.time > b.time;     }   };   bool cmp(int x, int y) { return x < y; }   int pic[1010][1010];   int book[1010][1010];   int dx[4] = {0, 0, 1, -1};   int dy[4] = {1, -1, 0, 0};   int n, m, t;   bool check(int x, int y) {     bool f1 = false;     bool f2 = false;     for (register int i = 0; i <= 3; ++i) {       if (x + dx[i] < 0 || x + dx[i] > n - 1 || y + dy[i] < 0 ||           y + dy[i] > m - 1)         continue;       if (pic[x + dx[i]][y + dy[i]] != pic[x][y]) {         f2 = true;       } else {         f1 = true;         book[x][y] = 0;       }     }     if (f1 && !f2) return false;     if (f1 && f2) return true;     if (!f1 && f2) return false;   }   int main() {     cin >> n >> m >> t;     string s;     for (register int i = 0; i <= n - 1; ++i) {       cin >> s;       for (register int j = 0; j <= m - 1; ++j) {         pic[i][j] = s[j] -  0 ;       }     }     node now, re;     priority_queue<node> q;     for (register int i = 0; i <= n - 1; ++i) {       for (register int j = 0; j <= m - 1; ++j) {         book[i][j] = inf;         if (check(i, j)) {           now.x = i;           now.y = j;           now.time = 0;           q.push(now);         }       }     }     while (!q.empty()) {       re = q.top();       q.pop();       int x = re.x, y = re.y;       for (register int i = 0; i <= 3; ++i) {         if (x + dx[i] < 0 || x + dx[i] > n - 1 || y + dy[i] < 0 ||             y + dy[i] > m - 1)           continue;         if (pic[x + dx[i]][y + dy[i]] != pic[x][y]) {           now.x = x + dx[i];           now.y = y + dy[i];           now.time = re.time + 1;           if (book[now.x][now.y] > now.time) {             book[now.x][now.y] = now.time;             q.push(now);           }         }       }     }     long long x, y, p;     for (register int i = 1; i <= t; ++i) {       cin >> x >> y >> p;       x--;       y--;       if (p < book[x][y] || book[x][y] == inf)         cout << pic[x][y] << endl;       else         cout << (pic[x][y] + (p - 1ll * book[x][y]) % 2) % 2 << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   long double r, v;   long double eps = 1e-8;   const long double mpi = acos(-1.0);   inline long double dist(long double theta) {     return 2.0L * r * (sin(theta) + theta);   }   inline double ti(long double theta, long long ni) {     return 2.0L * r * (mpi * ni + theta) / v;   }   void solve() {     int si, fi;     scanf( %d%d , &si, &fi);     long double d = fi - si;     long long ni = d / (2.0L * mpi * r);     long double left = 0;     long double right = mpi;     d -= 2.0L * r * mpi * ni;     long double dleft = dist(left);     long double dright = dist(right);     long double e = eps * v;     while (dright - dleft > e) {       long double mid = (right + left) / 2.0L;       long double dmid = dist(mid);       if (dmid > d) {         right = mid;         dright = dmid;       } else {         left = mid;         dleft = dmid;       }     }     double res = ti(right, ni);     printf( %.10f n , res);     return;   }   int main() {     int n, ri, vi;     scanf( %d%d%d , &n, &ri, &vi);     r = ri;     v = vi;     while (n--) {       solve();     }     cout.flush();     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   template <class T>   ostream& operator<<(ostream& os, vector<T> V) {     os <<  [  ;     for (auto v : V) os << v <<    ;     return os <<  ] ;   }   template <class L, class R>   ostream& operator<<(ostream& os, pair<L, R> P) {     return os <<  (  << P.first <<  ,  << P.second <<  ) ;   }   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cerr << name <<   :   << arg1 << endl;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   const long long INF = 0x3f3f3f3f;   const long long M = 1e9 + 7;   const long long N = 1e5 + 5;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cout << fixed << setprecision(15);     long long n, k;     cin >> n >> k;     long long l[n], r[n];     for (long long i = 0; i < n; ++i) cin >> l[i] >> r[i];     vector<long long> v;     for (long long i = 0; i < n; ++i) v.push_back(l[i]), v.push_back(r[i]);     sort((v).begin(), (v).end());     map<long long, long long> ml, mr;     for (long long i = 0; i < 2 * n; ++i) mr[v[i]] = i;     for (long long i = 0; i < 2 * n; ++i) ml[v[2 * n - 1 - i]] = 2 * n - 1 - i;     long long a[2 * n];     memset(a, 0, sizeof(a));     for (long long i = 0; i < n; ++i) a[ml[l[i]]]++, a[mr[r[i]]]--;     partial_sum(a, a + 2 * n, a);     vector<long long> w;     for (long long i = 0; i < 2 * n; ++i) {       if (w.size() % 2 == 0) {         if (a[i] >= k) {           w.push_back(v[i]);         }       } else {         if (a[i] < k) {           w.push_back(v[i]);         }       }     }     cout << w.size() / 2 <<   n ;     for (long long i = 0; i < w.size(); i += 2) {       cout << w[i] <<     << w[i + 1] <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   struct less_key {     bool operator()(pair<int64_t, int64_t> p1, pair<int64_t, int64_t> p2) {       return (p1.first > p2.first) ||              ((p1.first == p2.first) && (p1.second < p2.second));     }   };   struct pair_hash {     std::size_t operator()(const pair<int64_t, int64_t>& k) const {       return static_cast<size_t>(k.first ^ k.second);     }   };   const int64_t mod = 100000000007ll;   const int64_t inf = 10000000000000007ll;   int64_t n, m;   char a[10004][10004];   int64_t sx, sy;   int64_t tx, ty;   bool x1[10004], x2[10004];   bool _q1[10004], y2[10004];   bool check_x(int64_t x1, int64_t x2, int64_t y) {     bool f = 1;     int64_t q = x1 < x2 ? 1 : -1;     for (int64_t i = x1;; i += q) {       if (a[i][y] ==  * ) {         f = 0;         break;       }       if (i == x2) {         break;       }     }     return f;   }   bool check_y(int64_t _q1, int64_t y2, int64_t x) {     bool f = 1;     int64_t q = _q1 < y2 ? 1 : -1;     for (int64_t i = _q1;; i += q) {       if (a[x][i] ==  * ) {         f = 0;         break;       }       if (i == y2) {         break;       }     }     return f;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m;     for (int64_t i = 0; i < n; i++) {       for (int64_t j = 0; j < m; j++) {         cin >> a[i][j];         if (a[i][j] ==  S ) {           sx = i;           sy = j;         } else if (a[i][j] ==  T ) {           tx = i;           ty = j;         }       }     }     if (sx == tx) {       bool f = 1;       int64_t q = sy < ty ? 1 : -1;       for (int64_t i = sy; i != ty; i += q) {         if (a[sx][i] ==  * ) {           f = 0;           break;         }       }       if (f) {         cout <<  YES ;         return 0;       }     }     if (sy == ty) {       bool f = 1;       int64_t q = sx < tx ? 1 : -1;       for (int64_t i = sx; i != tx; i += q) {         if (a[i][sy] ==  * ) {           f = 0;           break;         }       }       if (f) {         cout <<  YES ;         return 0;       }     }     bool f = 0;     for (int64_t i = 0; i < n && !f; i++) {       if (check_x(i, sx, sy) && check_x(i, tx, ty) && check_y(sy, ty, i)) {         f = 1;       }     }     for (int64_t j = 0; j < m && !f; j++) {       if (check_y(j, sy, sx) && check_y(j, ty, tx) && check_x(sx, tx, j)) {         f = 1;         break;       }     }     cout << (f ?  YES  :  NO );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 10010;   char s[N];   char t[N];   int n, m;   int main() {     scanf( %s , s + 1);     n = strlen(s);     scanf( %d , &m);     while (m--) {       int l, r, k;       scanf( %d%d%d , &l, &r, &k);       k %= (r - l + 1);       for (int i = 0; i < k; i++) {         t[l + i] = s[r - k + 1 + i];       }       for (int i = k; i <= r - l; i++) t[l + i] = s[l - k + i];       for (int i = l; i <= r; i++) s[i] = t[i];     }     printf( %s n , s + 1);     return 0;   }
#include <bits/stdc++.h>   const int N = 200;   char a[N][N];   int main() {     int n, m, k;     std::cin >> n >> m >> k;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         std::cin >> a[i][j];       }     }     int ret = 0;     int replaces = 0;     for (int j = 0; j < m; j++) {       int past = 0;       int maxOnes = 0;       int curReplaces = 0;       for (int i = 0; i < n; i++) {         if (a[i][j] !=  1 ) continue;         int ones = 0;         for (int x = i; x < std::min(n, i + k); x++) {           if (a[x][j] ==  1 ) ones++;         }         if (ones > maxOnes) {           maxOnes = ones;           curReplaces = past;         }         past++;       }       ret += maxOnes;       replaces += curReplaces;     }     std::cout << ret <<     << replaces <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int qpow(int b, int e, int m = 998244353) {     int a = 1;     for (; e; e >>= 1, b = (long long)b * b % m)       if (e & 1) a = (long long)a * b % m;     return a;   }   struct node {     long long sum;     long long pre;     long long suf;     long long ans;   };   node segt[1000000];   node combine(node &a, node &b) {     node c;     c.sum = a.sum + b.sum;     c.pre = max(a.pre, a.sum + b.pre);     c.suf = max(b.suf, b.sum + a.suf);     c.ans = max(max(a.ans, b.ans), a.suf + b.pre);     return c;   }   void build(long long i, long long tl, long long tr, vector<long long> &d,              vector<long long> &h) {     if (tl == tr) {       node t;       t.ans = 0;       t.sum = d[tl];       t.pre = 2 * h[tl];       t.suf = 2 * h[tl] + d[tl];       segt[i] = t;       return;     }     long long tm = (tl + tr) / 2;     build(i * 2, tl, tm, d, h);     build(i * 2 + 1, tm + 1, tr, d, h);     node c = combine(segt[i * 2], segt[i * 2 + 1]);     segt[i] = c;   }   node getans(long long i, long long tl, long long tr, long long l, long long r) {     if (l > r) {       node t;       t.sum = t.pre = t.suf = t.ans = INT_MIN;       return t;     }     if (tl == l && tr == r) return segt[i];     long long tm = (tl + tr) / 2;     node a = getans(i * 2, tl, tm, l, min(tm, r));     node b = getans(i * 2 + 1, tm + 1, tr, max(tm + 1, l), r);     node c = combine(a, b);     ;     return c;   }   void pn(node a) {     cout <<  ans :   << a.ans <<   pre:   << a.pre <<   suf :  << a.suf          <<   sum :   << a.sum << endl;   }   void solve() {     long long n, m;     cin >> n >> m;     vector<long long> d(2 * n + 1);     vector<long long> h(2 * n + 1);     for (int i = 1; i <= n; i++) {       cin >> d[i];       d[i + n] = d[i];     }     for (int i = 1; i <= n; i++) {       cin >> h[i];       h[i + n] = h[i];     }     build(1, 1, 2 * n, d, h);     while (m--) {       long long a, b;       cin >> a >> b;       long long l, r;       l = b + 1;       if (a <= b) {         r = n + a - 1;       } else {         r = a - 1;       }       auto ans = getans(1, 1, 2 * n, l, r);       cout << ans.ans << endl;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t;     t = 1;     while (t--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[200002];   pair<int, int> b[200002];   int main() {     int n, k, max = 0;     for (int i = 0; i < 200001; i++) {       a[i] = 2147483647;     }     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d %d , &b[i].first, &b[i].second);     }     sort(b, b + n);     for (int i = 0; i < n; i++) {       int d = b[i].first - b[i].second;       int* c = upper_bound(a, a + max, d);       int index = c - a;       if (index + 1 > max) {         max = index + 1;       }       a[index] = min(a[index], b[i].first + b[i].second);     }     printf( %d n , max);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, k, p[200005];   int vis[200005], cntn;   int pre[200005], nxt[200005];   int flag = 0;   struct node {     int p, s;     node(int p = 0, int s = 0) : p(p), s(s) {}     bool operator<(const node &b) const { return p < b.p; }   };   void del(int x) {     nxt[pre[x]] = nxt[x];     pre[nxt[x]] = pre[x];     vis[x] = flag + 1;     cntn--;   }   priority_queue<node> q;   int main() {     cin >> n >> k;     cntn = n;     for (int i = 1; i <= n; i++) {       scanf( %d , &p[i]);       pre[i] = i - 1;       nxt[i] = i + 1;       q.push(node(p[i], i));     }     while (cntn) {       while (vis[q.top().s] != 0) q.pop();       node t = q.top();       q.pop();       del(t.s);       int cur = pre[t.s];       for (int i = 0; i < k && cur != 0; i++, cur = pre[cur]) del(cur);       cur = nxt[t.s];       for (int i = 0; i < k && cur != n + 1; i++, cur = nxt[cur]) del(cur);       flag = 1 - flag;     }     for (int i = 1; i <= n; i++) cout << vis[i];     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   int n, m, k;   int a[11][11];   int f[11][11];   int cnt[21];   long long read() {     long long x = 0, f = 1;     char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) f = -1;       c = getchar();     }     while (isdigit(c)) {       x = x * 10 + c -  0 ;       c = getchar();     }     return x * f;   }   void write(long long x) {     if (x < 0) putchar( - ), x = -x;     if (x >= 10) write(x / 10);     putchar(x % 10 +  0 );   }   void writern(long long x) { write(x), putchar(   ); }   void writeln(long long x) { write(x), puts(  ); }   long long dfs(int x, int y) {     if (y > m) {       y = 1, x++;     }     if (x > n) return 1;     long long res = 0;     int s = f[x - 1][y] | f[x][y - 1];     int cc = 0, flag = -1;     for (int i = s; i; i -= (i & -i)) cc++;     if (m + n - x - y + 1 > k - cc) return 0;     for (int i = s ^ ((1 << k) - 1); i; i -= (i & -i)) {       int t = i & (-i);       int tt = log2(t) + 1;       if (a[x][y] == 0 || a[x][y] == tt) {         f[x][y] = s | t;         cnt[tt]++;         if (cnt[tt] == 1) {           if (flag == -1) flag = dfs(x, y + 1);           res += flag;         } else if (cnt[tt])           res += dfs(x, y + 1);         res %= mod;         cnt[tt]--;       }     }     return res;   }   int main() {     n = read(), m = read();     k = read();     if (n + m - 1 > k) {       writeln(0);       return 0;     }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         a[i][j] = read();         if (a[i][j]) cnt[a[i][j]]++;       }     writeln(dfs(1, 1) % mod);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, s1 = 0, s2 = 0, s = 0;     cin >> n;     s1 = ((n + 1) * n) / 2, s2 = s1 / 2;     vector<long> a;     for (long i = n; i > 0 && s2 > 0; --i)       if (s2 >= i)         if (s2 - i < i && s2 - i != 0) {           a.push_back(i), a.push_back(s2 - i), s += s2;           break;         } else           a.push_back(i), s2 -= i, s += i;     cout << abs(s1 - s - s) <<   n  << a.size() <<    ;     for (long i = 0; i < a.size(); cout << a[i] <<    , ++i)       ;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> t;   int sz = 0;   const int INF = 2e9;   void build(vector<int> a) {     t.assign(2 * sz + 2, INF);     for (int i = 0; i < sz; i++) {       t[i + sz] = a[i];     }     for (int i = sz - 1; i > 0; i--) {       t[i] = min(t[2 * i], t[2 * i + 1]);     }   }   int get(int v, int tl, int tr, int l, int r) {     if (tl > r || l > tr) {       return INF;     }     if (tl == l && tr == r) {       return t[v];     }     int tm = (tl + tr) / 2;     return min(get(2 * v, tl, tm, l, min(r, tm)),                get(2 * v + 1, tm + 1, tr, max(l, tm + 1), r));   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     sz = 1;     while (sz < 4 * n) {       sz *= 2;     }     vector<int> a(sz);     vector<pair<int, int>> q;     vector<pair<int, int>> qq;     for (int i = 0; i < n; i++) {       int l, r;       cin >> l >> r;       q.emplace_back(l, 0);       qq.emplace_back(l, r);       q.emplace_back(r, 1);       q.emplace_back(l - 1, -1);       q.emplace_back(r + 1, -1);     }     sort(q.begin(), q.end());     int b = 0;     for (int i = 0; i < 4 * n; i++) {       if (q[i].second == 0) {         b++;         int id = lower_bound(q.begin(), q.end(), make_pair(q[i].first, -1)) -                  q.begin();         a[id] = max(a[id], b);       } else {         int id = lower_bound(q.begin(), q.end(), make_pair(q[i].first, -1)) -                  q.begin();         a[id] = max(a[id], b);         if (q[i].second == 1) b--;       }     }     for (int i = 0; i < a.size(); i++) {       if (a[i] == 0) {         a[i] = INF;       }     }     build(a);     for (int i = 0; i < n; i++) {       int minimum = get(           1, 0, sz - 1,           lower_bound(q.begin(), q.end(), make_pair(qq[i].first, -1)) - q.begin(),           lower_bound(q.begin(), q.end(), make_pair(qq[i].second, -1)) -               q.begin());       if (minimum > 1 && minimum != INF) {         cout << i + 1;         return 0;       }     }     cout << -1;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     scanf( %d %d , &n, &k);     vector<int> a(n);     for (int i = 0; i < n; ++i) scanf( %d , &a[i]);     int sum = 0;     for (int i = 0; i < n; ++i) sum += a[i];     if (sum % k) {       printf( No n );       return 0;     }     int req = sum / k;     int cur = 0;     int len = 0;     for (int i = 0; i < n; ++i) {       if (a[i] > req) {         printf( No n );         return 0;       }     }     vector<int> ans;     for (int i = 0; i < n; ++i) {       cur += a[i];       len++;       if (cur == req) {         ans.push_back(len);         cur = 0;         len = 0;       } else if (cur > req) {         cur = a[i];         len = 1;       }     }     if ((int)ans.size() == k) {       printf( Yes n );       for (int i = 0; i < k; ++i) printf( %d  , ans[i]);     } else {       printf( No n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   set<int> S1, S2, S3;   map<int, bool> vis;   int p[2000001], a[200001], b[2000001];   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> p[i];     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) cin >> b[i];     for (int i = 0; i < n; i++) {       if (a[i] == 1 || b[i] == 1) S1.insert(p[i]);       if (a[i] == 2 || b[i] == 2) S2.insert(p[i]);       if (a[i] == 3 || b[i] == 3) S3.insert(p[i]);     }     int m;     cin >> m;     for (int i = 0; i < m; i++) {       int c;       cin >> c;       if (c == 1) {         if (S1.empty())           cout << -1 <<    ;         else {           auto ans = S1.begin();           S2.erase(*ans);           S3.erase(*ans);           cout << *ans <<    ;           S1.erase(*ans);         }       } else if (c == 2) {         if (S2.empty())           cout << -1 <<    ;         else {           auto ans = S2.begin();           S1.erase(*ans);           S3.erase(*ans);           cout << *ans <<    ;           S2.erase(*ans);         }       } else {         if (S3.empty())           cout << -1 <<    ;         else {           auto ans = S3.begin();           S1.erase(*ans);           S2.erase(*ans);           cout << *ans <<    ;           S3.erase(*ans);         }       }     }   }
#include <bits/stdc++.h>   using namespace std;   char s[5010];   int main() {     scanf( %s , s);     int n = (int)strlen(s);     int res = 0;     for (int i = 0; i < n; i++) {       int cnt1 = 0, cnt2 = 0;       for (int j = i; j < n; j++) {         if (s[j] ==  ( ) cnt1++;         if (s[j] ==  ) ) cnt1--;         if (s[j] ==  ? ) cnt2++;         if (cnt2 && cnt2 > cnt1) cnt2--, cnt1++;         if (cnt1 < 0) break;         if (cnt2 == cnt1) res++;       }     }     printf( %d , res);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 400000 + 10;   const int M = 26;   namespace Trie {   int trie[N][M], cnt[N], size;   void init() {     size = 0;     memset(trie[0], 0, sizeof(trie[0]));     cnt[0] = 0;   }   void insert(char str[]) {     int u = 0, v;     for (int i = 0; str[i]; i++) {       v = str[i] -  a ;       if (!trie[u][v]) {         trie[u][v] = ++size;         cnt[size] = 0;         memset(trie[size], 0, sizeof(trie[size]));       }       u = trie[u][v];     }     cnt[u]++;   }   }  // namespace Trie   namespace SAM {   struct state {     int len, link, siz;     int next[M];   } st[N];   int sz, last;   void init() {     sz = 1, last = 0;     st[0].len = 0;     st[0].link = -1;   }   void add(int c) {     int r = sz++;     st[r].len = st[last].len + 1;     st[r].siz = 1;     int p = last;     while (p != -1 && !st[p].next[c]) st[p].next[c] = r, p = st[p].link;     if (p == -1)       st[r].link = 0;     else {       int q = st[p].next[c];       if (st[q].len == st[p].len + 1)         st[r].link = q;       else {         int qq = sz++;         st[qq].link = st[q].link;         memcpy(st[qq].next, st[q].next, sizeof(st[q].next));         st[qq].len = st[p].len + 1;         st[qq].siz = 0;         while (p != -1 && st[p].next[c] == q) st[p].next[c] = qq, p = st[p].link;         st[q].link = st[r].link = qq;       }     }     last = r;   }   int tp[N], tmp[N];   void get_topo() {     memset(tmp, 0, sizeof(tmp));     for (int i = 0; i < sz; i++) tmp[st[i].len]++;     for (int i = 1; i < sz; i++) tmp[i] += tmp[i - 1];     for (int i = 0; i < sz; i++) tp[--tmp[st[i].len]] = i;   }   void calc_siz() {     get_topo();     for (int i = sz - 1; i > 0; i--) st[st[tp[i]].link].siz += st[tp[i]].siz;     st[0].siz = 0;   }   }  // namespace SAM   long long dfs(int tu, int su) {     long long ans = 1ll * Trie::cnt[tu] * SAM::st[su].siz;     for (int i = 0; i < M; i++) {       int tv = Trie::trie[tu][i];       int sv = SAM::st[su].next[i];       if (tv == 0 || sv == 0) continue;       ans += dfs(tv, sv);     }     return ans;   }   int id[N];   void ddfs(int i) {     if (id[i] == -1) return;     for (int j = 0; j < M; j++) {       int v = Trie::trie[i][j];       if (v) {         id[v] = SAM::st[id[i]].next[j];         if (id[v] == 0) id[v]--;         ddfs(v);       }     }   }   char s[N];   int main() {     SAM::init(), Trie::init();     scanf( %s , s);     for (int i = 0; s[i]; i++) SAM::add(s[i] -  a );     SAM::calc_siz();     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %s , s);       Trie::insert(s);     }     ddfs(0);     long long ans = 0;     for (int i = 0; i <= Trie::size; i++) {       if (id[i] <= 0) continue;       if (Trie::cnt[i]) ans += 1ll * Trie::cnt[i] * dfs(0, id[i]);     }     printf( %lld n , ans);   }