Codeforces Round #223 (Div. 2) C
1 second
256 megabytes
standard input
standard output
Sereja loves number sequences very much. That's why he decided to make himself a new one following a certain algorithm.
Sereja takes a blank piece of paper. Then he starts writing out the sequence in m stages. Each time he either adds a new number to the end of the sequence or takes l first elements of the current sequence and adds them c times to the end. More formally, if we represent the current sequence as a1, a2, ..., an, then after we apply the described operation, the sequence transforms into a1, a2, ..., an[, a1, a2, ..., al] (the block in the square brackets must be repeated c times).
A day has passed and Sereja has completed the sequence. He wonders what are the values of some of its elements. Help Sereja.
The first line contains integer m (1 ≤ m ≤ 105) — the number of stages to build a sequence.
Next m lines contain the description of the stages in the order they follow. The first number in the line is a type of stage (1 or 2). Type 1 means adding one number to the end of the sequence, in this case the line contains integer xi (1 ≤ xi ≤ 105) — the number to add. Type 2 means copying a prefix of length li to the end ci times, in this case the line further contains two integers li, ci (1 ≤ li ≤ 105, 1 ≤ ci ≤ 104), li is the length of the prefix, ci is the number of copyings. It is guaranteed that the length of prefix li is never larger than the current length of the sequence.
The next line contains integer n (1 ≤ n ≤ 105) — the number of elements Sereja is interested in. The next line contains the numbers of elements of the final sequence Sereja is interested in. The numbers are given in the strictly increasing order. It is guaranteed that all numbers are strictly larger than zero and do not exceed the length of the resulting sequence. Consider the elements of the final sequence numbered starting from 1from the beginning to the end of the sequence.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.
Print the elements that Sereja is interested in, in the order in which their numbers occur in the input.
- 6
1 1
1 2
2 2 1
1 3
2 5 2
1 4
16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
- 1 2 1 2 3 1 2 1 2 3 1 2 1 2 3 4
题目大意:
维护一个初始为空的序列,支持两种操作,共 m 个(1 <= m <= 10 ^ 5):
1 将一个数插入到数列的尾端(插入的数不大于10 ^ 5)
2 将数列的前 l 位复制 c 次,贴到数列的尾端(1 <= l <= 10 ^ 5, 1 <= c <= 10 ^ 4)
然后进行 n (1 <= n <= 10 ^ 5)次查询,每次给出一个数 x ,输出最终数列的第x位
- #include <iostream>
- #include <stdio.h>
- #include <string>
- #include <string.h>
- #include <algorithm>
- #include <stdlib.h>
- #include <vector>
- using namespace std;
- typedef long long LL ;
- const int Max_N = ;
- LL Len[Max_N] ;
- int n ;
- int type[Max_N] ;
- LL L_X[Max_N] ,C[Max_N] ;
- int find_id(LL x){ /*二分查找区间段*/
- int Left = ;
- int Right = n ;
- int Mid ,ans ;
- while(Left <= Right){
- Mid = (Left + Right) >> ;
- if(Len[Mid] >= x){
- ans = Mid ;
- Right = Mid - ;
- }
- else
- Left = Mid + ;
- }
- return ans ;
- }
- int answer(LL id){ /*递归查找值*/
- int index = find_id(id) ;
- if(type[index] == )
- return L_X[index] ;
- LL re = id - Len[index - ] ;
- re %= L_X[index] ;
- if(re == )
- re = L_X[index] ;
- return answer(re) ;
- }
- int main(){
- int i , m;
- LL id ;
- scanf("%d",&n) ;
- Len[] = ;
- for(i = ; i <= n ; i++){
- scanf("%I64d",&type[i]) ;
- if(type[i] == ){
- scanf("%I64d",&L_X[i]) ;
- Len[i] = Len[i-] + ;
- }
- else{
- scanf("%I64d%I64d",&L_X[i],&C[i]) ;
- Len[i] = Len[i-] + L_X[i]*C[i] ;
- }
- }
- scanf("%d",&m) ;
- while(m--){
- scanf("%I64d",&id) ;
- printf("%d ",answer(id)) ;
- }
- return ;
- }
Codeforces Round #223 (Div. 2) C的更多相关文章
- Codeforces Round #223 (Div. 2) A
A. Sereja and Dima time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并
题目链接:http://codeforces.com/contest/381/problem/E E. Sereja and Brackets time limit per test 1 secon ...
- Codeforces Round #223 (Div. 2)--A. Sereja and Dima
Sereja and Dima time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #633 (Div. 2)
Codeforces Round #633(Div.2) \(A.Filling\ Diamonds\) 答案就是构成的六边形数量+1 //#pragma GCC optimize("O3& ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
随机推荐
- (转帖)BootStrap入门教程 (三)
上讲回顾:Bootstrap的基础CSS(Base CSS)提供了优雅,一致的多种基础Html页面要素,包括排版,表格,表单,按钮等,能够满足前端工程师的基本要素需求. Bootstrap作为完整 ...
- [转]Python中urllib与urllib2的区别与联系
引用文章1:http://my.oschina.net/u/558071/blog/144792 引用文章2:http://zhuoqiang.me/python-urllib2-usage.html ...
- session.flush加锁测试.
测试结论 1 session.flush (用于提交SQL执行计划. hibernate会给数据库加锁, 执行效果等同于select for update的锁级别.如果是oracle 默认为lock ...
- C++ Socket编程步骤 【转】
sockets(套接字)编程有三种,流式套接字(SOCK_STREAM),数据报套接字(SOCK_DGRAM),原始套接字(SOCK_RAW):基于TCP的socket编程是采用的流式套接字. 服务器 ...
- css之四大类选择器
一.选择器: 关于组合选择器之间的区别: 后代选择器(A B)与子选择器(A>B)之间的区别: 后代选择器: 子选择器: 总结:后代选择器的作用范围是一个元素里面包含的所有元素,即包括的是所有的 ...
- 轻量级开源内存数据库SQLite性能测试
[IT168 专稿]SQLite是一款轻型的数据库,它占用资源非常的低,同时能够跟很多程序语言相结合,但是支持的SQL语句不会逊色于其他开源数据库.它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品 ...
- Func系列3:自定义模块
简介 Func自带的模块已经非常丰富,但在日常系统运维当中,尤其是面对大规模的服务器集群.不同类别的业务平台,次是Func自带的模块或许已经不能满足我们的需求,所以有必要通过自定义模块来填补这块的不足 ...
- HTML标签使用特写
页面定时刷新标签 //页面定时刷新 <meta http-equiv="> Input标记各种特殊用法 <%--禁止鼠标选择内容--%> <input id=& ...
- 【转】SQL SERVER DateTime类型的精度
先看下边的SQL 语句 CREATE TABLE #DateTest( Id INT, SampleDate DATETIME ) INSERT INTO #DateTest VALUES(1,'1 ...
- C#之线程和并发
建议大家对C#撑握的不错的时候,可以去看一些开源项目.走技术这条路,就要耐得住寂寞(群里双休日说要让群主找妹子进群的人必须反思),练好内功.不撑握C#高级知识点,别想看懂优秀的开源项目,更别指望吸收其 ...