【线段树(单点修改,区间求和)】HDU1166 - 敌军布阵

hdu1166 敌兵布阵,单点修改,区间求和。

【ATTENTION】MAXN要开成节点数的4倍,开得不够会提示TLE。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #define lson l,m,root<<1
 5 #define rson m+1,r,root<<1|1
 6 using namespace std;
 7 const int MAXN=50000*4+500;
 8 int n;
 9 int sum[MAXN];
10
11 void pushUP(int root)
12 {
13     sum[root]=sum[root<<1]+sum[root<<1|1];
14 }
15
16 void build(int l,int r,int root)
17 {
18     if (l==r)
19     {
20         scanf("%d",&sum[root]);
21         return;
22     }
23     int m=(l+r)>>1;
24      build(lson);
25     build(rson);
26     pushUP(root);
27 }
28
29 void update(int p,int delta,int l,int r,int root)
30 {
31     if (l==r)
32     {
33         sum[root]+=delta;
34         return;
35     }
36     int m=(l+r)>>1;
37     if (p<=m) update(p,delta,lson);
38     if (p>m) update(p,delta,rson);
39     pushUP(root);
40 }
41
42 int query(int L,int R,int l,int r,int root)
43 {
44     int result=0;
45     if (l>=L && r<=R)
46     {
47         return sum[root];
48     }
49     int m=(l+r)>>1;
50     if (L<=m) result+=query(L,R,lson);
51     if (R>m) result+=query(L,R,rson);
52     return result;
53 }
54
55 int main()
56 {
57     int t;
58     scanf("%d",&t);
59     for (int kase=0;kase<t;kase++)
60     {
61         cout<<"Case "<<kase+1<<":"<<endl;
62         scanf("%d",&n);
63         build(1,n,1);
64         char s[6];
65         while (scanf("%s",s))
66         {
67             if (s[0]==‘E‘) break;
68             int a,b;
69             scanf("%d%d",&a,&b);
70             if (s[0]==‘A‘) update(a,b,1,n,1);
71             if (s[0]==‘S‘) update(a,-b,1,n,1);
72             if (s[0]==‘Q‘) cout<<query(a,b,1,n,1)<<endl;
73         }
74     }
75     return 0;
76 }
时间: 2024-08-01 10:45:04

【线段树(单点修改,区间求和)】HDU1166 - 敌军布阵的相关文章

Lightoj 1348 Aladdin and the Return Journey (树链剖分)(线段树单点修改区间求和)

Finally the Great Magical Lamp was in Aladdin's hand. Now he wanted to return home. But he didn't want to take any help from the Genie because he thought that it might be another adventure for him. All he remembered was the paths he had taken to reac

树链剖分+线段树 单点修改 区间求和 模板

马上要去西安打邀请赛了,存下板子 首先是vector存图的: #include<bits/stdc++.h> using namespace std; #define ll long long #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define mid int m = (l + r) >> 1 const int M = 2e5+10; int fa[M],dep[M],siz[M],son[M

hdu1394(枚举/树状数组/线段树单点更新&amp;区间求和)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 或 -1, 交换两个不相邻数 a, b, 逆序数 += 两者间大于 a 的个数 - 两者间小于 a 的个数: 所以只要求出初始时的逆序对数,就可以推出其余情况时的逆序对数.对于求初始逆序对数,这里 n 只有 5e3,可以直接暴力 / 树状数组 / 线段树 / 归并排序: 代码: 1.直接暴力 1

Spoj 1716 Can you answer these queries III 线段树 单点修改 区间求最大子段和

题目链接:点击打开链接 == 原来写1的时候已经把更新函数写好了.. #include <cstdio> #include <iostream> #include <algorithm> #include <string.h> #include <math.h> #include <vector> #include <map> using namespace std; #define N 50050 #define Lso

HDOJ--4893--Wow! Such Sequence!【线段树+单点、区间更新】

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4893 题意:给你一个长度n的数列,初始都为0,有三种操作,第一种给第k个位置的数加d,第二种是查询区间 [l , r] 的总和,第三种是使区间 [l , r] 的值改为离它最近的那个斐波那契数的值. 我刚开始用sum数组存储节点的值,第三种操作是个区间更新,但是区间更新的值不一样,我就想当然的搜到最底部的节点来处理更新,果断T了.后来想了想,其实可以在节点上再加一个信息,就是这个值下次进行第三种操作要变

【HDU】1754 I hate it ——线段树 单点更新 区间最值

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 37448    Accepted Submission(s): 14816 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要

hdu 3308 线段树单点更新 区间合并

http://acm.hdu.edu.cn/showproblem.php?pid=3308 学到两点: 1.以区间端点为开始/结束的最长......似乎在Dp也常用这种思想 2.分类的时候,明确标准逐层分类,思维格式: 条件一成立: { 条件二成立: { } else { } } else { 条件二成立: { } else { } } 上面的这种方式很清晰,如果直接想到那种情况iif(条件一 &条件二)就写,很容易出错而且把自己搞乱,或者情况不全,,,我就因为这WA了几次 3.WA了之后 ,

hdu2795(线段树单点更新&amp;区间最值)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一个 h * w 的板子,要在上面贴 n 条 1 * x 的广告,在贴第 i 条广告时要尽量将其靠上贴,并输出其最上能贴在哪个位置: 思路:可以将每行剩余空间大小存储到一个数组中,那么对于当前 1 * x 的广告,只需找到所有剩余空间大于的 x 的行中位置最小的即可: 不过本题数据量为 2e5,直接暴力因该会 tle.可以用个线段树维护一下区间最大值,然后查询时对线段树二分即可: 代码

hdu - 4973 - A simple simulation problem.(线段树单点更新 + 区间更新)

题意:初始序列 1, 2, ..., n,m次操作(1 <= n,m<= 50000),每次操作可为: D l r,将区间[l, r]中的所有数复制一次: Q l r,输出区间[l, r]中同一数字个数的最大值. (0 <= r – l <= 10^8, 1 <= l, r <= 序列元素个数) 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4973 -->>因为区间内数字是依次递增的,所以可以以数字为叶建线段

ZOJ 3632 Watermelon Full of Water(dp+线段树 单点修改)

题意: 一共有n天 每天西瓜售价为dp[i]元 该天的西瓜能吃v[i]天 而且这天如果买了西瓜之前的西瓜就要扔掉 问每天都吃到西瓜的最小花费是多少 思路: 从最后一天开始dp最小花费 并用线段树单点更新来维护 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; #define ll long long #def