POJ3468:A Simple Problem with Integers (线段树||树状数组||Splay解决基本问题的效率对比)
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
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
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
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
题意:就是区间更改,区间求和。
思路:常规线段树,也可以树状态数组,也可以splay。
ps:我记得csl蔡神就是经常用splay来做线段树题,我最近发现这真是个好习惯。很大的好处就是写平衡树可以写得很灵活吧。
这里主要是都写一下,然后对比下效率。 也尽量以后多写splay。)
体会:由于splay基本操作后都要把Now节点splay到根节点,可以从这里想办法优化。
比如,不要求在线的时候(比如区间第K大),可以向莫队一样,排序后再回答,这样,每次splay的高度会小一些。
其他优化就待续了,目前平衡树做得少。
树状数组:1969ms:
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=;
ll a[maxn],b[maxn],c[maxn];
char opt[];int n,m;
int lowbit(int x) {return x&(-x);}
void add(ll *bb,ll *cc,int x,int val)
{
ll tmp=x*val;
for(int i=x;i<=n+;i+=lowbit(i))
bb[i]+=val,cc[i]+=tmp;
}
ll query(ll *bb,ll *cc,int x)
{
ll res=;
for(int i=x;i;i-=lowbit(i)) res+=bb[i]; res*=(x+);
for(int i=x;i;i-=lowbit(i)) res-=cc[i];
return res+a[x];
}
int main()
{
while(~scanf("%d%d",&n,&m)){
for(int i=;i<=n;i++) scanf("%lld",&a[i]),a[i]+=a[i-];
for(int i=;i<=n;i++) b[i]=c[i]=;
while(m--){
scanf("%s",opt); int x,y,z;
if(opt[]=='Q')scanf("%d%d",&x,&y),printf("%lld\n",query(b,c,y)-query(b,c,x-));
else scanf("%d%d%d",&x,&y,&z),add(b,c,x,z),add(b,c,y+,-z);
}
} return ;
}
线段树:2407ms:
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn=;
int n,m;int a[maxn];
struct TREE
{
ll sum[maxn<<];int lazy[maxn<<];
void build(int Now,int l,int r)
{
lazy[Now]=;
if(l==r) { sum[Now]=a[l]; return;}
int Mid=(l+r)>>;
build(Now<<,l,Mid);
build(Now<<|,Mid+,r);
pushup(Now);
}
void add(int Now,int l,int r,int x,int y,int val)
{
if(x<=l&&y>=r) { sum[Now]+=(ll)(r-l+)*val;lazy[Now]+=val; return ;}
pushdown(Now,l,r); int Mid=(l+r)>>;
if(y<=Mid) add(Now<<,l,Mid,x,y,val);
else if(x>Mid) add(Now<<|,Mid+,r,x,y,val);
else add(Now<<,l,Mid,x,Mid,val),add(Now<<|,Mid+,r,Mid+,y,val);
pushup(Now);
}
ll query(int Now,int l,int r,int x,int y)
{
if(x<=l&&y>=r) return sum[Now];
pushdown(Now,l,r); int Mid=(l+r)>>;
if(y<=Mid) return query(Now<<,l,Mid,x,y);
else if(x>Mid) return query(Now<<|,Mid+,r,x,y);
else return query(Now<<,l,Mid,x,Mid)+query(Now<<|,Mid+,r,Mid+,y);
pushup(Now);
}
void pushup(int Now) { sum[Now]=sum[Now<<]+sum[Now<<|];}
void pushdown(int Now,int l,int r)
{
int Mid=(l+r)>>;
lazy[Now<<]+=lazy[Now];sum[Now<<]+=(ll)(Mid-l+)*lazy[Now];
lazy[Now<<|]+=lazy[Now];sum[Now<<|]+=(ll)(r-Mid)*lazy[Now];
lazy[Now]=;
}
}Tree;
int main()
{
while(~scanf("%d%d",&n,&m)){
for(int i=;i<=n;i++) scanf("%d",&a[i]);
Tree.build(,,n);
for(int i=;i<=m;i++){
char opt[];int x,y,z;
scanf("%s",opt);
if(opt[]=='Q') scanf("%d%d",&x,&y),printf("%lld\n",Tree.query(,,n,x,y));
else scanf("%d%d%d",&x,&y,&z),Tree.add(,,n,x,y,z);
}
} return ;
}
splay:3891ms。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn=;
int a[maxn];
struct Splay
{
int ch[maxn][],sz[maxn],fa[maxn],rt,cnt;
ll sum[maxn],key[maxn],lazy[maxn];
void init()
{
rt=cnt=;
}
int get(int x)
{
return ch[fa[x]][]==x;
}
void pushdown(int Now)
{
if(!lazy[Now]) return ;
int cl=ch[Now][],cr=ch[Now][];
if(cl){
sum[cl]+=sz[cl]*lazy[Now];
lazy[cl]+=lazy[Now]; key[cl]+=lazy[Now];
}
if(cr){
sum[cr]+=sz[cr]*lazy[Now];
lazy[cr]+=lazy[Now]; key[cr]+=lazy[Now];
}
lazy[Now]=;
}
void update(int Now)
{
sum[Now]=key[Now]; sz[Now]=;
if(ch[Now][]) sum[Now]+=sum[ch[Now][]],sz[Now]+=sz[ch[Now][]];
if(ch[Now][]) sum[Now]+=sum[ch[Now][]],sz[Now]+=sz[ch[Now][]];
}
void rotate(int x)
{
int old=fa[x],fold=fa[old],opt=(ch[old][]==x);
pushdown(old); pushdown(x);
fa[ch[x][opt^]]=old; ch[old][opt]=ch[x][opt^];
ch[x][opt^]=old; fa[old]=x; fa[x]=fold;
if(!fold) rt=x;
else ch[fold][ch[fold][]==old]=x;
update(old); update(x);
}
void splay(int x,int y)
{
for(int f;(f=fa[x])!=y;rotate(x)){
if(fa[f]!=y)
rotate(get(x)==get(f)?f:x);
}
if(!y) rt=x;
}
int build(int L,int R)
{
if(L>R) return -;
int Mid=(L+R)>>;
if(!rt) rt=Mid;
key[Mid]=a[Mid]; sum[Mid]=a[Mid]; sz[Mid]=;
lazy[Mid]=fa[Mid]=ch[Mid][]=ch[Mid][]=;
int cl=build(L,Mid-);
if(cl!=-) {
ch[Mid][]=cl; fa[cl]=Mid; sz[Mid]+=sz[cl];
}
int cr=build(Mid+,R);
if(cr!=-){
ch[Mid][]=cr; fa[cr]=Mid; sz[Mid]+=sz[cr];
}
update(Mid);
return Mid;
}
ll query(int x,int y)
{
splay(x,); splay(y,x);
return sum[ch[ch[rt][]][]];
}
void change(int x,int y,int z)
{
splay(x,); splay(y,x);
int t=ch[ch[rt][]][];
key[t]+=z; lazy[t]+=z;
sum[t]+=sz[t]*z;
}
}S;
int main()
{
int N,Q,x,y,z; char opt[];
while(~scanf("%d%d",&N,&Q)){
S.init();
for(int i=;i<=N;i++) scanf("%d",&a[i+]); a[]=a[N+]=;
S.build(,N+);
while(Q--){
scanf("%s",opt);
if(opt[]=='C'){
scanf("%d%d%d",&x,&y,&z);
S.change(x,y+,z);
}
else {
scanf("%d%d",&x,&y);
printf("%lld\n",S.query(x,y+));
}
}
}
return ;
}
POJ3468:A Simple Problem with Integers (线段树||树状数组||Splay解决基本问题的效率对比)的更多相关文章
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- POJ3468 A Simple Problem with Integers 【段树】+【成段更新】
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 57666 ...
- POJ3468 A Simple Problem with Integers(线段树延时标记)
题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...
- POJ3468 A Simple Problem with Integers —— 线段树 区间修改
题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- 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 [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
随机推荐
- Leetcode 260.只出现一次的数字III
只出现一次的数字III 给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次. 找出只出现一次的那两个元素. 示例 : 输入: [1,2,1,3,2,5] 输出: [3,5 ...
- poj3648,2-sat求解
关键是题意的理解,英语,有时候明明每个字都认识,但是还是理解错误!哎!!悲剧啊!题意啊! 这是关键!开始误理解为n对新娘郞,非也!是只有一对,其他是夫妇,理解后就好做了,建立图 是关键,怎么转化关系, ...
- poj3694+hdu2460 求桥+缩点+LCA/tarjan
这个题使我更深理解了TARJAN算法,题意:无向图,每添加一条边后文桥的数量,三种解法:(按时间顺序),1,暴力,每每求桥,听说这样能过,我没过,用的hash判重,这次有俩个参数(n->10w, ...
- iOS url带中文下载时 报错解决方法
问题描述:下载文件时, 请求带中文的URL的资源时,比如:http://s237.sznews.com/pic/2010/11/23/e4fa5794926548ac953a8a525a23b6f2/ ...
- Construct Binary Tree from Preorder and Inorder Traversal (DFS,参考)
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Go和HTTPS(TLS)
原文链接: http://studygolang.com/wr?u=http%3a%2f%2ftonybai.com%2f2015%2f04%2f30%2fgo-and-https%2f 近期在构思一 ...
- 学习MarkDown--初体验
学习MarkDownPad 突然兴趣来了,在博客园里面也有Markdown的格式,昨天晚上安装了MarkDownPad pro这个是免费的,但是有些功能不支持.本来想破解的,百度了很多方法感觉不靠谱, ...
- Android切图注意事项
1.App Logo大小共五种: 48*48 72*72 96*96 144*144 192*192 2. App启动页所需尺寸: 320×480 480×800 720*1280 1080*1920 ...
- Semaphore使用
Semaphore使用
- apache下配置认证用户
有时候我们须要给我apacheserver下制定的文件夹加上用户认证,方便一些而用户进行文件的浏览.配置例如以下: 1 设置用户 1 htpasswd -c file_path user_name 回 ...