CF803G - Periodic RMQ Problem 动态开点线段树 或 离线
题意
有一个长度为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 动态开点线段树 或 离线的更多相关文章
- Codeforces 803G Periodic RMQ Problem ST表+动态开节点线段树
思路: (我也不知道这是不是正解) ST表预处理出来原数列的两点之间的min 再搞一个动态开节点线段树 节点记录ans 和标记 lazy=-1 当前节点的ans可用 lazy=0 没被覆盖过 els ...
- 【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) ...
- [bzoj 3531][SDOI2014]旅行(树链剖分+动态开点线段树)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3531 分析: 对于每个颜色(颜色<=10^5)都建立一颗线段树 什么!那么不是M ...
- codeforces 893F - Physical Education Lessons 动态开点线段树合并
https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...
- HDU 6183 Color it(动态开点线段树)
题目原网址:http://acm.hdu.edu.cn/showproblem.php?pid=6183 题目中文翻译: Time Limit: 20000/10000 MS (Java/Others ...
- [2016湖南长沙培训Day4][前鬼后鬼的守护 chen] (动态开点线段树+中位数 or 动规 or 贪心+堆优化)
题目大意 给定一个长度为n的正整数序列,令修改一个数的代价为修改前后两个数的绝对值之差,求用最小代价将序列转换为不减序列. 其中,n满足小于500000,序列中的正整数小于10^9 题解(引自mzx神 ...
- 【BZOJ-4636】蒟蒻的数列 动态开点线段树 ||(离散化) + 标记永久化
4636: 蒟蒻的数列 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 247 Solved: 113[Submit][Status][Discuss ...
- codeforces 915E - Physical Education Lessons 动态开点线段树
题意: 最大$10^9$的区间, $3*10^5$次区间修改,每次操作后求整个区间的和 题解: 裸的动态开点线段树,计算清楚数据范围是关键... 经过尝试 $2*10^7$会$MLE$ $10^7$会 ...
- CF915E Physical Education Lessons 动态开点线段树
题目链接 CF915E Physical Education Lessons 题解 动态开点线段树 代码 /* 动态开点线段树 */ #include<cstdio> #include&l ...
随机推荐
- ubuntu防火墙规则之ufw
前言 因公司项目的需求,需要对客户端机器简便使用防火墙的功能,所以可在页面进行简便设置防护墙规则,当然,这个功能需求放到我手上我才有机会学到.因为客户端机器都是ubuntu的,所以当然用了ubuntu ...
- JS面向对象编程(一):封装
js是一门基于面向对象编程的语言. 如果我们要把(属性)和(方法)封装成一个对象,甚至要从原型对象生成一个实例,我们应该怎么做呢? 一.生成对象的原始模式 假定把猫看 ...
- python编码问题——解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX
python实现爬虫遇到编码问题: error:UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX ...
- 【Android Studio】使用 Genymotion 调试出现错误 INSTALL_FAILED_CPU_ABI_INCOMPATI
RT -- 解决方法参考: https://my.oschina.net/u/242764/blog/375909 http://blog.csdn.net/wjr2012/article/detai ...
- 【iOS】打印方法名
为了便于追踪程序运行过程,可以在日志打印方法名,示例代码如下: NSLog(@"%@", NSStringFromSelector(_cmd)); 结果如图所示: 此外,在多个中, ...
- 【iOS】the executable was signed with invalid entitlements
又遇到了这个问题,貌似之前遇到过,如图所示: 原因:开发证书里没添加手机. PS: Xcode7 除外,据说已经不需要证书了,这里用的是 6.4
- Eclipse Other Projects小问题
Eclipse 不知什么时候多了个 "Other Projects" 文件夹,所有的项目又多了一层目录,如图所示: 虽然对功能没任何影响,但每次打开有些麻烦,多少感觉有些不爽…… ...
- UIRefreshControl 问题
这两天在学UIRefreshControl,主要参照的是github上的一个Demo(网址 https://github.com/evgeniymikholap/UIRefreshControlExa ...
- python 处理json数据
python 处理 json数据 以下是登录账号后获取的json数据,headers中注意加入cookie值 需要处理的数据如下: 全部代码如下 #!/usr/bin/env python # -*- ...
- wamp不显示文件图标
wamp不显示文件图标 效果如下图 右键图片"在新的标签页打开图片"后会跳转到404页面,并显示The requested URL /icons/unknown.gif was n ...