Traffic Jams in the Land

CodeForces - 498D

  Some country consists of (n + 1) cities, located along a straight highway. Let's number the cities with consecutive integers from 1 to n + 1 in the order they occur along the highway. Thus, the cities are connected by n segments of the highway, the i-th segment connects cities number i and i + 1. Every segment of the highway is associated with a positive integer ai > 1 — the period of traffic jams appearance on it.

In order to get from city x to city y (x < y), some drivers use the following tactics.

Initially the driver is in city x and the current time t equals zero. Until the driver arrives in city y, he perfors the following actions:

  • if the current time t is a multiple of ax, then the segment of the highway number x is now having traffic problems and the driver stays in the current city for one unit of time (formally speaking, we assign t = t + 1);
  • if the current time t is not a multiple of ax, then the segment of the highway number x is now clear and that's why the driver uses one unit of time to move to city x + 1 (formally, we assign t = t + 1 and x = x + 1).

You are developing a new traffic control system. You want to consecutively process qqueries of two types:

  1. determine the final value of time t after the ride from city x to city y (x < y) assuming that we apply the tactics that is described above. Note that for each query t is being reset to 0.
  2. replace the period of traffic jams appearing on the segment number x by value y(formally, assign ax = y).

Write a code that will effectively process the queries given above.

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of highway segments that connect the n + 1 cities.

The second line contains n integers a1, a2, ..., an (2 ≤ ai ≤ 6) — the periods of traffic jams appearance on segments of the highway.

The next line contains a single integer q (1 ≤ q ≤ 105) — the number of queries to process.

The next q lines contain the descriptions of the queries in the format cxy (c — the query type).

If c is character 'A', then your task is to process a query of the first type. In this case the following constraints are satisfied: 1 ≤ x < y ≤ n + 1.

If c is character 'C', then you need to process a query of the second type. In such case, the following constraints are satisfied: 1 ≤ x ≤ n, 2 ≤ y ≤ 6.

Output

For each query of the first type output a single integer — the final value of time t after driving from city x to city y. Process the queries in the order in which they are given in the input.

Examples

Input
102 5 3 2 3 5 3 4 2 410C 10 6A 2 6A 1 3C 3 4A 3 11A 4 9A 5 6C 7 3A 8 10A 2 5

Output
53146244题意:某个国家有(n+1)(n+1)个城市(1≤n≤10^5),城市之间有高速公路,其中第ii段高速公路是从城市i通往城市(i+1)的,而且第i条道路有一个属性值ai(2≤ai≤6)表示这段城市的拥堵状态,当我们要从城市x到城市y时候,定义时刻t从0开始,如果当前时刻t是ax的倍数,那么当前车辆就不能行驶,只能停在原地,等待当前这一秒过去(相当于从x到y需要花费2秒),否则花费1秒。现在有两类操作,q(1≤q≤10^5)次查询: 
  • 1 x y查询从城市x到城市y所需要耗费的时间;
  • 2 x y修改第x个城市的拥堵值ax为y。

题解:因为题目给出2 ≤ y ≤ 6,而2-6的最小公倍数是60,相当于第0s进入和第60s进入某一个城市花费的时间相同,所以可以以60为一个周期,对每一个结点创建一个大小为60的时间数组,记录从0-59任何一个时间进入该结点所花费的时间。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 using namespace std;
  6 const int maxn=2e5+10;
  7 struct node{
  8     int l;
  9     int r;
 10     int val;
 11     int t[61];
 12 }e[maxn<<2];
 13 int a[maxn];
 14 void pushup(int cur)//注意!!
 15 {
 16     for(int i=0;i<60;i++)
 17     {
 18         int temp=e[cur<<1].t[i];//左儿子当前时间进入需要花费的时间
 19         int nex=(i+temp)%60;//右儿子需要的时间为左儿子花费的时间加上进入左儿子的初始时间
 20         e[cur].t[i]=temp+e[cur<<1|1].t[nex];
 21     }
 22 }
 23 void build(int l,int r,int cur)
 24 {
 25     e[cur].l=l;
 26     e[cur].r=r;
 27     if(l==r)//对每一个结点创建一个时间数组记录任何时间进入的花费
 28     {
 29         for(int i=0;i<60;i++)
 30         {
 31             if(i%a[l]==0)
 32             {
 33                 e[cur].t[i]=2;
 34             }
 35             else
 36                 e[cur].t[i]=1;
 37         }
 38         return;
 39     }
 40     int mid=(l+r)/2;
 41     build(l,mid,cur<<1);
 42     build(mid+1,r,cur<<1|1);
 43     pushup(cur);
 44 }
 45 void update(int tar,int val,int cur)
 46 {
 47     if(e[cur].l==e[cur].r)
 48     {
 49         a[tar]=val;
 50         for(int i=0;i<60;i++)
 51         {
 52             if(i%a[tar]==0)
 53             {
 54                 e[cur].t[i]=2;
 55             }
 56             else
 57                 e[cur].t[i]=1;
 58         }
 59         return;
 60     }
 61     int mid=(e[cur].l+e[cur].r)/2;
 62     if(tar<=mid)
 63         update(tar,val,cur<<1);
 64     else
 65         update(tar,val,cur<<1|1);
 66     pushup(cur);
 67 }
 68 int query(int pl,int pr,int cur,int t)
 69 {
 70     if(pl<=e[cur].l&&e[cur].r<=pr)
 71     {
 72         return t+e[cur].t[t%60];
 73     }
 74     int mid=(e[cur].l+e[cur].r)/2;
 75     if(pl<=mid)
 76         t=query(pl,pr,cur<<1,t);
 77     if(pr>mid)
 78         t=query(pl,pr,cur<<1|1,t);
 79 return t;
 80 }
 81 int main()
 82 {
 83     int n;
 84     cin>>n;
 85     for(int i=1;i<=n;i++)
 86         scanf("%d",&a[i]);
 87     build(1,n,1);
 88     int q,x,y;
 89     char op[10];
 90     cin>>q;
 91     while(q--)
 92     {
 93         scanf("%s %d%d",op,&x,&y);
 94         if(op[0]=='C')
 95         {
 96             update(x,y,1);
 97         }
 98         if(op[0]=='A')
 99         {
100             int res=query(x,y-1,1,0);
101             printf("%d\n",res);
102         }
103     }
104 return 0;
105 }

转载于:https://www.cnblogs.com/1013star/p/9597530.html

Traffic Jams in the Land(线段树好题)相关推荐

  1. CF498D Traffic Jams in the Land

    嘟嘟嘟 题面:有n条公路一次连接着n + 1个城市,每一条公路有一个堵塞时刻a[i],如果当前时间能被a[i]整除,那么通过这条公路需要2分钟:否则需要1分钟. 现给出n条公路的a[i],以及m次操作 ...

  2. 线段树模板题3:区间染色问题

    1.3线段树模板题3:区间染色问题 在DotA游戏中,帕吉的肉钩实际上是大多数英雄中最恐怖的东西.挂钩由长度相同的几个连续的金属棍组成. 现在,帕吉(Pudge)希望对挂接进行一些操作. 让我们将钩子 ...

  3. J.哭泣的阿木木(线段树模板题)

    哭泣的阿木木 Description 没啥用的背景故事: 在远古的恕瑞玛,有一个孤独而又忧郁的灵魂,阿木木.他在世间游荡,只为找到一个朋友.他遭受了一种远古的巫术诅咒,注定忍受永世的孤单,因为被他触碰 ...

  4. UVA 12086 Potentiometers(线段树裸题)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  5. 【线段树】Traffic Jams in the Land(CF498D)

    正题 luogu CF498D 题目大意 给n个1-6的数,让你进行以下操作: 修改其中一个数 从第x个数走到第y个数(x≤yx\leq yx≤y),到达一个点时,如果当前时间能被该数整除,那么时间+ ...

  6. 树套树 ---- 树状数组套权值线段树模板题 P2617 Dynamic Rankings 动态第K大

    题目链接 题目大意: 给你一个数组aaa,aaa有两个操作 询问aaa中[l,r][l,r][l,r]区间里面第kkk小的数是哪个? 修改axa_xax​为yyy 解题思路: 首先我们知道权值线段树是 ...

  7. poj2823 线段树模板题 点修改(也可以用单调队列)

    这道题吧 没计算时间 因为给了那么多 一算还可以 就直接写了线段树,刘汝佳那本模板 然后!poj的g++比C++慢大约500ms.......g++tle,C++就过了 Sliding Window ...

  8. hdu1156(简单线段树 模板题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  9. 线段树-HDU5737-这题有点神

    HDU5737 题意 [1][1][1]有长度为nnn的序列A,BA,BA,B [2]Q[2]Q[2]Q此操作两种类型 (1,l,r,x)(1,l,r,x)(1,l,r,x)将区间[l,r][l,r] ...

最新文章

  1. 基于Windows配置COLMAP环境
  2. 引入Redis|tensorflow实现 聊天AI--PigPig养成记(3)
  3. wtp-all-in-one-sdk-R-1.5 使用方法简单说明
  4. 橡皮擦_日本推出改邪归正橡皮擦,看得我头顶一凉
  5. oracle 10g 报错:ORA-00257: archiver error. Connect internal only, until freed
  6. 如果linux目录中没有srv,了解linux系统目录,proc,root,sbin,selinux,srv!
  7. pythonsplit函数_Python split()函数如何工作
  8. LightMapping和LightProbe
  9. Python 爬虫实例(10)—— 四行代码实现刷 博客园 阅读数量
  10. java读取配置文件中文乱码
  11. SpringBean生命周期详解
  12. 贪心算法 看这一篇就够了
  13. 10.4. 嗅探工具
  14. H3C路由器配置——动态路由OSPF协议
  15. 在树莓派上配置迅雷远程下载
  16. C++模版与特化与偏特化
  17. Clickhouse MergeTree系列(Replacing、Summing等)表引擎使用说明
  18. JME-java开发3D游戏
  19. VMware安装Ubuntu-18.04.3 Server版本
  20. 《从零开始:机器学习的数学原理和算法实践》chap1

热门文章

  1. 基于Arduino的多功能智能交通信号灯的设计与实现 ---------对盲人语音播报,红灯结束时铃声提醒,信号灯倒计时和闯红灯语音劝阻
  2. w7怎么写html代码,笔记本win7系统使用记事本编辑和运行html代码的方法
  3. 导航地图是怎样绘制出来的?
  4. 谷粒商城项目篇8_分布式高级篇_商城首页、性能压测、优化(Nginx动静分离)
  5. 大众点评cat接入记录
  6. 拼多多进军美国市场是为国内电商人铺路还是强走了最后的蛋糕?
  7. Linux程序设计-3-Linux编程准备知识
  8. 怎么将静图变动图?用这个网站就够了
  9. 互联网岳麓峰会从长沙走向世界
  10. 刷脸开门上班取外卖等都会无处不在