UVALive - 3942 Remember the Word[树状数组]
2016-10-14 22:17
453 查看
UVALive - 3942 Remember the Word |
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 ,c ,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)); } } } }
相关文章推荐
- UVALive 3942 - Remember the Word(DP,数组Trie+指针Trie)
- [UVALive3942] Remember the Word && 字符串
- UVALive 3942 Remember the word 字典树 动态规划
- UVaLive 3942 Remember the Word (dp+字典树)
- UVALive - 3942 Remember the Word[Trie DP]
- UVALive 3942 (LA 3492) Remember the Word Trie树 + 记忆化搜索
- UVALive 3942 Remember the Word 前缀树Trie
- UVALive - 3942 Remember the Word
- UVA Live-3942 Remember the Word(trie树入门题)
- UVALIVE 3942 Remember the Word 字典树
- UVALive 3942 Remember the Word(trie + dp)
- UVALive 3942 Remember the Word(字典树 + 简单dp)
- UVALive - 3942 Remember the Word (dp+字典树)
- UVALive 3942 Remember the Word(字典树+dp)
- UVALive - 3942 Remember the Word Trie
- uvalive 3942 - Remember the Word(Trie)
- uvalive 3942 Remember the Word 字典树+dp
- 【Uvalive3942】Remember the Word——字典树+dp
- (trie)UVALive - 3942 Remember the Word
- UVALive - 3942 Remember the Word