• Traffic Jams in the Land(线段树好题)


    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 ≤ n2 ≤ 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
    10
    2 5 3 2 3 5 3 4 2 4
    10
    C 10 6
    A 2 6
    A 1 3
    C 3 4
    A 3 11
    A 4 9
    A 5 6
    C 7 3
    A 8 10
    A 2 5
    Output
    5
    3
    14
    6
    2
    4
    4
    题意:某个国家有(n+1)(n+1)个城市(1n10^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
    ",res);
    102         }
    103     }
    104 return 0;
    105 }
  • 相关阅读:
    Linux 学习 step by step (2)
    公共建筑能耗监测平台的GPRS通讯服务器的开发方法分享
    幸福框架:可扩展的、动态的、万能的 编号生成器
    C++ Data Member内存布局
    .NET程序集强命名删除与再签名技术 源代码剖析
    hdu 2191(多重背包)
    五种情况下会刷新控件状态(刷新所有子FWinControls的显示)——从DFM读取数据时、新增加子控件时、重新创建当前控件的句柄时、设置父控件时、显示状态被改变时
    终于懂了:Delphi消息的Result域出现的原因——要代替回调函数的返回值!(MakeObjectInstance不会帮助处理(接收)消息回调函数的返回值)
    Firemonkey实现Mac OS程序中内嵌浏览器的功能(自己动手翻译,调用苹果提供的webkit框架)
    感悟:市场经济看得就是主观能动性,有则富贵可及,无则无限趋于零
  • 原文地址:https://www.cnblogs.com/1013star/p/9597530.html
Copyright © 2020-2023  润新知