Yaroslav and Points

明明区间合并一下就好的东西, 为什么我会写得这么麻烦的方法啊啊啊。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); vector<int> oo;
int getPos(int x) {
return lower_bound(oo.begin(), oo.end(), x) - oo.begin() + ;
}
int getPosL(int x) {
return lower_bound(oo.begin(), oo.end(), x) - oo.begin() + ;
}
int getPosR(int x) {
return upper_bound(oo.begin(), oo.end(), x) - oo.begin();
} namespace SGT {
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
LL sum[N << ], cnt[N << ];
LL lazya[N << ], lazyb[N << ], all[N << ];
inline void push(int rt) {
if(lazya[rt]) {
lazya[rt << ] += lazya[rt];
lazya[rt << | ] += lazya[rt];
all[rt << ] += cnt[rt << ] * lazya[rt];
all[rt << | ] += cnt[rt << | ] * lazya[rt];
lazya[rt] = ;
}
if(lazyb[rt]) {
lazyb[rt << ] += lazyb[rt];
lazyb[rt << | ] += lazyb[rt];
all[rt << ] += sum[rt << ] * lazyb[rt];
all[rt << | ] += sum[rt << | ] * lazyb[rt];
lazyb[rt] = ;
}
}
inline void pull(int rt) {
sum[rt] = sum[rt << ] + sum[rt << | ];
cnt[rt] = cnt[rt << ] + cnt[rt << | ];
all[rt] = all[rt << ] + all[rt << | ];
}
void modify1(int p, LL val, int l, int r, int rt) {
if(l == r) {
cnt[rt] += val;
sum[rt] += oo[l - ] * val;
if(!cnt[rt]) all[rt] = ;
return;
}
push(rt);
int mid = l + r >> ;
if(p <= mid) modify1(p, val, lson);
else modify1(p, val, rson);
pull(rt);
}
void modify2(int L, int R, LL val, int l, int r, int rt) {
if(L > R) return;
if(l >= L && r <= R) {
lazya[rt] += val;
all[rt] += val * cnt[rt];
return;
}
push(rt);
int mid = l + r >> ;
if(L <= mid) modify2(L, R, val, lson);
if(R > mid) modify2(L, R, val, rson);
pull(rt);
}
void modify3(int L, int R, LL val, int l, int r, int rt) {
if(L > R) return;
if(l >= L && r <= R) {
lazyb[rt] += val;
all[rt] += val * sum[rt];
return;
}
push(rt);
int mid = l + r >> ;
if(L <= mid) modify3(L, R, val, lson);
if(R > mid) modify3(L, R, val, rson);
pull(rt);
}
PLL query1(int L, int R, int l, int r, int rt) {
if(L > R) return mk(, );
if(l >= L && r <= R) return mk(sum[rt], cnt[rt]);
push(rt);
int mid = l + r >> ;
PLL ans = mk(, ), tmp = mk(, );
if(L <= mid) {
tmp = query1(L, R, lson);
ans.fi += tmp.fi; ans.se += tmp.se;
}
if(R > mid) {
tmp = query1(L, R, rson);
ans.fi += tmp.fi; ans.se += tmp.se;
}
return ans;
}
LL query2(int L, int R, int l, int r, int rt) {
if(L > R) return ;
if(l >= L && r <= R) return all[rt];
push(rt);
int mid = l + r >> ;
LL ans = ;
if(L <= mid) ans += query2(L, R, lson);
if(R > mid) ans += query2(L, R, rson);
return ans;
}
} int n, m, x[N], c[N];
pair<PII, int> qus[N]; int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d", &x[i]);
c[i] = x[i];
oo.push_back(x[i]);
}
scanf("%d", &m);
for(int i = ; i <= m; i++) {
scanf("%d%d%d", &qus[i].se, &qus[i].fi.fi, &qus[i].fi.se);
if(qus[i].se == ) {
c[qus[i].fi.fi] += qus[i].fi.se;
oo.push_back(c[qus[i].fi.fi]);
}
}
sort(oo.begin(), oo.end());
oo.erase(unique(oo.begin(), oo.end()), oo.end());
for(int i = ; i <= n; i++) SGT::modify1(getPos(x[i]), , , SZ(oo), );
for(int i = ; i <= n; i++) {
PLL add = SGT::query1(getPos(x[i]), SZ(oo), , SZ(oo), );
SGT::modify2(getPos(x[i]), getPos(x[i]), add.fi, , SZ(oo), );
SGT::modify3(getPos(x[i]), getPos(x[i]), -add.se, , SZ(oo), );
}
for(int i = ; i <= m; i++) {
if(qus[i].se == ) {
int p = qus[i].fi.fi, d = qus[i].fi.se;
if(d > ) {
SGT::modify1(getPos(x[p]), -, , SZ(oo), );
SGT::modify2(, getPos(x[p]) - , d, , SZ(oo), );
SGT::modify2(getPos(x[p]) + , getPos(x[p] + d) - , x[p] + d, , SZ(oo), );
SGT::modify3(getPos(x[p]) + , getPos(x[p] + d) - , -, , SZ(oo), );
x[p] += d;
SGT::modify1(getPos(x[p]), , , SZ(oo), );
PLL add = SGT::query1(getPos(x[p]), SZ(oo), , SZ(oo), );
SGT::modify2(getPos(x[p]), getPos(x[p]), add.fi, , SZ(oo), );
SGT::modify3(getPos(x[p]), getPos(x[p]), -add.se, , SZ(oo), );
} else if(d < ) {
d = -d;
SGT::modify1(getPos(x[p]), -, , SZ(oo), );
SGT::modify2(, getPos(x[p] - d) - , -d, , SZ(oo), );
SGT::modify2(getPos(x[p] - d) + , getPos(x[p]) - , -x[p], , SZ(oo), );
SGT::modify3(getPos(x[p] - d) + , getPos(x[p]) - , , , SZ(oo), );
x[p] -= d;
SGT::modify1(getPos(x[p]), , , SZ(oo), );
PLL add = SGT::query1(getPos(x[p]), SZ(oo), , SZ(oo), );
SGT::modify2(getPos(x[p]), getPos(x[p]), add.fi, , SZ(oo), );
SGT::modify3(getPos(x[p]), getPos(x[p]), -add.se, , SZ(oo), );
}
} else {
int L = qus[i].fi.fi, R = qus[i].fi.se;
L = getPosL(L); R = getPosR(R);
LL ans = SGT::query2(L, R, , SZ(oo), );
PLL tmpL = SGT::query1(L, R, , SZ(oo), );
PLL tmpR = SGT::query1(R + , SZ(oo), , SZ(oo), );
ans -= tmpL.se * tmpR.fi;
ans += tmpL.fi * tmpR.se;
printf("%lld\n", ans);
}
}
return ;
} /*
*/

Codeforces 295E Yaroslav and Points 线段树的更多相关文章

  1. Codeforces Beta Round #19D(Points)线段树

    D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  2. codeforces 295E Yaroslav and Points (离线操作+离散化+区间合并)

    参考链接:http://blog.csdn.net/dyx404514/article/details/8817717 写的很详细,这里就不再赘述,附上我的代码. #include <iostr ...

  3. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  4. Codeforces 1140F Extending Set of Points 线段树 + 按秩合并并查集 (看题解)

    Extending Set of Points 我们能发现, 如果把x轴y轴看成点, 那么答案就是在各个连通块里面的x轴的个数乘以y轴的个数之和. 然后就变成了一个并查集的问题, 但是这个题目里面有撤 ...

  5. CodeForces 19D Points (线段树+set)

    D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  6. Codeforces 1140F Extending Set of Points (线段树分治+并查集)

    这题有以下几个步骤 1.离线处理出每个点的作用范围 2.根据线段树得出作用范围 3.根据分治把每个范围内的点记录和处理 #include<bits/stdc++.h> using name ...

  7. codeforces 22E XOR on Segment 线段树

    题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...

  8. Codeforces 588E. A Simple Task (线段树+计数排序思想)

    题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...

  9. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

随机推荐

  1. heidiSQL使用简介

    2017年12月5日09:49:42 星期二 HeidiSQL下载链接 1. 这个是免费的, 不用辛苦找破解码了, 也不用担心被破解软件装后门 2. 可以一次执行多条SQL语句分开选项卡显示 3. 可 ...

  2. I/O 模型

    5种I/O模型的基本区别: 阻塞式I/O 非阻塞式I/O I/O复用 信号异步模型 异步I/O 1. 阻塞 I/O 最流行的I/O模型是阻塞I/O模型,缺省情形下,所有套接口都是阻塞的.我们以数据报套 ...

  3. python学习第9-10天,函数。

    函数初识 为什么要使用函数? 函数最重要的目的是方便我们重复使用相同的一段程序. 将一些操作隶属于一个函数,以后你想实现相同的操作的时候,只用调用函数名就可以,而不需要重复敲所有的语句. 函数的定义与 ...

  4. mysql:赋予用户权限、查看及修改端口号

    一.mysql 赋给用户权限 grant all privileges on *.* to joe@localhost identified by '1'; flush privileges; 即用u ...

  5. 借助dubbo-admin来管理你的服务

      1.  Github上下载最新的dubbo源码包并解压   2. 修改配置信息(打开 dubbo-admin/src/main/webapp/WEB-INF下的dubbo.properties,修 ...

  6. 开通博客的第一天上传我的C#基础笔记。

    1.索引器  string arrStr = "sddfdfgfh";  索引器的目的就是为了方便而已,可以在该类型的对象后面直接写[]访问该对象里面的成员  Console.Wr ...

  7. pipeline

    执行顺序:pipeline 写 pipeline类class Scrapyproject1Pipeline(object): def process_item(self, item, spider): ...

  8. day11 函数的位置形参,位置实参,可变长位置形参,关键字形参

    今天内容 函数的参数详解 形参与实参 形参及形式参数,就是在定义函数是括号中指定的参数(本质就是一个名字) 实参及实际参数,指的是在调用函数是传入的参数)(本质就是一个值) 在调用函数是就会把形参和实 ...

  9. css之操作属性

    1.文本 1.文本颜色:color 颜色属性被用来设置文字的颜色. 颜色是通过CSS最经常的指定: 十六进制值 - 如: #FF0000 一个RGB值 - 如: RGB(255,0,0) 颜色的名称 ...

  10. Zookeeper客户端Curator的使用,简单高效

    Curator是Netflix公司开源的一个Zookeeper客户端,与Zookeeper提供的原生客户端相比,Curator的抽象层次更高,简化了Zookeeper客户端的开发量. 1.引入依赖: ...