NYOJ 1068 ST(线段树之 成段更新+区间求和)

ST

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 1
描述

“麻雀”lengdan用随机数生成了后台数据,但是笨笨的他被妹纸的问题给难住了。。。

已知lengdan生成了N(1=<N<=10005)个随机整数,妹子对这些数可能有以下几种操作或询问:

1,A a b c 表示给区间a到b内每个数都加上c;

2,S a b  表示输出区间a到b内的和;

3,Q a b 表示区间a到b内的奇数的个数;

为了使妹纸不口渴,所以我们决定妹纸的询问次数少一点,即(1=<M<=10000,M为询问次数)。

输入
多组测试数据。
每组测试数据第一行包含两个数N,M,表示N个整数,执行M次询问或操作。
紧接着一行输入N个整数,输入数据保证在int范围内。
接下来M行,每行输入一种操作。
输出
每次对于操作2和3,输出结果。
样例输入
5 5
1 2 3 4 5
Q 1 4
S 1 5
A 1 4 1
S 1 5
Q 2 5
样例输出
2
15
19
3
简单的线段树成段更新+区间求和,只是附加了求区间内的奇数个数。由于之前没有写过线段树成段更新的题目,导致因为一条向下更新的语句忘记写,调了一个上午才发现错误。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 10010;
#define lson l, mid, root<<1
#define rson mid+1, r, root<<1|1
struct node
{
    int l, r;
    LL sum, odd, add;
}a[N<<2];
void PushUp(int root)
{
    a[root].sum = a[root<<1].sum + a[root<<1|1].sum;
    a[root].odd = a[root<<1].odd + a[root<<1|1].odd;
}
void PushDown(int len, int root)
{
    if(a[root].add)
    {
        a[root<<1].add += a[root].add;
        a[root<<1|1].add += a[root].add;
        a[root<<1].sum += LL(len - (len>>1)) * a[root].add;
        a[root<<1|1].sum += LL(len>>1) * a[root].add;
        if(a[root].add % 2 == 1)
        {
            a[root<<1].odd = len - (len>>1) - a[root<<1].odd;  
            a[root<<1|1].odd = (len>>1) - a[root<<1|1].odd; 
        }
        a[root].add = 0;
    }
}
void build_tree(int l, int r, int root)
{
    a[root].l = l;
    a[root].r = r;
    a[root].add = 0;
    a[root].odd = 0;
    if(l == r)
    {
        scanf("%lld",&a[root].sum);
        if(a[root].sum % 2 == 1)
            a[root].odd = 1;
        return;
    }
    int mid = (l + r) >> 1;
    build_tree(lson);
    build_tree(rson);
    PushUp(root);
}
void update(int l, int r, int root, LL k)
{
    if(l <= a[root].l && r >= a[root].r)
    {
        a[root].add += k;
        a[root].sum += LL(a[root].r - a[root].l + 1) * k;
        if(k % 2 == 1)
            a[root].odd = (a[root].r - a[root].l + 1) - a[root].odd;
        return;
    }
    PushDown(a[root].r - a[root].l + 1, root);
    int mid = (a[root].l + a[root].r) >> 1;
    if(l <= mid) update(l, r, root<<1, k);
    if(r > mid) update(l, r, root<<1|1, k);
    PushUp(root);
}
LL Query(int l, int r, int root, char ch)
{
    if(l <= a[root].l && r >= a[root].r)
    {
        if(ch == 'Q') return a[root].odd;
        else if(ch == 'S') return a[root].sum;
    }
    PushDown(a[root].r - a[root].l + 1, root);
    LL ans = 0;
    int mid = (a[root].l + a[root].r) >> 1;
    if(l <= mid) ans += Query(l, r, root<<1, ch);
    if(r > mid) ans += Query(l, r, root<<1|1, ch);
    return ans;
}
int main()
{
    int n, m, i, x, y;
    LL z;
    char ch[5];
    while(~scanf("%d%d",&n,&m))
    {
        build_tree(1, n, 1);
        while(m--)
        {
            scanf("%s",ch);
            if(ch[0] == 'A')
            {
                scanf("%d%d%lld",&x,&y,&z);
                update(x, y, 1, z);
            }
            else
            {
                scanf("%d%d",&x,&y);
                printf("%lld\n", Query(x, y, 1, ch[0]));
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
x1y2 x2y3 x3y1-x1y3-x2y1-x3y2 是计算三角形面积的公式中的一部分。 在这个公式中,x1、x2、x3分别表示三角形的三个顶点的x坐标,y1、y2、y3分别表示三角形的三个顶点的y坐标。通过计算这个表达式的值,可以得到三角形的面积。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [TetraCluster:使用并行Java 2库的Java并行程序。 该程序在群集并行计算机上运行,​​以从给定的点集中找到...](https://download.csdn.net/download/weixin_42171208/18283141)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [线性代数有个题,求正交变换x=Qy,化二次型f(x1,x2,x3)=8x1x2+8x1x3+8x2x3为标准型求出特征值](https://blog.csdn.net/weixin_39956182/article/details/115882118)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [nyoj-67-三角形面积(S=(1/2)*(x1y2+x2y3+x3y1-x1y3-x2y1-x3y2))](https://blog.csdn.net/weixin_30492601/article/details/99541033)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值