Codeforces Round #457 (Div. 2)

Codeforces Round #457 (Div. 2)

A.模拟。

B.模拟。比赛的时候这题出锅了,然后就unrated了。注意要先保证最大值最小,再保证字典序最大。

C.构造。挑一个大于N的质数然后连啊连就可以了。

D.可持久化Trie。弄两个Trie。一个记录字符串,另一个记录数字。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 struct StringTrie {
 6     StringTrie *ch[26]; int val;
 7     StringTrie() {memset(this, 0, sizeof(*this)); val = -1;}
 8     StringTrie* set(int n, const char *str, int p, int v) {
 9         StringTrie *res = new StringTrie(); *res = *this;
10         if (p >= n) res->val = v;
11         else {
12             int c = str[p] - ‘a‘;
13             if (!ch[c]) ch[c] = new StringTrie();
14             res->ch[c] = ch[c]->set(n, str, p + 1, v);
15         } return res;
16     }
17     int query(int n, const char *str, int p) {
18         if (p >= n) return val;
19         else {
20             int c = str[p] - ‘a‘;
21             if (!ch[c]) return -1;
22             return ch[c]->query(n, str, p + 1);
23         }
24     }
25 } *srt[100005];
26 struct BinaryTrie {
27     BinaryTrie *ch[2]; int val;
28     BinaryTrie() {memset(this, 0, sizeof(*this));}
29     BinaryTrie* add(int x, int val, int p) {
30         BinaryTrie *res = new BinaryTrie(); *res = *this; res->val += val;
31         if (p >= 0) {
32             int c = (x >> p) & 1;
33             if (!ch[c]) ch[c] = new BinaryTrie();
34             res->ch[c] = ch[c]->add(x, val, p - 1);
35         } return res;
36     }
37     int query(int x, int p) {
38         if (p < 0) return 0;
39         else {
40             int c = (x >> p) & 1, res = 0;
41             if (c && ch[0]) res += ch[0]->val;
42             if (ch[c]) res += ch[c]->query(x, p - 1);
43             return res;
44         }
45     }
46 } *brt[100005];
47 int main() {
48     srt[0] = new StringTrie(); brt[0] = new BinaryTrie();
49     int n; scanf("%d", &n); char op[20], str[20];
50     for (int v, i = 1; i <= n; ++i) {
51         scanf("%s", op);
52         if (*op == ‘s‘) {
53             scanf("%s%d", str, &v); int l = strlen(str);
54             int x = srt[i-1]->query(l, str, 0);
55             srt[i] = srt[i-1]->set(l, str, 0, v);
56             brt[i] = brt[i-1];
57             if (~x) brt[i] = brt[i]->add(x, -1, 30);
58             brt[i] = brt[i]->add(v, 1, 30);
59         } else if (*op == ‘r‘) {
60             scanf("%s", str); int l = strlen(str);
61             int x = srt[i-1]->query(l, str, 0);
62             brt[i] = brt[i-1]; srt[i] = srt[i-1]->set(l, str, 0, -1);
63             if (~x) brt[i] = brt[i]->add(x, -1, 30);
64         } else if (*op == ‘q‘) {
65             scanf("%s", str); int l = strlen(str);
66             brt[i] = brt[i-1]; srt[i] = srt[i-1];
67             int x = srt[i]->query(l, str, 0);
68             if (~x) printf("%d\n", brt[i]->query(x, 30));
69             else puts("-1"); fflush(stdout);
70         } else {
71             scanf("%d", &v); srt[i] = srt[i-v-1]; brt[i] = brt[i-v-1];
72         }
73     }
74     return 0;
75 }

E.跟bzoj上的遥远的国度很类似。

原文地址:https://www.cnblogs.com/p0ny/p/8328207.html

时间: 2024-07-30 16:43:59

Codeforces Round #457 (Div. 2)的相关文章

【Codeforces Round #457 (Div. 2) A】 Jamie and Alarm Snooze

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 暴力往前走x分钟就好. 直到出现7为止. [代码] #include <bits/stdc++.h> using namespace std; int hh,mm,x,cnt; bool find7(){ if (hh%10==7 || (hh/10)==7) return true; if (mm%10==7 || (mm/10)==7) return true; return false; } int main(){ #i

【Codeforces Round #457 (Div. 2) B】Jamie and Binary Sequence

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把n分解成二进制的形式. n=2^a0+2^a1+...+2^a[q-1] 则固定就是长度为q的序列. 要想扩展为长为k的序列. 可以把2^x转化为2^(x-1)+2^(x-1)的形式. 这样序列的长度就+1了 它要求max{ai}最大 那么我们可以枚举ai的最大值是什么->i (递减着枚举) 然后比i大的ai都换成两个ai-1的形式. 然后看看序列的长度是否小于等于k; 如果小于k的话. 就把min{ai}分解成两个min{a

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd

Codeforces Round #273 (Div. 2)

Codeforces Round #273 (Div. 2) 题目链接 A:签到,仅仅要推断总和是不是5的倍数就可以,注意推断0的情况 B:最大值的情况是每一个集合先放1个,剩下都丢到一个集合去,最小值是尽量平均去分 C:假如3种球从小到大是a, b, c,那么假设(a + b) 2 <= c这个比較明显答案就是a + b了.由于c肯定要剩余了,假设(a + b)2 > c的话,就肯定能构造出最优的(a + b + c) / 3,由于肯定能够先拿a和b去消除c,而且控制a和b成2倍关系或者消除

Codeforces Round #339 (Div. 2) B. Gena&#39;s Code

B. Gena's Code It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, f

Codeforces Round #315 (Div. 1)

A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and un