498D Traffic Jams in the Land (线段数)

D. Traffic Jams in the Land
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 q queries 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 c, x, y (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.

Sample test(s)
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段路依次链接,每段路ai的堵塞系数,比如要从 x -> y城市,t=0出发,经过某段路时间 T ,若 T 是 ai的倍数,这条路堵塞,车子会多停在路两端城市一秒,若不是倍数,就一秒到下一个城市。q次询问,1.x -> y ,t=0出发,输出到达时间,2.改变某段路的阻塞系数。
 
思路(by大神):2,3,4,5,6的最小公倍数是60,所以时间是60一个周期,所以我们开60棵线段树,第i棵线段树的区间【L,R】表示我们在第i个时间点(从L出发的时间mod60后)从L出发到达R所需的时间。然后我们就可以在合并区间的时候维护了:sum[i][o] = sum[i][ls] + sum[(i+sum[i][ls])%60][rs]。这个合并方程就是本题的关键了。查询的时候也是用同样的方法合并。

 

代码:

 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std ;

#define ls ( o << 1 )
#define rs ( o << 1 | 1 )
#define lson ls , l , m
#define rson rs , m + 1 , r
#define root 1 , 1 , n
#define rt o , l , r
#define mid ( ( l + r ) >> 1 )

const int N = 100005;
int sum[60][N<<2],a[N];

void pushup (int o , int l , int r)
{
    int m = mid;
    for(int i=0; i<60; i++)
    sum[i][o] = sum[i][ls] + sum[( i + sum[i][ls] ) % 60][rs];
}
void build (int o , int l , int r)
{
    if (l == r)
    {
        for(int i=0; i<60; i++)
        sum[i][o] = 1+(i%a[l]==0);
        return;
    }
    int m = mid;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int x , int v , int o , int l , int r)
{
    if(l == r)
    {
        a[l] = v;
        for(int i=0; i<60; i++)
            sum[i][o] = 1 + (i % v == 0);
        return;
    }
    int m = mid;
    if(x <= m) update(x , v , lson);
    else update(x , v , rson);
    pushup(rt);
}
int query (int L , int R , int x , int o , int l , int r)
{
    if (L <= l && r <= R) return sum[x][o];
    int m = mid;
    if (R <= m) return query ( L , R , x , lson);
    if (m <  L) return query ( L , R , x , rson);
    int tmp = query(L , R , x , lson);
    return tmp + query(L , R , ( x + tmp ) % 60 , rson);
}
int main()
{
    int n,m,x,y;
    char op[5];
    cin >> n;
    for(int i=1;i<=n;i++) cin >> a[i];
    build(root);
    cin >> m;
    while(m--)
    {
        cin >> op;
        cin >> x;cin >> y;
        if(op[0]=='C')
            update (x,y,root);
        else
        cout << query(x,y-1,0,root) << endl;
    }
}
View Code

 

posted @ 2015-02-10 00:25  Doli  阅读(143)  评论(0)    收藏  举报