Q - Play With Sequence HDU - 3971 线段树 重新排序建树
Q - Play With Sequence
这个题目是一个线段树,比较特别的线段树,就是c询问一定次数之后重新排序建树来优化减低复杂度。
第一次碰到这种题目有点迷。
这个题目写还是很好写的,就是重新排序建树的位置不太好找。
不过可以知道的是,这是更新花费时间和排序花费时间的一个平衡,这个是一个二次函数,这个二次函数的最低点可以自己测出来。
现在可能有点听不懂,写完代码就很好理解了,
我测的每隔2000次C的操作就重新建树排序是最优的。
800,1000,2200,3000 都是可以的。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#include <map>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn = 3e5 + ;
typedef long long ll;
ll a[maxn], lc[maxn], rc[maxn], num[maxn];
string s[maxn];
struct node
{
ll lazy, max, min, len;
}tree[maxn*];
void push_up(int id)
{
tree[id].max = max(tree[id << ].max, tree[id << | ].max);
tree[id].min = min(tree[id << ].min, tree[id << | ].min);
//printf("tree[%d].min=%lld tree[%d].max=%lld\n", id, tree[id].min, id, tree[id].max);
} void build(int id,int l,int r)
{
tree[id].len = r - l + ;
tree[id].lazy = ;
if(l==r)
{
tree[id].max = tree[id].min = a[l];
return;
}
int mid = (l + r) >> ;
build(id << , l, mid);
build(id << | , mid + , r);
push_up(id);
} void push_down(int id)
{
if(tree[id].lazy)
{
int val = tree[id].lazy;
tree[id << ].max += val;
tree[id << ].min += val;
tree[id << | ].max += val;
tree[id << | ].min += val;
tree[id << ].lazy += val;
tree[id << | ].lazy += val;
// printf("tree[%d].max=%lld tree[%d].min=%lld\n", id << 1, tree[id << 1].max, id << 1, tree[id << 1].min);
// printf("tree[%d].max=%lld tree[%d].min=%lld\n", id << 1 | 1, tree[id << 1 | 1].max, id << 1 | 1, tree[id << 1 | 1].min);
tree[id].lazy = ;
}
}
void update(int id,int l,int r,ll x,ll y,ll val)
{
push_down(id);
// printf("id=%d l=%d r=%d x=%lld y=%lld val=%lld\n", id, l, r, x, y, val);
if(tree[id].min>=x&&tree[id].max<=y)
{
tree[id].lazy = val;
tree[id].min += val;
tree[id].max += val;
//printf("id=%d min=%lld max=%lld\n", id, tree[id].min, tree[id].max);
return;
}
int mid = (l + r) >> ;
if (tree[id << ].max >= x && tree[id << ].min <= y) update(id << , l, mid, x, y, val);
if (tree[id << | ].max >= x && tree[id << | ].min <= y) update(id << | , mid + , r, x, y, val);
push_up(id);
} int query(int id,int l,int r,ll x,ll y)
{
push_down(id);
if(tree[id].max<=y&&tree[id].min>=x)
{
return tree[id].len;
}
int mid = (l + r) >> , ans = ;
if (tree[id << ].max >= x && tree[id << ].min <= y) ans += query(id << , l, mid, x, y);
if (tree[id << | ].max >= x && tree[id << | ].min <= y) ans += query(id << | , mid + , r, x, y);
return ans;
} void push_alldown(int id,int l,int r)
{
if(l==r)
{
a[l] = tree[id].max;
return;
}
push_down(id);
int mid = (l + r) >> ;
push_alldown(id << , l, mid);
push_alldown(id << | , mid + , r);
} int main()
{
int n, m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for (int i = ; i <= n; i++) scanf("%lld", &a[i]);
sort(a + , a + + n);
build(, , n);
int cnt = ;
for(int i=;i<=m;i++)
{
cin >> s[i];
if (s[i] == "C") scanf("%lld%lld%lld", &lc[i], &rc[i], &num[i]), cnt++;
else scanf("%lld%lld", &lc[i], &rc[i]), num[i] = ;
}
int tot = ;
for(int i=;i<=m;i++)
{
if(s[i]=="C")
{
++tot;
//push_alldown(1, 1, n);
//printf("lc[%d]=%lld rc[%d]=%lld num[%d]=%lld\n", i, lc[i], i, rc[i], i, num[i]);
update(, , n, lc[i], rc[i], num[i]);
if(tot%==)
{
push_alldown(, , n);
sort(a + , a + + n);
build(, , n);
}
}
else
{
int ans = query(, , n, lc[i], rc[i]);
printf("%d\n", ans);
}
}
}
}
线段树 排序建树
Q - Play With Sequence HDU - 3971 线段树 重新排序建树的更多相关文章
- hdu 3436 线段树 一顿操作
Queue-jumpers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- hdu 3397 线段树双标记
Sequence operation Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu 2871 线段树(各种操作)
Memory Control Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 4267 线段树间隔更新
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
- hdu 4747 线段树
Mex Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- hdu 3954 线段树 (标记)
Level up Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 1754 线段树(Max+单点修改)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 1166 线段树(sum+单点修改)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)
Weak Pair Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
随机推荐
- 线程绑定cpu
#include <stdio.h> #include <pthread.h> #include <sys/sysinfo.h> #include <unis ...
- Juli函数
- pytorch cheatsheet
- B - How many integers can you find 杭电1976
Now you get a number N, and a M-integers set, you should find out how many integers which are small ...
- lua使用笔记2:Linux 中安装php的lua扩展
安装lua扩展的前提是lua已经安装好,如果没有安装,参照 1.http://pecl.php.net/package/lua 下载lua扩展 或者Linux下直接输入 wget http://pec ...
- python操作数据库-SQLSERVER-pyodbc
刚开始学python时,大家都习惯用pymssql去读写SQLSERVER.但是实际使用过程中,pymssql的读写性能以及可靠性的确不如pyodbc来的好. 正如微软官方推荐使用pyodbc库,作为 ...
- Java环境下 selenium webDriver + chrome浏览器搭建与调试
一.首先下载selenium webDriver jar包,下载地址如下: http://selenium-release.storage.googleapis.com/index.html 二.下载 ...
- fashion_mnist多分类训练,两种模型的保存与加载
from tensorflow.python.keras.preprocessing.image import load_img,img_to_array from tensorflow.python ...
- SpringBoot系列(十一)拦截器与拦截器链的配置与使用详解,你知道多少?
往期推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件详解 SpringBoot系列(四)web静 ...
- python学习21之高级特性
'''''''''1.切片(1)谁可以进行切片操作?——列表,元组,字符串(2)切片有以下几种操作'''#[a:b]:取从下标为a的元素开始,到下标为b-1的元素结束L=['aa','bb','cc' ...