题意:定义两种操作

  • 1 a ---- 向序列中插如一个元素a
  • 2 a b ---- 将序列的前a个元素[e1,e2,...,ea]重复b次插入到序列中

经过一列操作后,为处于某个位置p的元素是多少。数据范围共有105以内的操作,形成序列中的元素总个数大小不超过64bit长整型表示。

思考:只需要记录两种操作这些关键元素的位置,如果查询的坐标位置刚好是第一种产生的,那么直接就知道结果了;如果查询的是第二种操作产生的,它是由前a个元素重复b次而来,它来自前a个元素,依次递归向前找,直到找到第一种情况。

1 const int maxn = 100005;
2 __int64 pos[maxn];
3 __int64 val[maxn];
4 int flag[maxn];

可能用struct放到一起更直观一些。

flag用来标记,当前元素如果为1,则为第一种操作产生,pos此时记录当前元素位置,val记录当前元素的值。

flag为0时候,则为第二种操作产生,pos记录第二种操作开始的元素位置,val记录a,即序列的前a个元素,这个长度a。

知道这些我们就好办了,用二分,LowBound去找序列中查询元素位置在pos中的下界。然后根据flag来判断是否需要递归继续往下找,或是返回找到的结果。

 1 __int64 GetResult(__int64 key) {
2 int l = 0, r = len-1;
3 while (l <= r) {
4 int m = (l+r) >> 1;
5 if (pos[m] == key) {
6 if (flag[m]) return val[m];
7 else return GetResult((key-pos[m]+1)%val[m] != 0 ? ((key-pos[m]+1)%val[m]) : val[m]);
8 } else if (pos[m] < key) {
9 l = m+1;
10 } else {
11 r = m-1;
12 }
13 }
14 return GetResult((key-pos[r]+1)%val[r] != 0 ? ((key-pos[r]+1)%val[r]) : val[r]);
15 }

下面是完整代码:

  1 #define _CRT_SECURE_NO_DEPRECATE
2 #define _SECURE_SCL 0
3 #pragma comment(linker, "/STACK:66777216")
4 #include <algorithm>
5 #include <string>
6 #include <complex>
7 #include <cassert>
8 #include <memory>
9 #include <set>
10 #include <stack>
11 #include <map>
12 #include <list>
13 #include <deque>
14 #include <numeric>
15 #include <cctype>
16 #include <cstddef>
17 #include <vector>
18 #include <queue>
19 #include <iostream>
20 #include <iomanip>
21 #include <iterator>
22 #include <cmath>
23 #include <cstdio>
24 #include <cstdlib>
25 #include <sstream>
26 #include <fstream>
27 #include <ctime>
28 #include <cstring>
29 #include <functional>
30 #include <bitset>
31 using namespace std;
32
33 #if defined(_MSC_VER) || defined(__BORLANDC__)
34 typedef unsigned __int64 uint64;
35 typedef signed __int64 int64;
36 #else
37 typedef unsigned long long uint64;
38 typedef signed long long int64;
39 #endif
40 typedef vector<int> VI;
41 typedef vector<string> VS;
42 typedef pair<int,int> PII;
43 typedef pair<int64,int64> PLL;
44 typedef vector<int64> VL;
45
46 #define pb push_back
47 #define ppb pop_back
48 #define mp make_pair
49 #define fi first
50 #define se second
51 #define pii pair<int,int>
52 #define pdd pair<double,double>
53 #define FOR(i,a,b) for (int _n(b), i(a); i <= _n; i++)
54 #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;i--)
55 #define all(c) (c).begin(), (c).end()
56 #define SORT(c) sort(all(c))
57 #define REP(i,n) FOR(i,1,(n))
58 #define REPT(i,n) FOR(i,0,(n)-1)
59 #define L(s) (int)((s).size())
60 #define C(a) memset((a),0,sizeof(a))
61 #define IOS ios::sync_with_stdio(false)
62
63 const double pi = 3.1415926535897932384626433832795028841971;
64 const double EPS = 1E-9;
65 const int64 INF64 = (int64)1E18;
66 const int INF = 1000000000;
67
68 static inline bool get(int &v) {
69 int s = 1, c;
70 while(!isdigit(c = getchar())&&c != '-')
71 if(c == EOF) break ;
72
73 if(c == EOF) return 0;
74 if(c == '-') s = 0 , v = 0;
75 else v = c^48;
76 for(;isdigit(c = getchar());v = (v << 1) + (v << 3) + (c ^ 48));
77 v = (s ? v : -v);
78 return 1 ;
79 }
80 int len = 0;
81 const int maxn = 100005;
82 __int64 pos[maxn];
83 __int64 val[maxn];
84 int flag[maxn];
85
86 __int64 GetResult(__int64 key) {
87 int l = 0, r = len-1;
88 while (l <= r) {
89 int m = (l+r) >> 1;
90 if (pos[m] == key) {
91 if (flag[m]) return val[m];
92 else return GetResult((key-pos[m]+1)%val[m] != 0 ? ((key-pos[m]+1)%val[m]) : val[m]);
93 } else if (pos[m] < key) {
94 l = m+1;
95 } else {
96 r = m-1;
97 }
98 }
99 return GetResult((key-pos[r]+1)%val[r] != 0 ? ((key-pos[r]+1)%val[r]) : val[r]);
100 }
101
102
103 void run() {
104 int m, n, op;
105 __int64 e, l, c, v = 1;
106 cin >> m;
107 FOR(i, 1, m) {
108 cin >> op;
109 if (op == 1) {
110 cin >> e;
111 pos[len] = v;
112 val[len] = e;
113 flag[len] = 1;
114 len += 1;
115 v++;
116 } else {
117 cin >> l >> c;
118 pos[len] = v;
119 val[len] = l;
120 flag[len] = 0;
121 v = l*c + pos[len];
122 len += 1;
123 }
124 }
125 __int64 key;
126 cin >> n;
127 FOR(i, 1, n) {
128 cin >> key;
129 cout << GetResult(key) << " ";
130 }
131 cout << endl;
132 }
133
134
135
136 int main() {
137 #ifdef __DEBUG__
138 freopen("test.in","r",stdin);
139 freopen("test.out","w",stdout);
140 time_t st = clock();
141 #endif
142 run();
143 #ifdef __DEBUG__
144 printf( "\n=============\n");
145 printf("Time: %.2lf sec\n",(clock()-st)/double(CLOCKS_PER_SEC));
146 #endif
147 return 0;
148 }

Codeforce 380A Sereja and Prefixes【二分】的更多相关文章

  1. codeforces 380A Sereja and Prefixes (递归)

    题目: A. Sereja and Prefixes time limit per test 1 second memory limit per test 256 megabytes input st ...

  2. Codeforces 380A - Sereja and Prefixes

    原题地址:http://codeforces.com/problemset/problem/380/A 让期末考试整的好久没有写题, 放假之后由于生病也没怎么做,新年的第一场CF也不是那么在状态,只过 ...

  3. codeforce 955c --Sad powers 思路+二分查找

    这一题的题意是   定义一个数,该数特点是为a的p次方 (a>0,p>1) 再给你n个询问,每个询问给出一个区间,求区间内该数的数目. 由于给出的询问数极大(10e5) 所以,容易想到应该 ...

  4. CF:Problem 426B - Sereja and Mirroring 二分或者分治

    这题解法怎么说呢,由于我是把行数逐步除以2暴力得到的答案,所以有点二分的意思,可是昨天琦神说是有点像分治的意思.反正总的来说:就是从大逐步细化找到最优答案. 可是昨晚傻B了.靠! 多写了点东西,然后就 ...

  5. [codeforce 975C] Valhalla Siege (二分)

    Examples input 5 5 1 2 1 2 1 3 10 1 1 1 output 3 5 4 4 3 input 4 4 1 2 3 4 9 1 10 6 output 1 4 4 1 N ...

  6. Codeforces Round #223 (Div. 2) C

    C. Sereja and Prefixes time limit per test 1 second memory limit per test 256 megabytes input standa ...

  7. Codeforces 381 简要题解

    做的太糟糕了...第一题看成两人都取最优策略,写了个n^2的dp,还好pre-test良心(感觉TC和CF的pretest还是很靠谱的),让我反复过不去,仔细看题原来是取两边最大的啊!!!前30分钟就 ...

  8. 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...

  9. codeforce 359D 二分+ 动态规划(sparse table)

    原题链接:http://codeforces.com/problemset/problem/359/D 思路:首先对符合题目的长度(r-l)从0到n-1进行二分查找,对每一个长度进行check,看是否 ...

随机推荐

  1. windows端口占用

    原文链接http://zhhll.icu/2020/04/08/windows/windows%E4%B9%8B%E7%AB%AF%E5%8F%A3%E5%8D%A0%E7%94%A8/ 1.查看当前 ...

  2. PHP将数据集转换成树状结构

    /** * 把返回的数据集转换成Tree * @param array $list 要转换的数据集 * @param string $pid parent标记字段 * @param string $l ...

  3. 初识sa-token,一行代码搞定登录授权!

    前言 在java的世界里,有很多优秀的权限认证框架,如Apache Shiro.Spring Security 等等.这些框架背景强大,历史悠久,其生态也比较齐全. 但同时这些框架也并非十分完美,在前 ...

  4. js原型链原理

    先附上原型链的图,能看懂的本文就没必要看了,看不懂的可以带着疑问看文章 一.构造函数 什么是构造函数:当一个普通函数创建一个类对象是,那么就程它为构造函数. 特点: 默认首字母大写 使用new关键字来 ...

  5. 借助window.performance实现基本的前端基础性能监控日志

    借助window.performance实现基本的前端基础性能监控日志并二次重写console方法方便日常前端console日志的调试 npm install sn-console

  6. 在JavaScript种遇到这样的错误如何解决XML 解析错误:格式不佳 位置:http:/... 行 27,列 32:

    相信很多人在开发的过程中都会遇到在js中解析xml文档的问题.有时候文档解析失败,但就是不知道怎么失败的,哪里格式不对.这里教大家一个方法来排查JavaScript解析xml文档格式出错的办法. 1. ...

  7. kubernets之卷

    一 卷的由来以及种类和常用的卷的类型 前面介绍了大部分都是pod的管理以及在集群内部和集群外部如何访问pod,但是我们也了解到,pod是有生命周期的,当pod所在节点下线,或者等其他原因原因导致pod ...

  8. python的零碎知识

    1.Python代码操作git 安装 pip3 install gitpython 操作git import os from git.repo import Repo # gitpython def ...

  9. linux登陆欢迎信息及命令提示符修改

    登录信息修改 登陆信息显示数据 : /etc/issue and /etc/motd 登陆终端机的时候,会有几行提示的字符串,这些设置在/etc/issue里面可以修改,提示内容在/etc/motd中 ...

  10. Python hashlib的简单使用

    hashlib模块针对不同的安全哈希和消息摘要算法实现了一个通用的接口,其中包括SHA1, SHA224, SHA256, SHA384, SHA512算法以及RSA的MD5算法. 使用方法 第一步 ...