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

UVALive - 3942

Remember the Word

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+5;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}
    return x*f;
}
int n,a[N],c[N],x,y;
char op[10];
inline int lowbit(int x){return x&-x;}
inline void build(int n){
    memset(c,0,sizeof(c));
    for(int i=1;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=0;
    for(int i=p;i>0;i-=lowbit(i)) ans+=c[i];
    return ans;
}
int main(){
    int cas=0;
    while((n=read())){
        if(cas!=0) putchar(‘\n‘);
        printf("Case %d:\n",++cas);
        for(int i=1;i<=n;i++) a[i]=read();
        build(n);
        while(true){
            scanf("%s",op);
            if(op[0]==‘E‘) break;
            if(op[0]==‘S‘){
                x=read();y=read();
                y=y-a[x];
                add(x,y);
            }else{
                x=read();y=read();
                printf("%d\n",sum(y)-sum(x-1));
            }
        }
    }
}
时间: 2024-10-21 00:26:52

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

UVAlive - 4329 —— Ping pong 【树状数组】

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13895 #include <iostream> #include <cstdio> #include <cstring> #include <string> using namespace std; const int MAXA = 100000 + 5; int n; int c[MAXA]; int sum(int i) { int

UVALive 4329 Ping pong(树状数组)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13895 题意:一条街上住有n个乒乓选手,每个人都有一个技能值,现在每场比赛需要3个人,两个选手,一个裁判:裁判须住在他们之间,且其技能值必须在两选手之间,求一共能组织多少种比赛. 注意到技能值的范围(1 ≤ ai ≤ 100000),所以我们可以树状数组(O(nlogn))处理得到一个ll数组,ll[x]表示住在x左边的并且技能值小于x的技能值的人数.类似逆着处理

UVALive - 3942 - Remember the Word (Trie树)

UVALive - 3942 Remember the Word Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing that Ray has a p

UVALive 3942 - Remember the Word(DP,数组Trie+指针Trie)

UVALive 3942 - Remember the Word(DP,数组Trie+指针Trie) ACM 题目地址: UVALive 3942 - Remember the Word 题意: 给一些单词,然后给一个长的单词,问有几种方法能组成这个大单词,单词可以重复用. 分析: DP[i]=sum{DP[j} (i<j<len),从后往前求. 本来用数组Trie写得爽爽的,1A了. 发现2s多,不能忍! 然后用指针Trie写了一遍,各种出错,整个人都不好了... 研究了一遍别人代码,发现快

UVALive 3942 Remember the Word 字典树+dp

/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5)和m个单词(每个单词长度最多100).单词都是不同的.该字符串可以由若干个单词组成,问最多有多少种组合方式. 思路:字典树+dp 用字典树处理好m个单词,定义dp[i]表示从i开始的字符串可以由单词组成的方式数. 那么dp[i] += dp[i+j]; j表示某个单词和字符串的[i,i+j-1]匹配

UvaLive 6667 Longest Chain (分治求三元组LIS&amp;树状数组)

题目链接: here 题意: 和hdu4742类似.区别就是一部分三元组是直接给出的.还有一部分是用他给的那个函数生成的.还有就是这里的大于是严格的大于a>b必须ax>bx,ay>by,az>bz. 思路: 思路也和hdu4742here类似.只是有几个比较棘手的问题.现在变成严格大于了.对于y还是很好办的.我们在排序y的时候可以使的标号大的排在前面这样就可以防止y和它一样的更新它了.感觉比较麻烦的是x一样怎么办.这个真没想出什么好办法.就只有x和mid+1的x不一样的建一个树状数

UVALive 6911---Double Swords(贪心+树状数组(或集合))

题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4923 problem  description Last night, Kingdom of Light was attacked by Kingdom of Dark! The queen of Kingdom of Light, Queen Ar, was ca

UVALive 4329 Ping pong (树状数组)

白书上的例题.花了很多时间在找bug上,刚学树状数组,这道题add操作时要注意上限不是n. #include <bits/stdc++.h> using namespace std; #define ll long long const ll maxn = 1e5 + 10; ll C[maxn]; ll n; inline ll lowbit(ll x) { return x & (-x); } ll sum(ll x) { ll ret = 0; while(x > 0) {

bzoj 2527 Meteors - 整体二分 - 树状数组

Description Byteotian Interstellar Union (BIU) has recently discovered a new planet in a nearby galaxy. The planet is unsuitable for colonisation due to strange meteor showers, which on the other hand make it an exceptionally interesting object of st