HDU 4348 I - To the moon 可持续化
队友套的可持续化线段树,徘徊在RE和MLE之间多发过的。。。
复用结点新的线段树平均要log2N个结点。
其实离线就好,按照时间顺序组织操作然后dfs。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector> using namespace std; const int maxn = ;
int n, m;
struct Node {
long long d, td;
int timelabel;
Node() {};
Node(long long _d, long long _td, int t) {
d = _d; td = _td; timelabel = t;
}
};
vector<Node> bit[maxn];
long long a[maxn], sum[maxn] = {}; inline int lowbit(int x)
{
return x & (-x);
} inline void update(int x, long long w, int timelabel)
{
int size;
long long tw = w * x;
while (x <= n) {
size = bit[x].size();
bit[x].push_back(bit[x][size-]);
size++;
bit[x][size-].d += w;
bit[x][size-].td += tw;
bit[x][size-].timelabel = timelabel;
x += lowbit(x);
}
} inline long long getsum(int x, int timelabel)
{
long long sd = , std = ;
int size, ox = x;
while (x > ) {
size = bit[x].size();
while (bit[x][size-].timelabel > timelabel) {
size--;
}
sd += bit[x][size-].d;
std += bit[x][size-].td;
x -= lowbit(x);
}
return (ox + ) * sd - std;
} inline long long getans(int x, int y, int timelabel)
{
return sum[y] - sum[x-] + getsum(y, timelabel) - getsum(x - , timelabel);
} inline void back(int timelabel)
{
int size;
for (int i = ; i <= n; ++i) {
size = bit[i].size();
while (bit[i][size-].timelabel > timelabel) {
bit[i].pop_back();
size--;
}
}
} void build()
{
for (int i = ; i <= n; ++i) {
bit[i].clear();
bit[i].push_back(Node(0LL, 0LL, ));
}
} int main()
{
//freopen("in.txt","r",stdin);
int cas = ;
while (scanf("%d%d", &n, &m) != EOF) {
if (cas++) printf("\n");
for (int i = ; i <= n; ++i) {
scanf("%lld\n", &a[i]);
sum[i] = sum[i-] + a[i];
}
char op[];
int x, y, z;
int timelabel = ;
build();
while (m--) {
scanf("%s", op);
if (op[] == 'Q') {
scanf("%d%d", &x, &y);
printf("%lld\n", getans(x, y, timelabel));
} else if (op[] == 'C') {
scanf("%d%d%d", &x, &y, &z);
timelabel++;
update(x, z, timelabel);
update(y + , -z, timelabel);
} else if (op[] == 'H') {
scanf("%d%d%d", &x, &y, &z);
printf("%lld\n", getans(x, y, z));
} else {
scanf("%d", &x);
timelabel = x;
back(x);
}
}
}
return ;
}
HDU 4348 I - To the moon 可持续化的更多相关文章
- 【HDU - 4348】To the moon(主席树在线区间更新)
BUPT2017 wintertraining(15) #8G 题意 给一个数组a,有n个数,m次操作.\(N, M ≤ 10^5, |A i| ≤ 10^9, 1 ≤ l ≤ r ≤ N, |d| ...
- hdu 4348 To the moon (主席树)
版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4348 题意: 一个长度为n的数组,4种操作 : (1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 . (2)Q ...
- HDU 4348.To the moon SPOJ - TTM To the moon -可持久化线段树(带修改在线区间更新(增减)、区间求和、查询历史版本、回退到历史版本、延时标记不下放(空间优化))
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- hdu 4348 To the moon (主席树 区间更新)
链接: http://acm.hdu.edu.cn/showproblem.php?pid=4348 题意: 4种操作: C l r c 区间[l,r]加c,时间+1 Q l r 询问当前时 ...
- HDU 4348 To the moon 主席树 在线更新
http://acm.hdu.edu.cn/showproblem.php?pid=4348 以前做的主席树没有做过在线修改的题做一下(主席树这种东西正经用法难道不是在线修改吗),标记永久化比较方便. ...
- HDU 4348 SPOJ 11470 To the moon
Vjudge题面 Time limit 2000 ms Memory limit 65536 kB OS Windows Source 2012 Multi-University Training C ...
- HDU 4348 To the moon 可持久化线段树
To the moon Problem Description BackgroundTo The Moon is a independent game released in November 201 ...
- HDU 4348 To the moon 可持久化线段树,有时间戳的区间更新,区间求和
To the moonTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.a ...
- To the moon HDU - 4348 (主席树,区间修改)
Background To The Moon is a independent game released in November 2011, it is a role-playing adventu ...
随机推荐
- UVaLive 3635 Pie (二分)
题意:有f+1个人来分n个圆形派,每个人得到的必须是一个整块,并且是面积一样,问你面积是多少. 析:二分这个面积即可,小了就多余了,多了就不够分,很简单就能判断. 代码如下: #pragma comm ...
- Grid++Report应用(引入项目中)
1.将Grid++Report安装文件中(\WebSamples\asp.net(csharp)\App_Code)的ReportData.cs,MssqlReportData.cs两个文件复制到自己 ...
- POJ3450【KMP理解】
题意: 求多个字符串的最长公共子串 思路: 4000个串,200长度. 一种暴力,对于一个串最多有200*200=40000级别个子串,然后我要再处理一下next数组200,8e6复杂度: 然后我要和 ...
- POJ 3067【树状数组】
题意: 给你两行数字,n个m个,然后给你k条线直接把两个数连起来,问有多少个交叉的 思路: 假定上一行是起点,下一行是终点. 把路按照起点从大到下排序, 然后可以直接对每条路查询,这条路目前的交叉数, ...
- studio 配置 opencv3.1
环境 win10 android studio2.0 OpenCV-3.1.0-android-sdk android-ndk-r10e-windows-x86_64 jdk-8u102-window ...
- andriod ndk配置
r7及以后版本不需要再配置cywin ,留影 先在path中配置ndk的环境变量,位置为ndk的根目录,也可在图5中的位置配置环境变量
- 解决tomcat一闪而过(转)
转自 http://blog.csdn.net/znn626/article/details/7893555 遇到很多次运行startup.bat后,一个窗口一闪而过的问题,但是从来没去纠正怎样修改配 ...
- bzoj1726:[Usaco2006 Nov]Roadblocks 次短路
Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她的旅途,于是她每次回农场,都会选择第二短的路径,而不象我们所习惯的那样, ...
- UML建模 | Rose | 没有跳出可选择新建RUP的对话框解决方法
- [題解]luogu P1156 垃圾陷阱
前言:[數據刪除] 來源:題解 不發題面了 首先我们来分析题目,“每个垃圾都可以用来吃或堆放”,浓浓的透露出一个背包气息.我们可以类比背包问题的放或不放.于是dp[i][j]dp[i][j]dp[i] ...