UVALive - 3942 Remember the Word[树状数组]

时间:2020-12-08 02:41:10
UVALive - 3942

A potentiometer, or potmeter for short, is an electronic device with a variable electric resistance. It has two terminals and some kind of control mechanism (often a dial, a wheel or a slide) with which the resistance between the terminals can be adjusted from zero (no resistance) to some maximum value. Resistance is measured in Ohms, and when two or more resistors are connected in series (one after the other, in a row), the total resistance of the array is the sum of the resistances of the individual resistors.

In this problem we will consider an array of N potmeters, numbered 1 to N from left to right. The left terminal of some potmeter numbered x is connected to the right terminal of potmeter x − 1, and its right terminal to the left terminal of potmeter x + 1. The left terminal of potmeter 1 and the right terminal of potmeter N are not connected.

Initially all the potmeters are set to some value between 0 and 1000 Ohms. Then we can do two things:

• Set one of the potmeters to another value.
• Measure the resistance between two terminals anywhere in the array.

Input

The input consists less than 3 cases. Each case starts with N, the number of potmeters in the array,
on a line by itself. N can be as large as 200000. Each of next N lines contains one numbers between 0
and 1000, the initial resistances of the potmeters in the order 1 to N. Then follow a number of actions,
each on a line by itself. The number of actions can be as many as 200000. There are three types of
action:

  • “S x r” - set potmeter x to r Ohms. x is a valid potmeter number and r is between 0 and 1000.

  • “M x y” - measure the resistance between the left terminal of potmeter x and the right terminal

    of potmeter y. Both numbers will be valid and x is smaller than or equal to y.

  • “END” - end of this case. Appears only once at the end of a list of actions.

    A case with N = 0 signals the end of the input and it should not be processed.
    Output

    For each case in the input produce a line ‘Case n:’, where n is the case number, starting from 1.
    For each measurement in the input, output a line containing one number: the measured resistance

    in Ohms. The actions should be applied to the array of potmeters in the order given in the input.
    Print a blank line between cases.

    Warning: Input Data is pretty big (∼ 8 MB) so use faster IO.
    Sample Input

    3
    100
    100
    100
    M11
    M13
    S 2 200
    M12
    S30
    M23
    END
    10
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    M 1 10
    END
    0

    Sample Output

    Case 1:
    100
    300
    300

    200

    Case 2: 55


单点修改和区间查询
改成y等价于+y-a[x]
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N=2e5+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,a[N],c[N],x,y;
char op[];
inline int lowbit(int x){return x&-x;}
inline void build(int n){
memset(c,,sizeof(c));
for(int i=;i<=n;i++){
c[i]+=a[i];
if(i+lowbit(i)<=n) c[i+lowbit(i)]+=c[i];
}
}
inline void add(int p,int v){
a[p]+=v;
for(int i=p;i<=n;i+=lowbit(i)) c[i]+=v;
}
inline int sum(int p){
int ans=;
for(int i=p;i>;i-=lowbit(i)) ans+=c[i];
return ans;
}
int main(){
int cas=;
while((n=read())){
if(cas!=) putchar('\n');
printf("Case %d:\n",++cas);
for(int i=;i<=n;i++) a[i]=read();
build(n);
while(true){
scanf("%s",op);
if(op[]=='E') break;
if(op[]=='S'){
x=read();y=read();
y=y-a[x];
add(x,y);
}else{
x=read();y=read();
printf("%d\n",sum(y)-sum(x-));
}
}
}
}
 

UVALive - 3942 Remember the Word[树状数组]的更多相关文章

  1. UVALive 4329 Ping pong(树状数组)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13895 题意:一条街上住有n个乒乓选手,每个人都有一个技能值,现在 ...

  2. UVALive 6947 Improvements(DP&plus;树状数组)

    [题目链接] https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=sho ...

  3. UVALive 3942 Remember the Word 字典树&plus;dp

    /** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...

  4. UVALive 6911---Double Swords&lpar;贪心&plus;树状数组(或集合)&rpar;

    题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  5. UVALive 2191 Potentiometers (树状数组)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  6. UvaLive 6667 Longest Chain &lpar;分治求三元组LIS&amp&semi;amp&semi;树状数组&rpar;

    题目链接: here 题意: 和hdu4742类似.差别就是一部分三元组是直接给出的.另一部分是用他给的那个函数生成的.还有就是这里的大于是严格的大于a>b必须ax>bx,ay>by ...

  7. UVALive - 4329 Ping pong 树状数组

    这题不是一眼题,值得做. 思路: 假设第个选手作为裁判,定义表示在裁判左边的中的能力值小于他的人数,表示裁判右边的中的能力值小于他的人数,那么可以组织场比赛. 那么现在考虑如何求得和数组.根据的定义知 ...

  8. UVALive 6911 Double Swords 树状数组

    Double Swords 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8 ...

  9. UVALive 4329 树状数组第二题

    大白书上的题目,比较巧妙的是其分析,为了求某个i点做裁判的时候的情况数,只要知道左边有多少比它小的记为ansc,右边有多少比它小的记为ansd,则总种数,必定为 ansc*(右边总数-ansd)+an ...

随机推荐

  1. Docker命令详解

    Docker命令详解   最近学习Docker,将docker所有命令实验了一番,特整理如下: # docker --help Usage: docker [OPTIONS] COMMAND [arg ...

  2. spring整合hibernate的详细步骤

    Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...

  3. 数据结构作业——order(二叉树遍历)

    order Description 给出一棵二叉树的中序遍历和每个节点的父节点,求这棵二叉树的先序和后 序遍历. Input 输入第一行为一个正整数 n 表示二叉树的节点数目, 节点编号从 1 到 n ...

  4. epoch iteration batchsize

    深度学习中经常看到epoch. iteration和batchsize,下面按自己的理解说说这三个的区别: (1)batchsize:批大小.在深度学习中,一般采用SGD训练,即每次训练在训练集中取b ...

  5. C&num;--简单的串口通信程序

    前几天做毕业设计,其中要用到串口和下位机进行通信,于是自己捣鼓了一个简单的串口通信程序. 在做通信之前要先弄一个SerialPort组件出来,当然也可以通过程序来创建.本次设计中采用的是拖的winfo ...

  6. tengine2&period;2&period;3报错502的The proxy server received an invalid response from an upstream server问题处理

    tengine2.2.3报错502的The proxy server received an invalid response from an upstream server问题处理 现象:访问订单的 ...

  7. oracle数据库备份和恢复

    参考地址:https://www.cnblogs.com/1175429393wljblog/p/9529334.html Oracle数据导入导出imp/exp 在cmd的dos命令提示符下执行,而 ...

  8. 数据库设计 Step by Step &lpar;2&rpar;——数据库生命周期

    引言:数据库设计 Step by Step (1)得到这么多朋友的关注着实出乎了我的意外.这也坚定了我把这一系列的博文写好的决心.近来工作上的事务比较繁重,加之我期望这个系列的文章能尽可能的系统.完整 ...

  9. CentOS下KVM网卡设置成网桥时获取镜像端口的流量

    首先,网桥配置好之后就能实现一个简单的交换机,而交换机的特点就是MAC地址学习,那么KVM的网卡设置成网桥之后,也就是相当于连接到了交换机上. 此时如果要实现在二层交换机或三层交换机做端口镜像,并把这 ...

  10. 关于pycharm导入其他项目时出现找不到python无法运行的问题

    之前拿到一个别的人用scrapy写的一个爬虫想运行看看,然后就出了类似于这种错误(类似的,这个是网上找的),一直提示找不到XXX路径下的python,然后无法运行执行文件... 我一看这个简单,这种就 ...