A Simple Problem with Integers(线段树)
F - A Simple Problem with Integers
Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu
Description
给出了一个序列,你需要处理如下两种询问。
"C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000)。
"Q a b" 询问[a, b]区间中所有值的和。
Input
第一行包含两个整数N, Q。1 ≤ N,Q ≤ 100000.
第二行包含n个整数,表示初始的序列A (-1000000000 ≤ Ai ≤ 1000000000)。
接下来Q行询问,格式如题目描述。
Output
对于每一个Q开头的询问,你需要输出相应的答案,每个答案一行。
Sample Input
- 10 5
- 1 2 3 4 5 6 7 8 9 10
- Q 4 4
- Q 1 10
- Q 2 4
- C 3 6 3
- Q 2 4
Sample Output
- 4
- 55
- 9
- 15
//自己写,又超时了,网上看了别人的才过的,在类里面要增加一个积累量这个数据,这样,增加命令的时候只要将特定区间积累量加起来并且num+总和,不必遍历到每一个叶节点,但是在查询时,还是要释放,因为,可能查的区间,在积累的这个区间只占一部分。
这样节约了时间,好像叫lazy的思想
- #include <stdio.h>
- #include <string.h>
- struct
- {
- int l;
- int r;
- double num;
- double Inc;
- }shu[];
- void Init (int left,int right,int k)
- {
- shu[k].l=left;
- shu[k].r=right;
- shu[k].Inc=;
- if(left==right)
- {
- scanf("%lf",&shu[k].num);
- return;
- }
- int mid=(left+right)/;
- Init(left,mid,*k);
- Init(mid+,right,*k+);
- shu[k].num=shu[*k].num+shu[*k+].num;
- }
- void insert(int left,int right,double c,int k)
- {
- if (shu[k].l==left&&shu[k].r==right)//到某个区间
- {
- shu[k].Inc+=c;
- return;
- }
- shu[k].num+=(right-left+)*c;
- int mid=(shu[k].l+shu[k].r)/;
- if (right<=mid)
- insert(left,right,c,*k);
- else if (left>mid)
- insert (left,right,c,*k+);
- else
- {
- insert(left,mid,c,*k);
- insert(mid+,right,c,*k+);
- }
- }
- double query(int left,int right,int k)
- {
- if (shu[k].l==left&&shu[k].r==right)//
- {
- return shu[k].num+(shu[k].r-shu[k].l+)*shu[k].Inc;
- }
- int mid=(shu[k].l+shu[k].r)/;
- if(shu[k].Inc!=)//将这个区间的积累量释放
- {
- shu[k].num+=(shu[k].r-shu[k].l+)*shu[k].Inc;//自己先加上
- insert(shu[k].l,mid,shu[k].Inc,*k);
- insert(mid+,shu[k].r,shu[k].Inc,*k+);
- shu[k].Inc=;
- }
- if (left>mid) return query(left,right,*k+);
- else if (right<=mid) return query(left,right,*k);
- else
- return query(left,mid,*k)+query(mid+,right,*k+);
- }
- int main()
- {
- int Q;
- int all_p,a,b;
- double c;
- char comend;
- while (scanf("%d%d",&all_p,&Q)!=EOF)
- {
- Init(,all_p,);
- while (Q--)
- {
- getchar();
- scanf("%c",&comend);
- scanf("%d%d",&a,&b);
- if (comend=='Q') printf("%.0lf\n",query(a,b,));
- if (comend=='C')
- {
- scanf("%lf",&c);
- insert(a,b,c,);
- }
- }
- }
- return ;
- }
A Simple Problem with Integers(线段树)的更多相关文章
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...
- Poj 3468-A Simple Problem with Integers 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- A Simple Problem with Integers(线段树,区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 83822 ...
- POJ A Simple Problem with Integers 线段树 lazy-target 区间跟新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 105742 ...
随机推荐
- 每日一个机器学习算法——LR(逻辑回归)
本系列文章用于汇集知识点,查漏补缺,面试找工作之用.数学公式较多,解释较少. 1.假设 2.sigmoid函数: 3.假设的含义: 4.性质: 5.找一个凸损失函数 6.可由最大似然估计推导出 单个样 ...
- EF实体查询出的数据List<T>转DataTable出现【DataSet 不支持 System.Nullable<>】的问题
public static DataTable ToDataTable<T>(this IEnumerable<T> varlist, CreateRowDelegate< ...
- 哈希—— POJ 3349 Snowflake Snow Snowflakes
相应POJ题目:点击打开链接 Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions ...
- 深入分析JavaWeb Item22 -- 国际化(i18n)
一.国际化开发概述 软件的国际化:软件开发时,要使它能同一时候应对世界不同地区和国家的訪问,并针对不同地区和国家的訪问.提供对应的.符合来訪者阅读习惯的页面或数据. 国际化(international ...
- 联想电脑Win8升级win10后Wlan关闭无法开启解决办法
官网下载电源驱动,下载无线网上驱动 开启电脑 按fn+f5 电源管理界面就出来了 把无线网卡打开 就ok了 这样就开启了无线! 如果还不行,可进行如下尝试,希望有所帮助: 1.开机进bios(一般是按 ...
- 利用python批量缩放图片
废话少说,上代码: import matplotlib as mpl mpl.use('Agg') import os import matplotlib.pyplot as plt from sci ...
- leetcode_Power of Two_easy
Given an integer, write a function to determine if it is a power of two. 题目意思:推断某个数是否是2的幂. 方法:直接进行bi ...
- 【Nginx-反向代理server】基础知识(二)之多进程模式
Nginx的多进程模式 nginx在启动后.会有一个master进程和多个worker进程.master进程主要用来管理worker进程,包括:接收来自外界的信号.向各worker进程发送信号,监控w ...
- 在ubuntu下安装ns2-allinone-2.35.tar.gz
1.软件下载 首先先下载ns-allinone-2.35.tar.gz (下载路径http://sourceforge.net/projects/nsnam/files/),将其放到你/home/my ...
- Linux 脚本点滴知识积累
1.以openwrt中的/etc/hotplug.d/button/00-button为例 . /lib/functions.sh do_button () { local button ---- ...