国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

[hdu1166]線段樹模板

2019-11-08 02:29:15
字體:
供稿:網(wǎng)友
#include <cstdio>#include <string>using namespace std;#define lson rt << 1#define rson rt << 1 | 1const int maxn = 5e4 + 1;int st[200010];//數(shù)組開的太小會TLEstring a;void build(int l,int r,int rt){int m = l + r >> 1;if(l == r){scanf("%d",st+rt);return;}build(l, m, lson);build(m+1, r, rson);st[rt] = st[lson] + st[rson];return;}int que(int L, int R, int l, int r,int rt){int m = l + r >> 1;int ans;if(l == r)return st[rt];if(L <= l && R >= r)return st[rt];if(R <= m)return que(L,R,l,m,lson);if(L > m)return que(L,R,m+1,r,rson);else ans = que(L,R,m+1,r,rson) + que(L,R,l,m,lson);return ans;}void update(int p,int temp,int l, int r,int rt){int m = l + r >> 1;if(l == r && l == p){st[rt] += temp;return ;}if(m < p)update(p,temp, m+1, r, rson);else update(p, temp, l, m, lson);st[rt] = st[lson] + st[rson];return;}int main(){int t;int cas = 1;// freopen("test.in","r",stdin);scanf("%d", &t);while(1){bool ok = true;char q[5];int n;scanf("%d", &n);build(1,n,1);while(1){scanf("%s", q);if(q[0] == 'E')break;else if(q[0] == 'Q'){int le,ri,ans;if(ok){PRintf("Case %d:/n",cas);ok = false;}scanf("%d %d", &le, &ri);ans = que(le, ri ,1 ,n ,1);printf("%d/n",ans);}else{int cur,num;scanf("%d %d", &cur, &num);update(cur,q[0] == 'S'?-1*num:num,1,n,1);}}if(cas++ == t)break;}return 0;}

還可以用樹狀數(shù)組寫,代碼簡介了很多

#include <cstdio>#include <cstring>using namespace std;const int maxn = 5e4 + 1;int c[maxn],n;int lowbit(int x){	return x&(-x);}int sum(int x){	int ans = 0;	while(x > 0){		ans += c[x];		x -= lowbit(x);	}	return ans;}int update(int x,int add){	while(x <= n){		c[x] += add;		x += lowbit(x);	}	return 0;}int main(){	int t;	int cas = 1;//	freopen("test.in","r",stdin);	scanf("%d", &t);	while(1){		int a;		bool ok = true;		char q[5];		memset(c, 0, sizeof(c));		scanf("%d", &n);		for(int i = 1; i <= n; i++){			scanf("%d",&a);			update(i,a);		}		while(1){			scanf("%s", q);			if(q[0] == 'E')break;			else if(q[0] == 'Q'){				int le,ri,ans;				if(ok){					printf("Case %d:/n",cas);					ok = false;				}				scanf("%d %d", &le, &ri);				ans = sum(ri) - sum(le-1);				printf("%d/n",ans);			}			else{				int cur,num;				scanf("%d %d", &cur, &num);				update(cur,q[0] == 'S'?-1*num:num);			}		}		if(cas++ == t)break;	}	return 0;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 正阳县| 蒙自县| 乐至县| 镇坪县| 菏泽市| 霍州市| 江孜县| 兴和县| 始兴县| 峨眉山市| 宜丰县| 如东县| 定州市| 来宾市| 永安市| 基隆市| 澎湖县| 南宁市| 繁昌县| 栾川县| 永安市| 南陵县| 湛江市| 赤峰市| 大埔县| 惠安县| 博兴县| 青阳县| 望江县| 渝北区| 教育| 区。| 平武县| 手游| 通河县| 湛江市| 铅山县| 丽水市| 高青县| 玛沁县| 庆阳市|