UVA 12086 Potentiometers(树状数组|| 线段树单点更新)
2017-08-08 19:06
423 查看
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 an
e2e6
other 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
200
Case 2: 55
题意:
很水的线段树单点更新和区间求和,我直接用树状数组做了
代码:
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 an
e2e6
other 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
题意:
很水的线段树单点更新和区间求和,我直接用树状数组做了
代码:
#include<algorithm> #include<iostream> #include<cstring> #include<stdio.h> #include<math.h> #include<string> #include<stdio.h> #include<queue> #include<stack> #include<map> #include<deque> #define M (t[k].l+t[k].r)/2 #define lson k*2 #define rson k*2+1 #define ll long long using namespace std; int sum[200005]; int n; int lowbit(int x) { return x&(-x); } void update(int x,int v)//日常更新 { while(x<=200001)//这里+1是因为有要-1的操作,放在在查询时x=0无限循环,所以所有边界和值都+1 { sum[x]+=v; x+=lowbit(x); } } int query(int x)//日常查询 { int s=0; while(x>0) { s+=sum[x]; x-=lowbit(x); } return s; } int main() { int i,j,m,x,y; int ans=1; char s[10]; while(scanf("%d",&n)!=EOF&&n) { memset(sum,0,sizeof(sum)); for(i=1;i<=n;i++) { scanf("%d",&x); update(i+1,x);//防止出问题全部+1 } if(ans!=1) printf("\n"); printf("Case %d:\n",ans); ans++; while(scanf("%s",s)!=EOF) { if(s[0]=='M') { scanf("%d%d",&x,&y); x++; y++; printf("%d\n",query(y)-query(x-1));//因为这里要-1,所以所有+1,查询[x,y]和相当于[0,y]的和减去[0,x]的和 } else if(s[0]=='S') { scanf("%d%d",&x,&y); x++; int t=query(x)-query(x-1);//先减去原来的值,再加上新的值 update(x,-t); update(x,y); } else break; } } return 0; }
相关文章推荐
- POJ 2828 Buy Tickets(线段树 树状数组/单点更新)
- HDU 1166 敌兵布阵(树状数组||线段树单点更新)
- hdoj 1541 Stars 【树状数组 线段树】【单点更新 区间求和】
- UVa 12086 Potentiometers 树状数组裸题 单点更新 区间查询
- UVA 12086 - Potentiometers(树状数组)
- hdu4893 Wow! Such Sequence!,树状数组,线段树,单点修改,区间更新
- 树状数组、线段树模板(一)——单点更新 + HDU 1166 敌兵布阵
- POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)
- hdu 1166 线段树与树状数组入门 单点更新
- uva 12086 - Potentiometers(树状数组)
- hdu 1166 敌兵布阵 单点更新 树状数组 线段树 两种做法
- POJ - 2155 Matrix (二维树状数组 + 区间修改 + 单点求值 或者 二维线段树 + 区间更新 + 单点求值)
- Hdu 1166 敌兵布阵 树状数组 或 线段树 单点更新,区间求和
- POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)
- UVA 12086 Potentiometers(树状数组)
- NBOJv2 1050 Just Go(线段树/树状数组区间更新单点查询)
- UVA12086 树状数组 + 裸 +单点重置
- hdoj 1166 敌兵布阵 【单点更新+区间求和】 【线段树】 【树状数组】
- POJ 2352 Stars(树状数组||线段树单点更新)
- UVa12086 - Potentiometers(树状数组即Fenwick树)