A Simple Problem with Integers
http://poj.org/problem?id=3468
http://acm.hdu.edu.cn/showproblem.php?pid=4267
C++版本一
/*
*@Author: STZG
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q;
int ans,cnt,flag,temp;
int a[N];
char str[3];
ll lazy[N<<2];
ll tree[N<<2];
void pushup(int rt){tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
void pushdown(int llen,int rlen,int rt){tree[rt<<1]+=lazy[rt]*llen;tree[rt<<1|1]+=lazy[rt]*rlen;lazy[rt<<1]+=lazy[rt];lazy[rt<<1|1]+=lazy[rt];lazy[rt]=0;
}
void build(int l,int r,int rt){if(l==r){scanf("%lld",&tree[rt]);return;}int mid=(l+r)>>1;build(l,mid,rt<<1);build(mid+1,r,rt<<1|1);pushup(rt);}
void updata(int l,int r,int rt,int L,int R,int C){if(l>=L&&r<=R){lazy[rt]+=C;tree[rt]+=C*(r-l+1);return;}int mid=(l+r)>>1;pushdown(mid-l+1,r-mid,rt);if(L<=mid)updata(l,mid,rt<<1,L,R,C);if(R>mid)updata(mid+1,r,rt<<1|1,L,R,C);pushup(rt);
}
ll query(int l,int r,int rt,int L,int R){if(l>=L&&r<=R){return tree[rt];}int mid=(l+r)>>1;pushdown(mid-l+1,r-mid,rt);ll res=0;if(L<=mid)res+=query(l,mid,rt<<1,L,R);if(R>mid)res+=query(mid+1,r,rt<<1|1,L,R);return res;
}
int main()
{
#ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout);
#endifscanf("%d%d",&n,&q);//scanf("%d",&t);//while(t--){}build(1,n,1);int l,r;while(q--){scanf("%s %d %d",str,&l,&r);if(str[0]=='Q')cout<<query(1,n,1,l,r)<<endl;else{scanf("%d",&t);updata(1,n,1,l,r,t);}}//cout << "Hello world!" << endl;return 0;
}
C++版本二
#include <cstdio>typedef long long LL;const int maxn =1e5+5;
LL a[2][maxn];
LL psum[maxn];
int n;inline int lowbit(int x)
{return x&-x;
}
void add(LL a[],int x,int d)
{while(x<=n){a[x]+=d;x+=lowbit(x);}
}
LL sum(LL a[],int x)
{LL ret=0;while(x){ret+=a[x];x-=lowbit(x);}return ret;
}LL query(int x)
{return sum(a[1],x)*x+sum(a[0],x);
}int main()
{int q;scanf("%d%d",&n,&q);for(int i=1;i<=n;i++){scanf("%I64d",&psum[i]);psum[i]+=psum[i-1];}char op[3];while(q--){int l,r;scanf("%s%d%d",op,&l,&r);if(op[0]=='Q')printf("%I64d\n",query(r)-query(l-1)+psum[r]-psum[l-1]);else{int c;scanf("%d",&c);add(a[1],l,c);add(a[1],r,-c);add(a[0],l,c*(-l+1));add(a[0],r,c*r);}}
}
A Simple Problem with Integers相关推荐
- 【线段树】【模板】讲解 + 例题1 HDU - 1754 I Hate It (点修改分数)+ 例题二 POJ - 3468 A Simple Problem with Integers(区间加值)
[线段树][模板]讲解 + 例题1 HDU - 1754 I Hate It (点修改分数)+ 例题二 POJ - 3468 A Simple Problem with Integers(区间加值) ...
- poj 3243:A Simple Problem with Integers
3243:A Simple Problem with Integers 查看 提交 统计 提示 提问 总时间限制: 5000ms 单个测试点时间限制: 2000ms 内存限制: 131072 ...
- poj3468 A Simple Problem with Integers
http://acm.hust.edu.cn:8080/judge/problem/viewProblem.action?id=14607 题意:题目给你n个数,m个操作,接下来一行给你这n个数,接下 ...
- (线段树模板)A Simple Problem with Integers --POJ--3468
链接: http://poj.org/problem?id=3468 代码: 1 #include<stdio.h> 2 #include<algorithm> 3 #incl ...
- POJ3468 A Simple Problem with Integers【线段树 成段更新+求和 lazy标志】
用longlong替换__int64也成. #define LL long long 输入输出用%lld Problem: 3468 User: qq1203456195 Memory: 4284 ...
- poj 3468 A Simple Problem with Integers(线段树区区)
题目链接: http://poj.org/problem?id=3468 题目大意: 给出N个数,和M次查询 C a b c 区间[a,b]的值都加上c Q a b 查询区间[a,b]值 ...
- A Simple Problem with Integers POJ - 3468(线段树+区间查询+区间修改+建树+懒惰标记模板)+(树状数组)
题意: 有一个数组,有两种操作.1: Q a b 求[a,b]的和 2:C a b c 给[a,b] 的所有元素都加上c. 题目: You have N integers, A1, A2, ... , ...
- POJ 3468 A Simple Problem with Integers(线段树:区间更新)
http://poj.org/problem?id=3468 题意: 给出一串数,每次在一个区间内增加c,查询[a,b]时输出a.b之间的总和. 思路: 总结一下懒惰标记的用法吧. 比如要对一个区间范 ...
- 上海大都会 H.A Simple Problem with Integers
题目描述 You have N integers A1, A2, ... , AN. You are asked to write a program to receive and execute t ...
最新文章
- NCBI下载SRA数据的4种方法
- [Android] Git组件化部署
- Windows平台:Nginx+Tornado部署Flask
- 清华大学王晨阳:轻量级Top-K推荐框架及相关论文介绍
- 企业版Java EE正式易主 甲骨文再次放手
- 从零开始学keras之变分自编码器生成图像
- POJ-3469	Dual Core CPU 最小割最大流
- python进阶到高阶大全(强烈推荐)
- Seafile搭建以及onlyoffice在线办公
- 视频流媒体服务器智能云终端如何快速获取直播流地址?
- 树莓派入门(八)--树莓派USB触摸屏校准及旋转屏幕方向后校准》
- 从集显到核显!Intel的显卡技术发展史
- Web自动化【9】——生成HTML格式的report,并邮件发送
- nuc7 android tv,NUC7PJYH HDMI在特定显示屏上出现问题
- java初始化含十个数的数组_java初始化数组的方法
- Redis入门指南之复制
- 【大赛八卦】2020国际初中生信息学竞赛中国代表队蝉联团体总分第一和金牌人数第一
- 51Nod-TalkingData数据科学精英夏令营挑战赛-B-丢手绢
- 收藏 | 美国数字资产行政命令中文版全文
- Golang 必知必会Go Mod命令
热门文章
- java订单编号生产代码,java 订单编号 生成器,可用于生产环境
- mysql illegal mix of_mysql字符集问题:Illegal mix of collations
- python列表常用方法_第24p,必须掌握,列表的常用方法
- 手机刷的面具是什么_从刷入到跑路-Magisk(面具)刷入使用教程
- 翻译职称计算机能力,2018年职称计算机word2003考点辅导:用好Office2003中的翻译功能...
- mysql5.5的方言_mysql方言问题
- 更新r语言_【R语言学习最佳资料之一】R小抄速查表精简更新版
- mysql online ddl和pt_online ddl与pt-osc详解
- html偷拍代码,一段植入木马的html代码
- WebService传输SoapObject数据出现 org.ksoap2.serialization.SoapObject cannot be cast to java.util.Vector的问题