Mutual Training for Wannafly Union #7(待续)

剩下的题等礼拜天补吧。最近要赶作业什么的还要补点dp

A - Ada and List(伸展树或者vector暴力- -)

思路:

  vector玄学均摊复杂度...能直接暴力A了。窝居然写了人生第一个伸展树- -。蠢到家了。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     vector<int>vec;
 6     int n, m;
 7     scanf("%d%d", &n, &m);
 8     for(int i = 1; i <= n; i++)
 9     {
10         int x;
11         scanf("%d", &x);
12         vec.push_back(x);
13     }
14
15     for(int i = 0; i < m; i++)
16     {
17         int cmd, k, x;
18         scanf("%d%d", &cmd, &k);
19         if(cmd == 1)
20         {
21             scanf("%d", &x);
22             vec.insert(vec.begin() + k - 1,  x);
23         }
24         else if(cmd == 2)
25         {
26             vec.erase(vec.begin() + k - 1);
27         }
28         else if(cmd == 3)
29         {
30             printf("%d\n", vec[k - 1]);
31         }
32     }
33     return 0;
34 }

  这个伸展树的板子还是挺厉害了。

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3
  4 #define Key_value ch[ch[root][1]][0]
  5 const int MAXN = 500010;
  6 const int INF = 0x3f3f3f3f;
  7 int pre[MAXN],ch[MAXN][2],key[MAXN],size[MAXN];
  8 int root,tot1;
  9 int sum[MAXN],rev[MAXN],same[MAXN];
 10 int lx[MAXN],rx[MAXN],mx[MAXN];
 11 int s[MAXN],tot2;//内存池和容量
 12 int a[MAXN];
 13 int n,q;
 14
 15 void NewNode(int &r,int father,int k)
 16 {
 17     if(tot2) r = s[tot2--];//取的时候是tot2--,存的时候就是++tot2
 18     else r = ++tot1;
 19     pre[r] = father;
 20     ch[r][0] = ch[r][1] = 0;
 21     key[r] = k;
 22     sum[r] = k;
 23     rev[r] = same[r] = 0;
 24     lx[r] = rx[r] = mx[r] = k;
 25     size[r] = 1;
 26 }
 27 void Update_Rev(int r)
 28 {
 29     if(!r)return;
 30     swap(ch[r][0],ch[r][1]);
 31     swap(lx[r],rx[r]);
 32     rev[r] ^= 1;
 33 }
 34 void Update_Same(int r,int v)
 35 {
 36     if(!r)return;
 37     key[r] = v;
 38     sum[r] = v*size[r];
 39     lx[r] = rx[r] = mx[r] = max(v,v*size[r]);
 40     same[r] = 1;
 41 }
 42 void push_up(int r)
 43 {
 44     int lson = ch[r][0], rson = ch[r][1];
 45     size[r] = size[lson] + size[rson] + 1;
 46     sum[r] = sum[lson] + sum[rson] + key[r];
 47     lx[r] = max(lx[lson],sum[lson] + key[r] + max(0,lx[rson]));
 48     rx[r] = max(rx[rson],sum[rson] + key[r] + max(0,rx[lson]));
 49     mx[r] = max(0,rx[lson]) + key[r] + max(0,lx[rson]);
 50     mx[r] = max(mx[r],max(mx[lson],mx[rson]));
 51 }
 52 void push_down(int r)
 53 {
 54     if(same[r])
 55     {
 56         Update_Same(ch[r][0],key[r]);
 57         Update_Same(ch[r][1],key[r]);
 58         same[r] = 0;
 59     }
 60     if(rev[r])
 61     {
 62         Update_Rev(ch[r][0]);
 63         Update_Rev(ch[r][1]);
 64         rev[r] = 0;
 65     }
 66 }
 67 void Build(int &x,int l,int r,int father)
 68 {
 69     if(l > r)return;
 70     int mid = (l+r)/2;
 71     NewNode(x,father,a[mid]);
 72     Build(ch[x][0],l,mid-1,x);
 73     Build(ch[x][1],mid+1,r,x);
 74     push_up(x);
 75 }
 76 void Init()
 77 {
 78     root = tot1 = tot2 = 0;
 79     ch[root][0] = ch[root][1] = size[root] = pre[root] = 0;
 80     same[root] = rev[root] = sum[root] = key[root] = 0;
 81     lx[root] = rx[root] = mx[root] = -INF;
 82     NewNode(root,0,-1);
 83     NewNode(ch[root][1],root,-1);
 84     for(int i = 0;i < n;i++)
 85         scanf("%d",&a[i]);
 86     Build(Key_value,0,n-1,ch[root][1]);
 87     push_up(ch[root][1]);
 88     push_up(root);
 89 }
 90 //旋转,0为左旋,1为右旋
 91 void Rotate(int x,int kind)
 92 {
 93     int y = pre[x];
 94     push_down(y);
 95     push_down(x);
 96     ch[y][!kind] = ch[x][kind];
 97     pre[ch[x][kind]] = y;
 98     if(pre[y])
 99         ch[pre[y]][ch[pre[y]][1]==y] = x;
100     pre[x] = pre[y];
101     ch[x][kind] = y;
102     pre[y] = x;
103     push_up(y);
104 }
105 //Splay调整,将r结点调整到goal下面
106 void Splay(int r,int goal)
107 {
108     push_down(r);
109     while(pre[r] != goal)
110     {
111         if(pre[pre[r]] == goal)
112         {
113             push_down(pre[r]);
114             push_down(r);
115             Rotate(r,ch[pre[r]][0] == r);
116         }
117         else
118         {
119             push_down(pre[pre[r]]);
120             push_down(pre[r]);
121             push_down(r);
122             int y = pre[r];
123             int kind = ch[pre[y]][0]==y;
124             if(ch[y][kind] == r)
125             {
126                 Rotate(r,!kind);
127                 Rotate(r,kind);
128             }
129             else
130             {
131                 Rotate(y,kind);
132                 Rotate(r,kind);
133             }
134         }
135     }
136     push_up(r);
137     if(goal == 0) root = r;
138 }
139 int Get_kth(int r,int k)
140 {
141     push_down(r);
142     int t = size[ch[r][0]] + 1;
143     if(t == k)return r;
144     if(t > k)return Get_kth(ch[r][0],k);
145     else return Get_kth(ch[r][1],k-t);
146 }
147
148 //在第pos个数后面插入tot个数
149 void Insert(int pos,int tot)
150 {
151     for(int i = 0;i < tot;i++)scanf("%d",&a[i]);
152     Splay(Get_kth(root,pos+1),0);
153     Splay(Get_kth(root,pos+2),root);
154     Build(Key_value,0,tot-1,ch[root][1]);
155     push_up(ch[root][1]);
156     push_up(root);
157 }
158
159 //删除子树
160 void erase(int r)
161 {
162     if(!r)return;
163     s[++tot2] = r;
164     erase(ch[r][0]);
165     erase(ch[r][1]);
166 }
167 //从第pos个数开始连续删除tot个数
168 void Delete(int pos,int tot)
169 {
170     Splay(Get_kth(root,pos),0);
171     Splay(Get_kth(root,pos+tot+1),root);
172     erase(Key_value);
173     pre[Key_value] = 0;
174     Key_value = 0;
175     push_up(ch[root][1]);
176     push_up(root);
177 }
178
179
180 //得到第pos个数开始的tot个数的和
181 int Get_Sum(int pos,int tot)
182 {
183     Splay(Get_kth(root,pos),0);
184     Splay(Get_kth(root,pos+tot+1),root);
185     return sum[Key_value];
186 }
187
188
189 void InOrder(int r)
190 {
191     if(!r)return;
192     push_down(r);
193     InOrder(ch[r][0]);
194     printf("%d ",key[r]);
195     InOrder(ch[r][1]);
196 }
197
198 int main()
199 {
200     scanf("%d%d", &n, &q);
201     Init();
202
203     while(q--)
204     {
205         int cmd;
206         scanf("%d", &cmd);
207         if(cmd == 1)
208         {//1 k x means that you will add number x to position k
209             int k;
210             scanf("%d", &k);
211             Insert(k - 1, 1);
212         }
213         else if(cmd == 2)
214         {//2 k means that you will erase number from position k
215             int k;
216             scanf("%d", &k);
217             Delete(k, 1);
218         }
219         else if(cmd == 3)
220         {//3 k means that you will print number from position k
221             int k;
222             scanf("%d", &k);
223             printf("%d\n",Get_Sum(k,1));
224         }
225
226     }
227     return 0;
228 }

B - Ada and Queue(双端队列)

思路:

  打个标记,记录一下现在是正着还是反着就好了。然后就是一个双端队列。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     deque<int>que;
 6     deque<int>::iterator it;
 7     deque<int>::reverse_iterator rit;
 8
 9     int n;
10     scanf("%d", &n);
11     int tig = 0;
12     while(n--)
13     {
14         char cmd[100];
15         scanf("%s", cmd);
16
17         if(cmd[0] == ‘b‘)
18         {//back - Print number from back and then erase it
19             if(que.size() == 0)
20             {
21                 puts("No job for Ada?");
22             }
23             else if(tig == 0)
24             {
25                 rit = que.rbegin();
26                 cout << *rit << "\n";
27                 que.pop_back();
28             }
29             else
30             {
31                 it = que.begin();
32                 cout << *it << "\n";
33                 que.pop_front();
34             }
35         }
36         else if(cmd[0] == ‘f‘)
37         {//front - Print number from front and then erase it
38             if(que.size() == 0)
39             {
40                 puts("No job for Ada?");
41             }
42             else if(tig == 1)
43             {
44                 rit = que.rbegin();
45                 cout << *rit << "\n";
46                 que.pop_back();
47             }
48             else if(tig == 0)
49             {
50                 it = que.begin();
51                 cout << *it << "\n";
52                 que.pop_front();
53             }
54         }
55         else if(cmd[0] == ‘r‘)
56         {//reverse - Reverses all elements in queue
57             tig ^= 1;
58         }
59         else if(cmd[0] == ‘p‘)
60         {//push_back N - Add element N to back
61             int x;
62             scanf("%d", &x);
63             if(tig == 0)
64             {
65                 que.push_back(x);
66             }
67             else
68             {
69                 que.push_front(x);
70             }
71         }
72         else if(cmd[0] == ‘t‘)
73         {//toFront N - Put element N to front
74             int x;
75             scanf("%d", &x);
76             if(tig == 1)
77             {
78                 que.push_back(x);
79             }
80             else
81             {
82                 que.push_front(x);
83             }
84         }
85     }
86     return 0;
87 }

C - Ada and Field

D - Ada and Cycle(bitset优化bfs)

E - Face Detection(水题暴力枚举)

思路:

  暴力枚举一下2*2的小矩形的左上角就好了,wa点!!!n*m的图,两层for循环写成n*n了!!!

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 int n, m;
 5 char ma[55][55];
 6 #define isInside(x, y)  1 <= x && x <= n && 1 <= y && y <= m
 7
 8 bool judge(int x, int y)
 9 {
10     if(isInside(x, y) && isInside(x + 1, y) && isInside(x, y + 1) && isInside(x + 1, y + 1))
11     {
12         map<char, int>s;
13         s[ma[x][y]] = 1;
14         s[ma[x + 1][y]] = 1;
15         s[ma[x][y + 1]] = 1;
16         s[ma[x + 1][y + 1]] = 1;
17         if(s[‘f‘] + s[‘a‘] + s[‘c‘] + s[‘e‘] == 4)  return true;
18     }
19     return false;
20 }
21 int main()
22 {
23     scanf("%d%d", &n, &m);
24     for(int i = 1; i <= n; i++)
25         scanf("%s", ma[i] + 1);
26
27     int ans = 0;
28     for(int i = 1; i <= n; i++)
29     {
30         for(int j = 1; j <= m; j++)
31         {
32             if(judge(i, j)) ans++;
33         }
34     }
35     printf("%d\n", ans);
36     return 0;
37 }

F - Gears

时间: 2024-10-20 00:45:51

Mutual Training for Wannafly Union #7(待续)的相关文章

Mutual Training for Wannafly Union #1解题报告

---恢复内容开始--- q神等人组织的vjudge上的多校训练,题目基本上都来自于CF,#1是上周进行的,参加后感觉收获很多,因为上周准备期中比较忙,解题报告现在补上. 比赛地址(兼题目地址) A题 DFS搜索所有路径,需要注意的是走的顺序,先向前走一步,再向上或向下或静止不动,由于车一直在走,可以认为是相对运动,即人再向右侧走两个格子.遇到当前位置是车就说明走不通,可以走出的条件是已经在此行所有车的后面. 注意,一定要写visited记录走没走过格子(也就我这种搜索初学者会犯这么傻的错误吧,

Mutual Training for Wannafly Union #2

codeforces 298A. Snow Footprints 分类讨论三种情况: ①..RRRRRR-  ②..LLLLLLL- ③..RRRLLLL.. //AC by lwq: #include<stdio.h> #include<algorithm> #include<string> #include<string.h> using namespace std; int main() { int n; scanf("%d",&a

【Mutual Training for Wannafly Union #1 】

A.Phillip and Trains CodeForces 586D 题意:过隧道,每次人可以先向前一格,然后向上或向下或不动,然后车都向左2格.问能否到达隧道终点. 题解:dp,一开始s所在列如果前方为'.'则dp[i]=1.r[i]代表上一次的dp[i]值. 如果该行当前可行,那么它就可以更新它上下两行(如果有),必须用r[i]去更新. 再判断每行在当前时间是否会发生撞车:看看位置 i+t*2,i+t*2+1,i+t*2+2 是否有车. #include <iostream> #inc

USACO(含training section)水题合集[5/未完待续]

(1) USACO2.1 Ordered Fractions 枚举 排序即可,注意1/1 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int N=165,L=1e5; struct fr{ int a,b; fr(int q=0,int w=1):a(q),b(w){} }f[L]; int n,cnt

UESTC 2016 Summer Training #5 Div.2(未完待续)

A #include <cstdio> #include <cstring> #include <vector> #define MAXN 100005 #define mem(a) memset(a, 0, sizeof(a)) using namespace std; int TreeArray[MAXN], Left[MAXN], Right[MAXN], Fork[MAXN]; typedef vector<int> Ve; vector<Ve

soj [Union Training I] N AC自动机 + dp

Problem: 给你m个字符串,问长度为n,至少包含其中一个字符串为子串的字符串的个数. Analize: - 将问题转化为 26k?不包含任意子串的个数 ,这里用AC自动机来建立禁止字符串. - 所有节点的子节点开始都为根节点,在添加禁止字符串的过程中扩展 - 利用fail指针来扩展禁止字符串,也就是当前字符串的fail指针的字符串如果都是禁止字符串那么当前节点也一定是. /**********************jibancanyang*************************

C++ 笔记(待续)

本文摘自:Thinking in C++ Vol.1  (添加部分C++ primer内容.待续...) 目录: 第零章:help and tips 第一章:对象 第二章:c in c++ 第三章:深入理解字符串 第四.五章:实现的隐藏 第六.七章:初始化和清除 第九章:内联函数 第十章:名字控制 第十一章:引用和拷贝构造函数 第十二章:运算符重载 第零章:help and tips [Index] 安装c/c++的帮助文档:yum install man-pages libstdc++-doc

Liberty Mutual Property Inspection, Winner&#39;s Interview: Qingchen Wang

Liberty Mutual Property Inspection, Winner's Interview: Qingchen Wang The hugely popular Liberty Mutual Group: Property Inspection Prediction competition wrapped up on August 28, 2015 with Qingchen Wang at the top of a crowded leaderboard. A total of

2016 UESTC Training for Data Structures

2016 UESTC Training for Data Structures A - 卿学姐与公主 题意: 有两个操作一个是单点造成攻击,第二个是求区间最大值. 题解: 0.裸的线段树操作,不过zkw线段树显然更好. 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 1000