嘟嘟嘟

题面:有n条公路一次连接着n + 1个城市,每一条公路有一个堵塞时刻a[i],如果当前时间能被a[i]整除,那么通过这条公路需要2分钟;否则需要1分钟。

现给出n条公路的a[i],以及m次操作。每一次操作:1.C x d:将第x条的堵塞时刻改为d。2.A x y:询问从城市x到城市y的所需时间。

这能想到是一个线段树的题,虽然做过好多道线段树的题,但遇到这种思路比较新奇的题,独立的想出来还是有一点困难。

于是稍微参照了一下题解。

我们观察一下a[i],2 <= a[i] <= 6,很小,而且这些数的最小公倍数最大只有60,那么对于每一个节点,我们开一个tim[60]的数组,tim[j]维护在时刻 j ,通过这段区间需要的时间。

那么区间合并的时候就是 j 从0~59枚举,这个区间的第 j 个时刻花费的时间等于左区间的从 j 时刻花费的时间x,加上右区间从j + x时刻花费的时间。于是这题就完事啦。

时间复杂度O(mlogn * 60)。

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 1e5 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int n, m;
char s[]; struct Tree
{
int l, r;
int tim[];
}t[maxn << ];
void pushup(int now)
{
for(int i = ; i < ; ++i)
t[now].tim[i] = t[now << ].tim[i] + t[now << | ].tim[(i + t[now << ].tim[i]) % ];
}
void build(int L, int R, int now)
{
t[now].l = L; t[now].r = R;
if(L == R)
{
int x = read();
for(int i = ; i < ; ++i)
if(!(i % x)) t[now].tim[i] = ;
else t[now].tim[i] = ;
return;
}
int mid = (L + R) >> ;
build(L, mid, now << );
build(mid + , R, now << | );
pushup(now);
}
void update(int idx, int d, int now)
{
if(t[now].l == t[now].r)
{
for(int i = ; i < ; ++i)
if(!(i % d)) t[now].tim[i] = ;
else t[now].tim[i] = ;
return;
}
int mid = (t[now].l + t[now].r) >> ;
if(idx <= mid) update(idx, d, now << );
else update(idx, d, now << | );
pushup(now);
}
int query(int L, int R, int now, int Tim)
{
if(t[now].l == L && t[now].r == R) return t[now].tim[Tim % ];
int mid = (t[now].l + t[now].r) >> ;
if(R <= mid) return query(L, R, now << , Tim);
else if(L > mid) return query(L, R, now << | , Tim);
else
{
int ret = query(L, mid, now << , Tim);
ret += query(mid + , R, now << | , (Tim + ret) % );
return ret;
}
} int main()
{
n = read();
build(, n, );
m = read();
for(int i = ; i <= m; ++i)
{
scanf("%s", s);
if(s[] == 'C')
{
int x = read(), d = read();
update(x, d, );
}
else
{
int L = read(), R = read();
write(query(L, R - , , )); enter; //别忘 R - 1
}
}
return ;
}

CF498D Traffic Jams in the Land的更多相关文章

  1. CF498D:Traffic Jams in the Land——题解

    https://vjudge.net/problem/CodeForces-498D http://codeforces.com/problemset/problem/498/D 题面描述: 一些国家 ...

  2. CF #284 div1 D. Traffic Jams in the Land 线段树

    大意是有n段路,每一段路有个值a,通过每一端路需要1s,如果通过这一段路时刻t为a的倍数,则需要等待1s再走,也就是需要2s通过. 比较头疼的就是相邻两个数之间会因为数字不同制约,一开始想a的范围是2 ...

  3. [codeforces] 498D Traffic Jams in th Land

    原题 简单的线段树问题. 对于题目中,a[i]的范围是2~6,我们仔细思考可以得出第0秒和第60秒是一样的(因为2~6的最小公倍数是60,),然后我们可以建一个线段树,里面记录0~59秒时刻开始通过这 ...

  4. Codeforces 498D Traffic Jams in the Land | 线段树

    题目大意: 给坐标轴1~n的点,每个点有一个权值,从一个点走到下一个点需要1s,如果当前时间是权值的倍数就要多花1s 给出q组操作,C表示单点修改权值,A表示询问0时刻x出发到y的时间 题解:因为权值 ...

  5. Codeforces Round #284 (Div. 1)

    A. Crazy Town 这一题只需要考虑是否经过所给的线,如果起点和终点都在其中一条线的一侧,那么很明显从起点走点终点是不需要穿过这条线的,否则则一定要经过这条线,并且步数+1.用叉积判断即可. ...

  6. CF数据结构练习

    1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...

  7. HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)

    Problem Description City C is really a nightmare of all drivers for its traffic jams. To solve the t ...

  8. Gym 100507I Traffic Jam in Flower Town (模拟)

    Traffic Jam in Flower Town 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/I Description ...

  9. ural 2020 Traffic Jam in Flower Town(模拟)

    2020. Traffic Jam in Flower Town Time limit: 1.0 secondMemory limit: 64 MB Having returned from Sun ...

随机推荐

  1. BFC --- Block Formatting Context --- 块级格式化上下文

    虽然知道块级格式化上下文是什么东西,但要我把这个东西给说清楚,还真的不是一件容易的事儿,所以这篇文章我就要说说清楚到底什么使传说中的BFC,即块级格式化上下文. 一.BFC的通俗理解 通俗的理解 -- ...

  2. GitHub+Hexo+gulp搭建博客网站

    一.前期准备 1.注册GitHub账号. 不做说明 2.创建仓库 创建一个新的仓库来放置我们的文件. 3.下载安装Node.js https://nodejs.org/en/   两个版本,选择右边那 ...

  3. 牛客网Java刷题知识点之数组、链表、哈希表、 红黑二叉树

    不多说,直接上干货! 首先来说一个非常形象的例子,来说明下数组和链表. 上体育课的时候,老师说:你们站一队,每个人记住自己是第几个,我喊到几,那个人就举手,这就是数组. 老师说,你们每个人记住自己前面 ...

  4. 转-------基于R-CNN的物体检测

    基于R-CNN的物体检测 原文地址:http://blog.csdn.net/hjimce/article/details/50187029 作者:hjimce 一.相关理论 本篇博文主要讲解2014 ...

  5. vim配置clojure开发环境备忘录

    1 需要使用的插件 vundle 使用教程 http://www.cnblogs.com/respawn/archive/2012/08/21/2649483.html vim-fireplace h ...

  6. Grafana监控可视化环境搭建

    依赖库Go 1.6NodeJS v4+sqlite3GO 环境搭建 vi /etc/profile export GOPATH="/root/go" export GOROOT=& ...

  7. 九度oj题目1521:二叉树的镜像

    题目1521:二叉树的镜像 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2061 解决:560 题目描述: 输入一个二叉树,输出其镜像. 输入: 输入可能包含多个测试样例,输入以EOF ...

  8. MongoDB集群怎样去访问?

    上一章节简单介绍了MONGODB的集群搭建.相信大家都已经很熟悉了.集群搭建完接下来应该考虑我们的程序应该怎样去访问他. 怎么读写数据等操作.下面把我在工作中的一些用法列出来供大家作为参考. 官网的链 ...

  9. Java使用UDP聊天程序

    主要想测试Java UDP通信.Java UDP使用DatagramSocket和DatagramPacket完成UDP通信 主要思路: 1.本机通信,ip地址为:127.0.0.1 2.开一个线程监 ...

  10. js之方法

    原文 在一个对象中绑定函数,称为这个对象的方法. 在JavaScript中,对象的定义是这样的: var xiaoming = { name: '小明', birth: 1990 }; 但是,如果我们 ...