CF

题意

有一个长度为n × k (<=1E9)的数组,有区间修改和区间查询最小值的操作。

思路

由于数组过大,直接做显然不行。

有两种做法,可以用动态开点版本的线段树,或者离线搞(还没搞)(搞好了)。

注意只有1E5次操作,所以真正被更新到的区间并不多,最差单次新开2×log(1E9)。

对于新开的区间的最小值,可以这样计算,如果区间表示的值大于n,那就是原来长度为n的区间的最小值,小于n的话,在ST表中查询即可。

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define debug(x) cerr<<#x << " := " << x << endl;
#define bug cerr<<"-----------------------"<<endl;
#define FOR(a, b, c) for(int a = b; a <= c; ++ a) typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll; template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); } template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} const int inf = 0x3f3f3f3f; const int mod = 1e9+; /**********showtime************/ const int maxn = 1e5+;
int b[maxn]; int st[maxn][], Log[maxn];
void init_st(int n) {
Log[] = -;
for(int i=; i<=n; i++) {
Log[i] = Log[i>>] + ;
st[i][] = b[i];
}
for(int j=; (<<j) <= n; j++) {
for(int i=; i + (<<j) - <= n; i++) {
st[i][j] = min(st[i][j-], st[i+ (<<(j-))][j-]);
}
}
}
int rmq_st(int L, int R) {
int k = Log[R - L + ];
return min(st[L][k], st[R-(<<k)+][k]);
}
int n,k;
int getmin(int le, int ri) {
if(ri - le + >= n) return rmq_st(, n);
int L = le % n; if(L == ) L = n;
int R = ri % n; if(R == ) R = n;
if(L <= R) return rmq_st(L, R);
return min(rmq_st(L, n), rmq_st(, R));
}
struct Node{
int le, ri;
int lc,rc;
int val, tag;
} tree[maxn * ];
int tot = ;
int newNode(int le, int ri) {
tot++;
tree[tot].le = le;
tree[tot].ri = ri;
tree[tot].lc = tree[tot].rc = ;
tree[tot].val = getmin(le, ri);
tree[tot].tag = ;
return tot;
}
void pushdown(int rt) {
tree[tree[rt].lc].val = tree[tree[rt].lc].tag = tree[rt].tag;
tree[tree[rt].rc].val = tree[tree[rt].rc].tag = tree[rt].tag;
tree[rt].tag = ;
}
void update(int L, int R, int b, int rt) {
if(L<=tree[rt].le && tree[rt].ri <= R) {
tree[rt].val = tree[rt].tag = b;
return;
}
int mid = (tree[rt].le + tree[rt].ri) >> ;
if(tree[rt].lc == ) tree[rt].lc = newNode(tree[rt].le, mid);
if(tree[rt].rc == ) tree[rt].rc = newNode(mid+, tree[rt].ri);
if(tree[rt].tag) pushdown(rt);
if(mid >= L) update(L, R, b, tree[rt].lc);
if(mid < R) update(L, R, b, tree[rt].rc);
tree[rt].val = min(tree[tree[rt].lc].val, tree[tree[rt].rc].val);
}
int query(int L, int R, int rt) {
if(L <= tree[rt].le && tree[rt].ri <= R) {
return tree[rt].val;
} int mid = (tree[rt].le + tree[rt].ri) >> ;
if(tree[rt].lc == ) tree[rt].lc = newNode(tree[rt].le, mid);
if(tree[rt].rc == ) tree[rt].rc = newNode(mid+, tree[rt].ri);
if(tree[rt].tag) pushdown(rt);
int res = inf;
if(mid >= L) res = min(res, query(L, R, tree[rt].lc));
if(mid < R) res = min(res, query(L, R, tree[rt].rc));
tree[rt].val = min(tree[tree[rt].lc].val, tree[tree[rt].rc].val);
return res;
}
int main(){
scanf("%d%d", &n, &k);
for(int i=; i<=n; i++) scanf("%d", &b[i]);
init_st(n);
int q; scanf("%d", &q);
newNode(, n * k);
while(q--) {
int op;
scanf("%d", &op);
if(op == ) {
int le,ri,x;
scanf("%d%d%d", &le, &ri, &x);
update(le, ri, x, );
}
else {
int le, ri;
scanf("%d%d", &le, &ri);
printf("%d\n", query(le, ri, ));
}
}
return ;
}

离线的话,我们可以记录下所有被问到的点,然后我们可以压缩原来长度为1E9的数组:问到的点保持不变,而相邻两点之间的区间压缩成一个点,保存这段区间的最小值即可。

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define debug(x) cerr<<#x << " := " << x << endl;
#define bug cerr<<"-----------------------"<<endl;
#define FOR(a, b, c) for(int a = b; a <= c; ++ a) typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll; template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); } template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} const int inf = 0x3f3f3f3f; const int mod = 1e9+; /**********showtime************/ const int maxn = 1e5+;
int b[maxn];
struct Query{
int op;
int le,ri;
int x;
} ask[maxn];
vector<int>v, nv,node; int n,k;
int st[maxn][];
int Log[maxn]; void init_st(int n){
Log[] = -;
for(int i = ; i<=n; i++) {
st[i][] = b[i];
Log[i] = Log[i>>] + ;
} for(int j=; (<<j) <= n; j++) {
for(int i=; i + ( << j) - <= n; i ++) {
st[i][j] = min(st[i][j-], st[i + (<<(j-)) ][j-]);
}
}
}
int get(int L, int R) {
int k = Log[R - L + ];
return min(st[L][k], st[R-(<<k)+][k]);
}
int getmin(int le, int ri) {
if(ri - le + >= n)
return get(, n);
int l = le % n; if(!l) l = n;
int r = ri % n; if(!r) r = n;
if(l <= r) return get(l, r);
return min(get(l, n), get(, r));
} int getid(int val) {
return lower_bound(nv.begin(), nv.end(), val) - nv.begin() + ;
} int mn[maxn*],lazy[maxn * ];
void build(int le, int ri, int rt) {
if(le == ri) {
mn[rt] = node[le-];
return;
}
int mid = (le + ri) >> ;
build(le, mid, rt<<);
build(mid+, ri, rt<<|);
mn[rt] = min(mn[rt<<], mn[rt<<|]);
}
void pushdown(int rt) {
mn[rt<<] = mn[rt<<|] = lazy[rt];
lazy[rt<<] = lazy[rt<<|] = lazy[rt];
lazy[rt] = ;
}
void update(int L, int R,int x, int le, int ri, int rt) {
if(le >= L && ri <= R){
lazy[rt] = x;
mn[rt] = x;
return ;
}
if(lazy[rt]) pushdown(rt);
int mid = (le + ri) >> ;
if(mid >= L) update(L, R, x, le, mid, rt<<);
if(mid < R) update(L, R, x, mid+, ri, rt<<|);
mn[rt] = min(mn[rt<<], mn[rt<<|]);
}
int query(int L, int R,int le, int ri, int rt) {
if(le >= L && ri <= R) {
return mn[rt];
}
if(lazy[rt]) pushdown(rt);
int mid = (le + ri) >> ;
int res = inf;
if(mid >= L) res = min(res, query(L, R, le, mid, rt<<));
if(mid < R) res = min(res, query(L, R, mid+,ri, rt<<|));
mn[rt] = min(mn[rt<<], mn[rt<<|]);
return res;
}
int main(){
scanf("%d%d", &n, &k);
for(int i=; i<=n; i++) scanf("%d", &b[i]);
init_st(n);
int q; scanf("%d", &q);
for(int i=; i<=q; i++){
int op;
scanf("%d", &op);
if(op == ) {
ask[i].op = op;
scanf("%d%d%d", &ask[i].le, &ask[i].ri, &ask[i].x);
v.pb(ask[i].le);
v.pb(ask[i].ri);
}
else{
ask[i].op = op;
scanf("%d%d", &ask[i].le, &ask[i].ri);
v.pb(ask[i].le);
v.pb(ask[i].ri);
}
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
int N = v.size();
// cout<<" ** "<<endl;
for(int i=; i<N; i++) {
node.pb( getmin(v[i],v[i]));
nv.pb(v[i]);
if(i+ < N && v[i] + <= v[i+] - )
{
node.pb(getmin(v[i]+, v[i+]-));
nv.pb(v[i]+);
}
} /// 把一个开区间当成一个点。 N = nv.size();
build(, N, );
for(int i=; i<=q; i++) {
if(ask[i].op == ) {
update(getid(ask[i].le),getid(ask[i].ri), ask[i].x, , N, );
}
else {
printf("%d\n",query(getid(ask[i].le), getid(ask[i].ri), , N, ));
}
}
return ;
}

CF803G - Periodic RMQ Problem 动态开点线段树 或 离线的更多相关文章

  1. Codeforces 803G Periodic RMQ Problem ST表+动态开节点线段树

    思路: (我也不知道这是不是正解) ST表预处理出来原数列的两点之间的min 再搞一个动态开节点线段树 节点记录ans 和标记 lazy=-1 当前节点的ans可用  lazy=0 没被覆盖过 els ...

  2. 【bzoj4999】This Problem Is Too Simple! 树链剖分+动态开点线段树

    题目描述 给您一颗树,每个节点有个初始值. 现在支持以下两种操作: 1. C i x(0<=x<2^31) 表示将i节点的值改为x. 2. Q i j x(0<=x<2^31) ...

  3. [bzoj 3531][SDOI2014]旅行(树链剖分+动态开点线段树)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3531 分析: 对于每个颜色(颜色<=10^5)都建立一颗线段树 什么!那么不是M ...

  4. codeforces 893F - Physical Education Lessons 动态开点线段树合并

    https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...

  5. HDU 6183 Color it(动态开点线段树)

    题目原网址:http://acm.hdu.edu.cn/showproblem.php?pid=6183 题目中文翻译: Time Limit: 20000/10000 MS (Java/Others ...

  6. [2016湖南长沙培训Day4][前鬼后鬼的守护 chen] (动态开点线段树+中位数 or 动规 or 贪心+堆优化)

    题目大意 给定一个长度为n的正整数序列,令修改一个数的代价为修改前后两个数的绝对值之差,求用最小代价将序列转换为不减序列. 其中,n满足小于500000,序列中的正整数小于10^9 题解(引自mzx神 ...

  7. 【BZOJ-4636】蒟蒻的数列 动态开点线段树 ||(离散化) + 标记永久化

    4636: 蒟蒻的数列 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 247  Solved: 113[Submit][Status][Discuss ...

  8. codeforces 915E - Physical Education Lessons 动态开点线段树

    题意: 最大$10^9$的区间, $3*10^5$次区间修改,每次操作后求整个区间的和 题解: 裸的动态开点线段树,计算清楚数据范围是关键... 经过尝试 $2*10^7$会$MLE$ $10^7$会 ...

  9. CF915E Physical Education Lessons 动态开点线段树

    题目链接 CF915E Physical Education Lessons 题解 动态开点线段树 代码 /* 动态开点线段树 */ #include<cstdio> #include&l ...

随机推荐

  1. HDU 多校 第三场 Find the answer

    这题是原来cf上的一道原题,不过对于有一些数据范围修改了,不过还是很好想的 题意:给定一个长度为N的数组,对于数组中的每个位置,满足当前和小于M所需要去掉的最小代价 分析:对于当前是否需要进行去掉一些 ...

  2. 2019杭电多校第二场hdu6602 Longest Subarray(线段树)

    Longest Subarray 题目传送门 解题思路 本题求一个最大的子区间,满足区间内的数字要么出现次数大于等于k次,要么没出现过.给定区间内的数字范围是1~c. 如果r为右边界,对于一种数字x, ...

  3. windows下用easybcd引导ubuntu出现grub的解决方案

    linux安装时吧boot挂在到单独的分区 如果grub覆盖了mbr的话可以用pe工具箱修复windows的mbr linux引导项选grub 驱动器选安装时挂载了/boot的分区 添加条目 启动即可 ...

  4. angularjs的input防抖

    在开发中,遇到一个这样的需求,使用$scope.$watch()方法监听input值的改变,然后去$resource请求,但是请求过于频繁,需要做逻辑调整.代码如下: var timeout; $sc ...

  5. poj 1050 To the Max(最大子矩阵之和)

    http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here  也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...

  6. 【Vue前端】Vue前端注册业务实现!!!【代码】

    用户注册前端逻辑 1. Vue绑定注册界面准备 1.导入Vue.js库和ajax请求的库 <script type="text/javascript" src="{ ...

  7. mysql是如何实现事务隔离以及MVCC详解

    提到事务,你肯定会想到ACID(Atomicity.Consistency.Isolation.Durability,即原子性.一致性.隔离性.持久性),我们就来说说其中I,也就是"隔离性& ...

  8. git的使用(一)

    git   —version  展示git的版本 tanya ~$ git --version git version 2.22.0 最小配置   git config —global user.na ...

  9. 调测Onvif事件总结解决办法

    主要在调测事件用例的过程中,发现了大量的信息,和未曾碰到的场景和非法错误等信息,先总结解决办法如下: (1)测试过程中发现以前的一个难题解决了,原先在生成soap空间命名的文件中有部分需要下载,离线生 ...

  10. Vue+Typescript中在Vue上挂载axios使用时报错

    Vue+Typescript中在Vue上挂载axios使用时报错 在vue项目开发过程中,为了方便在各个组件中调用axios,我们通常会在入口文件将axios挂载到vue原型身上,如下: main.t ...