2018年全国多校算法寒假训练营练习比赛(第五场)

A:逆序数

写过一篇原理,这里不想多说。 传送门

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 #define ULL unsigned LL
 5 #define fi first
 6 #define se second
 7 #define lson l,m,rt<<1
 8 #define rson m+1,r,rt<<1|1
 9 #define max3(a,b,c) max(a,max(b,c))
10 const int INF = 0x3f3f3f3f;
11 const LL mod = 1e9+7;
12 typedef pair<int,int> pll;
13 const int N = 100000+10;
14 int tree[N];
15 int lowbit(int x)
16 {
17     return x&(-x);
18 }
19 void add(int x)
20 {
21     while(x < N)
22     {
23         tree[x]++;
24         x += lowbit(x);
25     }
26 }
27 int Query(int x)
28 {
29     int ret = 0;
30     while(x)
31     {
32         ret += tree[x];
33         x -= lowbit(x);
34     }
35     return ret;
36 }
37 int main()
38 {
39     ios::sync_with_stdio(false);
40     cin.tie(0);
41     cout.tie(0);
42     int n;
43     cin >> n;
44     LL ans = 0;
45     while(n--)
46     {
47         int tmp;
48         cin >> tmp;
49         tmp++;
50         ans = ans + Query(N-1) - Query(tmp);
51         add(tmp);
52     }
53     cout << ans << endl;
54     return 0;
55 }
View Code

B:Big Wate Problem

树状数组裸题

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 #define ULL unsigned LL
 5 #define fi first
 6 #define se second
 7 #define lson l,m,rt<<1
 8 #define rson m+1,r,rt<<1|1
 9 #define max3(a,b,c) max(a,max(b,c))
10 const int INF = 0x3f3f3f3f;
11 const LL mod = 1e9+7;
12 typedef pair<int,int> pll;
13 const int N = 100000+10;
14 LL tree[N];
15 int n, m;
16 int lowbit(int x){
17     return x&(-x);
18 }
19 void add(int x, int c){
20     while(x <= n){
21         tree[x] += c;
22         x += lowbit(x);
23     }
24 }
25 LL Query(int x){
26     LL ret = 0;
27     while(x){
28         ret += tree[x];
29         x -= lowbit(x);
30     }
31     return ret;
32 }
33 int main(){
34     ios::sync_with_stdio(false);
35     cin.tie(0);
36     cout.tie(0);
37     cin >> n >> m;
38     int t;
39     for(int i = 1; i <= n; i++){
40         cin >> t;
41         add(i,t);
42     }
43     int x, y;
44     while(m--){
45         cin >> t >> x >> y;
46         if(t == 1){
47             add(x,y);
48         }
49         else {
50             cout << Query(y) - Query(x-1) << endl;
51         }
52     }
53     return 0;
54 }
View Code

C:字符串的问题

KMP裸题

注意一点的就是 aaaaaa 输出的是 aaaa, 只要不完全重合位置就好了。

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 #define ULL unsigned LL
 5 #define fi first
 6 #define se second
 7 #define lson l,m,rt<<1
 8 #define rson m+1,r,rt<<1|1
 9 #define max3(a,b,c) max(a,max(b,c))
10 const int INF = 0x3f3f3f3f;
11 const LL mod = 1e9+7;
12 typedef pair<int,int> pll;
13 string str;
14 const int N = 1e6+5;
15 int nt[N];
16 int n;
17 void get_nt(){
18     nt[0] = -1;
19     int k = -1, j = 0;
20     while(j < n){
21         if(k == -1 || str[k] == str[j]) nt[++j] = ++k;
22         else k = nt[k];
23     }
24 }
25 bool Kmp(int len){
26     for(int i = 1, j = 0; i < n-1; i++){
27         while(j > 0 && str[i] != str[j]) j = nt[j];
28         if(str[i] == str[j]) j++;
29         if(j == len) return true;
30     }
31     return false;
32 }
33 bool pre_suf(int len){
34     for(int i = 0; i < len; i++){
35         if(str[i] != str[n-len+i]) return false;
36     }
37     return true;
38 }
39 bool check()
40 {
41     for(int i = n-1; i >= 1; i--){
42         if(pre_suf(i))
43             if(Kmp(i)){
44             for(int j = 0; j < i; j++)
45                 cout << str[j];
46             return true;
47         }
48     }
49     return false;
50 }
51 int main(){
52     ios::sync_with_stdio(false);
53     cin.tie(0);
54     cout.tie(0);
55     cin >> str;
56     n = str.size();
57     get_nt();
58     if(!check()) cout << "Just a legend
";
59     return 0;
60 }
View Code

D:集合问题

DFS搜索, 右转传送门

E:情人节的灯泡

2维树状数组

每次对点(x1,y1)修改的时候 会 影响到区域D,

假设现在查询(x1,y1) ---(x2,y2)。

Query(x2,y2) = A+B+C+D.

Query(x1-1,y1-1) = A

Query(x1-1,y2) = A + C

Query(x2,y1-1) = A + B

然后组合一下就可以得到D的值

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 #define ULL unsigned LL
 5 #define fi first
 6 #define se second
 7 #define lson l,m,rt<<1
 8 #define rson m+1,r,rt<<1|1
 9 #define max3(a,b,c) max(a,max(b,c))
10 const int INF = 0x3f3f3f3f;
11 const LL mod = 1e9+7;
12 typedef pair<int,int> pll;
13 const int N = 1e3+10;
14 int vis[N][N];
15 int tree[N][N];
16 int n, m;
17 int lowbit(int x){
18     return x&(-x);
19 }
20 void Add(int x, int y, int c){
21     for(int i = x; i <= n; i+=lowbit(i)){
22         for(int j = y; j <= n; j+=lowbit(j)){
23             tree[i][j] += c;
24         }
25     }
26 }
27 int Query(int x, int y){
28     int ret = 0;
29     for(int i = x; i > 0; i-=lowbit(i)){
30         for(int j = y; j > 0; j-=lowbit(j)){
31             ret += tree[i][j];
32         }
33     }
34     return ret;
35 }
36 int main(){
37     ios::sync_with_stdio(false);
38     cin.tie(0);
39     cout.tie(0);
40     cin >> n >> m;
41     for(int i = 1; i <= n; i++)
42         for(int j = 1; j <= n; j++){
43             cin >> vis[i][j];
44             if(vis[i][j]) Add(i,j,1);
45     }
46     int x1, y1, x2, y2, t;
47     while(m--){
48         cin >> t;
49         if(t == 1){
50             cin >> x1 >> y1;
51             if(vis[x1][y1] == 0) vis[x1][y1] = 1, Add(x1,y1,1);
52             else vis[x1][y1] = 0, Add(x1,y1,-1);
53         }
54         else {
55             cin >> x1 >> y1 >> x2 >> y2;
56             cout << Query(x2,y2) +  Query(x1-1,y1-1) - Query(x2,y1-1) - Query(x1-1,y2) << endl;
57         }
58     }
59     return 0;
60 }
View Code

F:The Biggest Water Problem

代码:

只要while就好了

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 #define ULL unsigned LL
 5 #define fi first
 6 #define se second
 7 #define lson l,m,rt<<1
 8 #define rson m+1,r,rt<<1|1
 9 #define max3(a,b,c) max(a,max(b,c))
10 const int INF = 0x3f3f3f3f;
11 const LL mod = 1e9+7;
12 typedef pair<int,int> pll;
13 int c(int x)
14 {
15     int ret = 0;
16     while(x){
17         ret += x%10;
18         x /= 10;
19     }
20     return ret;
21 }
22 int main()
23 {
24     ios::sync_with_stdio(false);
25     cin.tie(0);
26     cout.tie(0);
27     int n;
28     cin >> n;
29     while(n >= 10){
30         n = c(n);
31     }
32     cout << n << endl;
33     return 0;
34 }
View Code

G:送分啦-QAQ

Fibonacci博弈

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 #define ULL unsigned LL
 5 #define fi first
 6 #define se second
 7 #define lson l,m,rt<<1
 8 #define rson m+1,r,rt<<1|1
 9 #define max3(a,b,c) max(a,max(b,c))
10 const int INF = 0x3f3f3f3f;
11 const LL mod = 1e9+7;
12 typedef pair<int,int> pll;
13 set<int> fib;
14 int main()
15 {
16     ios::sync_with_stdio(false);
17     cin.tie(0);
18     cout.tie(0);
19     fib.insert(3);
20     fib.insert(5);
21     int a = 3, b = 5, c;
22     while(a+b < 1e9+5){
23         c = a+b;
24         fib.insert(c);
25         a = b;
26         b = c;
27     }
28     int n;
29     cin >> n;
30     if(fib.count(n)){
31         cout << "Sha" << endl;
32     }
33     else cout << "Xian" << endl;
34     return 0;
35 }
View Code

H:Tree Recovery

线段树区域修改查询裸题

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 #define ULL unsigned LL
 5 #define fi first
 6 #define se second
 7 #define lson l,m,rt<<1
 8 #define rson m+1,r,rt<<1|1
 9 #define max3(a,b,c) max(a,max(b,c))
10 const int INF = 0x3f3f3f3f;
11 const LL mod = 1e9+7;
12 typedef pair<int,int> pll;
13 const int N = 100000+10;
14 LL tree[N << 2], a[N];
15 LL lazy[N << 2];
16 void Update(int rt)
17 {
18     tree[rt] = tree[rt<<1|1] + tree[rt<<1];
19 }
20 void PushDown(int rt, int lenl, int lenr)
21 {
22     if(lazy[rt])
23     {
24         lazy[rt<<1] += lazy[rt];
25         lazy[rt<<1|1] += lazy[rt];
26         tree[rt<<1] += 1ll*lenl*lazy[rt];
27         tree[rt<<1|1] += 1ll*lenr*lazy[rt];
28         lazy[rt] = 0;
29     }
30 }
31 void Add(int L, int R, int C, int l, int r, int rt)
32 {
33     if(L <= l && r <= R)
34     {
35         lazy[rt] += C;
36         tree[rt] += 1ll*(r-l+1)*C;
37         return ;
38     }
39     int m = (l+r)/2;
40     PushDown(rt, m-l+1,r-m);
41     if(L <= m) Add(L,R,C,lson);
42     if(m < R) Add(L,R,C,rson);
43     Update(rt);
44 }
45 LL Query(int L, int R, int l, int r, int rt)
46 {
47     if(L <= l && r <= R)
48         return tree[rt];
49     int m = (l+r)/2;
50     PushDown(rt,m-l+1,r-m);
51     LL ret = 0;
52     if(L <= m) ret += Query(L,R,lson);
53     if(m < R) ret += Query(L,R,rson);
54     return ret;
55 }
56 void Build(int l, int r, int rt)
57 {
58     lazy[rt] = 0;
59     if(l == r)
60     {
61         tree[rt] = a[l];
62         return ;
63     }
64     int m = (l+r)/2;
65     Build(lson);
66     Build(rson);
67     Update(rt);
68 }
69 int main()
70 {
71     ios::sync_with_stdio(false);
72     cin.tie(0);
73     cout.tie(0);
74     int n, m, x, y, z;
75     string str;
76     cin >> n >> m;
77     for(int i = 1; i <= n; i++)
78         cin >> a[i];
79     Build(1,n,1);
80     while(m--){
81         cin >> str;
82         if(str[0] == 'Q') {
83             cin >> x >> y;
84             cout << Query(x,y,1,n,1) << endl;
85         }
86         else {
87             cin >> x >> y >> z;
88             Add(x,y,z,1,n,1);
89         }
90     }
91     return 0;
92 }
View Code
原文地址:https://www.cnblogs.com/MingSD/p/8470880.html