[BZOJ3212][POJ3468]A Simple Problem with Integers
题目大意:
维护一个长度为$n(n\leq100000)$的数列,支持区间加、区间求和两种操作,操作共$m(m\leq100000)$次。
思路:
Splay区间操作。
#include<cstdio>
#include<cctype>
typedef long long int64;
inline int getint() {
register char ch;
register bool neg=false;
while(!isdigit(ch=getchar())) if(ch=='-') neg=true;
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return neg?-x:x;
}
inline char getalpha() {
register char ch;
while(!isalpha(ch=getchar()));
return ch;
}
const int N=;
class SplayTree {
private:
int par[N],ch[N][],size[N],root;
int64 val[N],sum[N],tag[N];
void push_down(const int &p) {
if(ch[p][]) {
tag[ch[p][]]+=tag[p];
val[ch[p][]]+=tag[p];
sum[ch[p][]]+=tag[p]*size[ch[p][]];
}
if(ch[p][]) {
tag[ch[p][]]+=tag[p];
val[ch[p][]]+=tag[p];
sum[ch[p][]]+=tag[p]*size[ch[p][]];
}
tag[p]=;
}
void push_up(const int &p) {
size[p]=size[ch[p][]]+size[ch[p][]]+;
sum[p]=sum[ch[p][]]+sum[ch[p][]]+val[p];
}
void rotate(const int &x) {
const int y=par[x],z=par[y];
push_down(y),push_down(x);
const int b=x==ch[y][];
par[ch[x][b]=par[ch[y][!b]=ch[x][b]]=y]=x;
par[ch[z][ch[z][]==y]=x]=z;
push_up(y),push_up(x);
}
void splay(int x,const int &goal) {
for(register int y=par[x],z=par[y];y!=goal;rotate(x),z=par[y=par[x]]) {
if(z!=goal) rotate((x==ch[y][])^(y==ch[z][])?x:y);
}
if(!goal) root=x;
}
int find(int x) {
for(register int y=root;;y=ch[y][size[ch[y][]]+<x]) {
push_down(y);
if(par[y]&&y==ch[par[y]][]) x-=size[ch[par[y]][]]+;
if(size[ch[y][]]+==x) return y;
}
}
public:
void build(const int &n) {
for(register int i=;i<=n+;i++) {
par[ch[i][]=i+]=i;
if(i&&i<=n) val[i+]=getint();
}
size[n+]=;
splay(n+,);
}
void modify(const int &l,const int &r,const int &x) {
splay(find(l),);
splay(find(r+),root);
tag[ch[ch[root][]][]]+=x;
val[ch[ch[root][]][]]+=x;
sum[ch[ch[root][]][]]+=x*size[ch[ch[root][]][]];
}
int64 query(const int &l,const int &r) {
splay(find(l),);
splay(find(r+),root);
return sum[ch[ch[root][]][]];
}
};
SplayTree t;
int main() {
const int n=getint(),m=getint();
t.build(n);
for(register int i=;i<m;i++) {
const int opt=getalpha(),l=getint(),r=getint();
if(opt=='C') t.modify(l,r,getint());
if(opt=='Q') printf("%lld\n",t.query(l,r));
}
return ;
}
[BZOJ3212][POJ3468]A Simple Problem with Integers的更多相关文章
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...
- poj------(3468)A Simple Problem with Integers(区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 60745 ...
- POJ3468 A Simple Problem with Integers 【段树】+【成段更新】
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 57666 ...
- bzoj3212 Pku3468 A Simple Problem with Integers 线段树
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2046 So ...
- BZOJ3212: Pku3468 A Simple Problem with Integers(线段树)
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2530 So ...
- poj3468 A Simple Problem with Integers (树状数组做法)
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
- POJ3468 A Simple Problem with Integers —— 线段树 区间修改
题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...
随机推荐
- Diycode开源项目 MainActivity分析
1.分析MainActivity整体结构 1.1.首先看一下这个界面的整体效果. 1.2.活动源代码如下 /* * Copyright 2017 GcsSloop * * Licensed under ...
- android json 解析 kotlin
前面 写了一次 kotlin解析json 但是,真的写得太烂,直接删掉了,现在重新整理一下.顺便记录一下今天坑了我很久的小问题. 1.首先从最简单的入手吧 一个json的字符串:=====就叫做jso ...
- mysql进阶二
数据库存储数据的特点: 1.数据存放到表中,然后表再放到库中 2.一个库中可以有多张表,每张表具有唯一的表名来标识自己 3.表中有一个或多个列,列又称为“字段” 数据库常见的管理系统 mysql.or ...
- App 设计技巧
http://www.360doc.com/content/14/1120/18/21412_426730809.shtml http://veryui.diandian.com/post/2013- ...
- centOS如何设置时间同步
1.进入系统-管理-时间和日期 2.这个需要root权限才能进行设置,在弹出框中填入root密码 3.设置时间和日期-勾选同步,并且选择NTP时间服务器,点击确定 4.选择时区为亚洲上海点击保存 ...
- Appium Windows服务端GUI详解
Appium Windows服务端GUI各项的解释,从官方扒过来的,界面图标和最新版本有点不太一样,其他还是比较简单易懂的 原文https://github.com/appium/appium-dot ...
- day02_04.算算多少人
第四题 算算有多少人? 第二题的升级版,看看你能不能做出来 利用你的编程思想去看这道题目,记住不要放过题目的每一个小细节 题目:操场上100多人排队,3人一组多1人,4个一组多2人,5人一组多3人,共 ...
- 聊聊、Tomcat中文乱码和JVM设置
set JAVA_OPTS=%JAVA_OPTS% -server -Xms512m -Xmx512m -Dfile.encoding=GBK -Dsun.jnu.encoding=GBK
- idea热部署设置(复制)
提出问题 IntelliJ IDEA工具如何设置热部署??? 解决问题 我的IDEA的版本是:IntelliJ IDEA 14.0.2 第一步:打开tomcat配置 这里写图片描述 第二步: 这里写图 ...
- 在线人数统计session管理
下午比较闲(其实今天都很闲),想了一下在线人数统计方面的实现,上网找了下这方面的知识,最初我的想法是,管理session,如果session销毁了就减少,如果登陆用户了就新增一个,但是如果是用户非法退 ...