2018 National multi-school algorithm winter training Camp Practice Competition (fifth)

Source: Internet
Author: User

"Topic link"

A-Reverse order number

Classic problem, there are many methods, such as tree array, line segment tree, merge sort, etc. The code is not posted.

B-big Water problem

Single-point modification for interval and tree-like array or line tree can be.

#include <bits/stdc++.h>using namespace std; const int MAXN = 1e5 + 10;long long C[MAXN]; int lowbit (int x) {  return x & (-X);} long Long (int p) {  long long res = 0;  while (p) {    res + = c[p];    P-= Lowbit (P);  }  return res;} void update (int x, long long y) {while  (x < MAXN) {    c[x] + = y;    x + = Lowbit (x);  }} int main () {     int n, m;  scanf ("%d%d", &n, &m);  for (int i = 1; I <= n; i + +) {    long long x;    scanf ("%lld", &x);    Update (i, x);  }  while (M-) {    int op;    scanf ("%d", &op);    if (op = = 1) {      int x;      Long long y;      scanf ("%d%lld", &x, &y);      Update (x, y);    } else {      int L, R;      scanf ("%d%d", &l, &r);      printf ("%lld\n", SUM (R)-sum (L-1));    }  }  return 0;}

C-Problems with strings

The violence in the game from the big to the small enumeration prefix length, KMP verify the AC? Also think about the complexity of the right.

#include <iostream> #include <cstring>using namespace std; const int N = 1000010;int Nx[n];char s[n], t[n];int Slen, Tlen;  void GetNext () {int J, K; j = 0; K =-1;  Nx[0] =-1; while (J < Tlen) if (k = =-1 | |    T[J] = = T[k]) nx[++j] = ++k;   else k = Nx[k];  }/* Returns the number of times the pattern string appears in the main string s */int kmp_count () {int ans = 0;     int I, j = 0;    if (Slen = = 1 && tlen = = 1) {if (s[0] = = T[0]) return 1;  else return 0;  } getNext ();    for (i = 0; i < Slen; i++) {while (J > 0 && s[i]! = t[j]) j = nx[j];    if (s[i] = = T[j]) j + +;      if (j = = Tlen) {ans++;    j = Nx[j]; }} return ans;  /*int Main () {int TT;  int i, CC;  cin>>tt;    while (tt--) {cin>>s>>t;    Slen = strlen (S);    Tlen = strlen (T);    The first occurrence of the cout<< "pattern string T in the main string s is:" <<kmp_index () <<endl;  cout<< "mode string T appears in the main string s in the number of times:" <<kmp_count () <<endl; } return 0;}  */int check (int x) {int p1 = 0; int P2 = SLEn-x;    while (P1 <= x-1) {if (S[P1]! = s[p2]) return 0;    P1 + +;  P2 + +;    } for (int i = 0; i < x; i + +) {T[i] = s[i];  T[i + 1] = 0;  } Tlen = x;  if (Kmp_count () >= 3) return 1; return 0;}  int main () {scanf ("%s", s);  Slen = strlen (S);  int ans = 0;  int L = 1, R = Slen;      for (int i = slen-1; I >= 1; I-) {if (check (i)) {ans = i;    Break  }} if (ans = = 0) printf ("Just a legend\n");    else {for (int i = 0; i < ans; i + +) {printf ("%c", S[i]);  } printf ("\ n"); }}

D-Collection Issues

Write a bunch of violent simulation rules to get past, see and check the collection of AC, to learn.

#include <bits/stdc++.h>using namespace std; const int MAXN = 110000;int n;int A, B;int a[maxn];map<int, int> belong;map<int, bool> flag;  int main () {int fail = 0;  scanf ("%d%d%d", &n, &a, &b);    for (int i = 1; I <= n; i + +) {scanf ("%d", &a[i]);  Flag[a[i]] = 1;  } sort (A + 1, a + 1 + N);        if (A = = B) {for (int i = 1; I <= n; i + +) {if (flag[b-a[i]] = = 0) {fail = 1;      Break    } Belong[a[i]] = 2;      }} else {while (1) {if (fail);      int ss = 0;        for (int i = 1; I <= n; i + +) {if (Belong[a[i]]) continue;          if (flag[a-a[i] [= 0 && flag[b-a[i]] = = 0) {fail = 1;        Break             } else if (flag[a-a[i]] = = 1 && (flag[b-a[i]] = = 0 | | belong[b-a[i]]) {if (belong[a-a[i]] = = 2) {            Fail = 1;          Break          } Belong[a[i]] = 1;          Belong[a-a[i]] = 1;        SS = 1; } else if ((flag[a-a[i]] = = 0 | |            Belong[a-a[i]]) && flag[b-a[i]] = = 1) {if (belong[b-a[i]] = = 1) {fail = 1;          Break          } Belong[a[i]] = 2;          Belong[b-a[i]] = 2;        SS = 1; } else {if (a-a[i]! = A[i] && b-a[i]! = A[i]) {if (belong[a-a[i]] = = 0 && belong            [B-a[i]] = = 0) {continue;              } else if (Belong[a-a[i]]) {Belong[a[i]] = belong[a-a[i]];            SS = 1;              } else {Belong[a[i]] = belong[b-a[i]];            SS = 1;              }} else if (a-a[i] = = A[i]) {if (belong[b-a[i] = = 1) {fail = 1;            Break            } Belong[a[i]] = 2;            Belong[b-a[i]] = 2;          SS = 1;              } else {if (belong[a-a[i]] = = 2) {fail = 1;            Break            } Belong[a[i]] = 1;     Belong[a-a[i]] = 1;       SS = 1;      }}} if (fail) break;    if (ss = = 0) break; }} if (fail = = 0) {for (int i = 1; I <= n; i + +) {if (belong[a[i] = = 1 && belong[a-a[i]]! = 1)      Fail = 1;    if (belong[a[i] [= 2 && belong[b-a[i]]! = 2) fail = 1;  }} if (fail) {printf ("no\n");    } else {printf ("yes\n");      for (int i = 1; I <= n; i + +) {printf ("%d", Belong[a[i]]-1);      if (i < n) printf ("");    else printf ("\ n"); }} return 0;}

E-The light bulb of Valentine's Day

Single-point update, sub-matrix and two-dimensional tree array can be.

#include <bits/stdc++.h>using namespace std; const int MAXN = 1010;int n, m; int C[maxn][maxn];int A[MAXN][MAXN]; int lowbit (int x) {return x & (-X);} void Add (int x, int y, int c) {for (int i = x; i <= n; i + = Lowbit (i)) {F    or (int j = y; J <= N; j + = Lowbit (j)) {C[i][j] + = C;  }}} int sum (int x,int y) {int ret = 0;    for (int i = x; i > 0; I-= Lowbit (i)) {for (int j = y;j > 0;j = Lowbit (j)) {ret + = C[i][j]; }} return ret;}  int main () {scanf ("%d%d", &n, &m);      for (int i = 1; I <= n; i + +) {for (int j = 1; J <= N; j + +) {scanf ("%d", &a[i][j]);    Add (I, J, A[i][j]);    }} while (M-) {int op;    scanf ("%d", &op);      if (op = = 1) {int x, y;      scanf ("%d%d", &x, &y);      if (a[x][y] = = 1) {Add (x, Y,-1);      } else {Add (x, y, 1);    } A[x][y] = (A[x][y] ^ 1);      } else {int x1, y1, x2, y2;      scanf ("%d%d%d%d", &x1, &y1, &x2, &y2); Printf ("%d\n", sum (x2, y2)-sum (x1-1, y2)-sum (x2, y1-1) + sum (x1-1, y1-1)); }} return 0;}

F-the Biggest water problem

Just a mock-up.

#include <bits/stdc++.h>using namespace Std;long long N;long long work (long long x) {  long long res = 0;  while (x) {    res + = x%;    x = X/10;  }  return res;} int main () {  scanf ("%d", &n);  while (1) {    if (n <= 9) {      printf ("%lld\n", n);      break;    }    n = Work (n);  }  return 0;}

G-Send out-qaq

25,161 samples of the problem and HDU

H-tree Recovery

Interval Plus, interval summation, line tree can be.

#include <bits/stdc++.h>using namespace std; const int MAXN = 1e5 + 10; int n, Q;long long sum[4 * Maxn];long long f[4 * MAXN];  void pushdown (int l, int r, int rt) {if (f[rt] = = 0) return;  int mid = (L + r)/2;  int len_left = (mid-l + 1);  int len_right = (r-mid);  F[2 * RT] + = F[rt];  f[2 * RT + 1] + = F[rt];  Sum[2 * RT] + = f[rt] * LEN_LEFT;  sum[2 * RT + 1] + = f[rt] * len_right; F[RT] = 0;} void pushup (int rt) {SUM[RT] =sum[2 * RT] + sum[2 * RT + 1];} void build (int l, int r, int rt) {if (L = = r) {scanf (    "%lld", &sum[rt]);  Return  } int mid = (L + r)/2;  Build (L, Mid, 2 * RT);  Build (mid + 1, r, 2 * RT + 1); Pushup (RT);}  void Update (int l, int R, Long long val, int l, int r, int rt) {if (L <= l && R <= R) {Sum[rt] + = val *    (r-l + 1);    F[RT] + = val;  Return  } pushdown (L, R, RT);  int mid = (L + r)/2;  if (l <= mid) Update (L, R, Val, L, Mid, 2 * RT);  if (R > Mid) Update (L, R, Val, mid + 1, r, 2 * RT + 1); Pushup (RT);} Long long query (int l, int r, int l, int r, int rt) {if (L <= l && R <= R) {return SUM[RT];  } pushdown (L, R, RT);  int mid = (L + r)/2;  Long long left = 0;  Long long right = 0;  if (l <= mid) left = query (L, R, L, Mid, 2 * RT);  if (R > Mid) Right = query (L, R, Mid + 1, r, 2 * RT + 1);  Pushup (RT); return left + right;}  int main () {scanf ("%d%d", &n, &q);  Build (1, N, 1);    while (Q-) {char op[10];    scanf ("%s", op);      if (op[0] = = ' Q ') {int L, R;      scanf ("%d%d", &l, &r);    printf ("%lld\n", Query (L, R, 1, N, 1));      } else {int L; int R;      A long long Val;      scanf ("%d%d%lld", &l, &r, &val);    Update (L, R, Val, 1, N, 1); }} return 0;}

2018 National multi-school algorithm winter training Camp Practice Competition (fifth)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.