bzoj 4447 小凸解密码

  • 先将原始状态的 \(B\) 处理出来,可以发现,若不修改,则每次指定的起始位置不同,对这个环 \(B\) 带来的影响只有 \(B_0\) 不同,即每次 \(B_0=A_0\) ,其他位置不变.可以询问时修改这个值,询问结束时改回去.
  • 如果要修改,可以发现修改 \(A_i\) 其实只会影响 \(B_i,B_{i+1}\) 的值,也可以较快完成.
  • 只需要用一个 \(set\) 维护环上的零区间,修改,查询时都分情况维护,回答就好了.断环成链(复制一份接在后面)可以减小编写难度.(虽然还是挺麻烦)
  • 临摹参考了Menci的代码.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
#define inf 1e9
inline int read()
{
int x=0;
bool pos=1;
char ch=getchar();
for(;!isdigit(ch);ch=getchar())
if(ch=='-')
pos=0;
for(;isdigit(ch);ch=getchar())
x=x*10+ch-'0';
return pos?x:-x;
}
const int MAXN=1e5+10;
const int P=10;
struct node{
int l,r;
node(int pos) : l(pos) {}
node(int l,int r): l(l),r(r) {}
};
bool operator < (const node &a,const node &b)
{
return a.l<b.l;
}
bool operator < (const int &x,const node &y)
{
return x<y.l;
}
typedef node ZeroRange;
int n,m,A[MAXN],B[MAXN<<1];
char C[MAXN];
set<node> s;
typedef set<node>::iterator it;
void update(int pos,int v)
{
if(v==0 && B[pos]!=0)//1->0
{
for(int x=pos;x<2*n;x+=n)//再拼一段,拼成长2n的链
{
it nx=s.lower_bound(x);
it pr=--s.lower_bound(x);
if(nx->l==x+1 && pr->r==x-1)
{
node newnode(pr->l,nx->r);
s.erase(pr);
s.erase(nx);
s.insert(newnode);
}
else if(nx->l==x+1)
{
node newnode(x,nx->r);
s.erase(nx);
s.insert(newnode);
}
else if(pr->r==x-1)
{
node newnode(pr->l,x);
s.erase(pr);
s.insert(newnode);
}
else
s.insert(node(x,x));
}
}
else if(v!=0 && B[pos]==0)//0->1
{
for(int x=pos;x<2*n;x+=n)
{
it cur=--s.upper_bound(x);
if(x!=cur->l && x!=cur->r)
{
node l(cur->l,x-1);
node r(x+1,cur->r);
s.erase(cur);
s.insert(l);
s.insert(r);
}
else if(x!=cur->r)
{
node newnode(x+1,cur->r);
s.erase(cur);
s.insert(newnode);
}
else if(x!=cur->l)
{
node newnode(cur->l,x-1);
s.erase(cur);
s.insert(newnode);
}
else
s.erase(cur);
}
}
B[pos]=B[pos+n]=v;
}
inline int calc(int x,int y,char opt)
{
if(opt=='+')
return (x + y) % P;
else
return x * y % P;
}
void update(int pos,int v,char opt)
{
A[pos]=v,C[pos]=opt;
update(pos,calc(A[pos],A[(pos-1+n)%n],C[pos]));
update((pos+1)%n,calc(A[(pos+1)%n],A[pos],C[(pos+1)%n]));
}
inline int dist(int x,int y)
{
if(x>y)
swap(x,y);
return min(y-x,x-y+n);
}
int dist(int pos,const node &nd)
{
if(nd.l==inf || nd.l==-inf)
return -1;
if(nd.l<n && nd.r>=n)
{
if(pos%n>=nd.l && pos%n<=nd.r)
return 0;
if(pos%n+n>=nd.l && pos%n+n<=nd.r)
return 0;
}
if(pos%n>=nd.l%n && pos%n<=nd.r%n)
return 0;
return min(dist(pos%n,nd.l%n),dist(pos%n,nd.r%n));
}
int query(int pos)
{
int bak=B[pos];
update(pos,A[pos]);
int ans;
if(s.size()==2)
ans=-1;
else if(s.size()==3)
ans=dist(pos,*++s.begin());
else
{
ans=-1;
bool flag=(++s.begin())->l == ((--(--s.end()))->r+1)%n;
int opos=((pos-n/2)+n)%n;
it opp=--s.upper_bound(opos);
if(opp==s.begin())
opp=--s.upper_bound(opos+n);
if(flag && (opp==++s.begin()))
opp=--s.upper_bound(opos+n);
if(opp!=s.begin() && !(flag && (opp==++s.begin() || opp == --(--s.end()))))
ans=max(ans,dist(pos,*opp));
if(opp!=++s.begin())
{
it pr=opp;
pr--;
if(!(flag && pr==++s.begin()))
ans=max(ans,dist(pos,*pr));
}
if(opp!=--s.end())
{
it nx=opp;
++nx;
if(!(flag && nx== --(--s.end())))
ans=max(ans,dist(pos,*nx));
}
}
update(pos,bak);
return ans;
}
int main()
{
n=read(),m=read();
for(int i=0;i<n;++i)
scanf("%d %c",&A[i],&C[i]);
for(int i=0;i<n;++i)
B[i]=B[i+n]=calc(A[i],A[(i-1+n)%n],C[i]);
for(int l=0;l<2*n;)
{
if(B[l]==0)
{
int r=l;
while(r+1<2*n && B[r+1]==0)
++r;
s.insert(node(l,r));
l=r+1;
}
else
++l;
}
s.insert(node(-inf,-inf));
s.insert(node(inf,inf));
for(int i=0;i<m;++i)
{
int op=read(),pos=read();
if(op==1)
{
int v;
char opt;
scanf("%d %c",&v,&opt);
update(pos,v,opt);
}
else
printf("%d\n",query(pos));
}
return 0;
}

bzoj 4447 小凸解密码的更多相关文章

  1. bzoj4447[Scoi2015]小凸解密码

    4447: [Scoi2015]小凸解密码 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 150  Solved: 58[Submit][Status ...

  2. 「SCOI2015」小凸解密码 解题报告

    「SCOI2015」小凸解密码 题意:给一个环,定义一段连续的极长\(0\)串为\(0\)区间,定义一个位置的离一个\(0\)区间的距离为这个位置离这个区间中\(0\)的距离的最小值,每次询问一个位置 ...

  3. [bzoj4447] [loj#2010] [Scoi2015] 小凸解密码

    Description 小凸得到了一个密码盘,密码盘被等分成 \(N\) 个扇形,每个扇形上有一个数字(0-9),和一个符号("+"或"*") 密码盘解密的方法 ...

  4. bzoj4447 SCOI2015 小凸解密码 password

    传送门:bzoj4447 题解: 调试简直恶心,不过调完发现其实还是挺好写的. 用\(\mathrm{set}\)维护一段\(0\)区间的左右端点,每次最多修改两个点,所以很好维护. 查询的时候在\( ...

  5. 2019.03.26 bzoj4447: [Scoi2015]小凸解密码(线段树)

    传送门 题意简述:咕咕咕 思路:考虑预处理出bbb数组,然后每次改动aaa都只会对第iii和i+1i+1i+1这两个位置产生影响,于是可以用线段树来维护bbb数组. 现在求答案的方法是断环为链,倍增整 ...

  6. 【LOJ】#2010. 「SCOI2015」小凸解密码

    题解 断环为链,把链复制两份 用set维护一下全是0的区间,然后查找x + n / 2附近的区间,附近各一个过不去,最后弃疗了改为查附近的两个,然后过掉了= = 熟练掌握stl的应用,你值得拥有(雾 ...

  7. bzoj 4445 小凸想跑步 - 半平面交

    题目传送门 vjudge的快速通道 bzoj的快速通道 题目大意 问在一个凸多边形内找一个点,连接这个点和所有顶点,使得与0号顶点,1号顶点构成的三角形是最小的概率. 假设点的位置是$(x, y)$, ...

  8. 【BZOJ4443】小凸玩矩阵(二分答案,二分图匹配)

    [BZOJ4443]小凸玩矩阵(二分答案,二分图匹配) 题面 BZOJ Description 小凸和小方是好朋友,小方给小凸一个N*M(N<=M)的矩阵A,要求小秃从其中选出N个数,其中任意两 ...

  9. bzoj 4446: [Scoi2015]小凸玩密室

    Description 小凸和小方相约玩密室逃脱,这个密室是一棵有n个节点的完全二叉树,每个节点有一个灯泡.点亮所有灯 泡即可逃出密室.每个灯泡有个权值Ai,每条边也有个权值bi.点亮第1个灯泡不需要 ...

随机推荐

  1. jni使用问题总结

    参考: https://blog.csdn.net/fred_lzy/article/details/53159138 https://blog.csdn.net/avi3/article/detai ...

  2. mysql 索引相关问题

    mysql中key .primary key .unique key 与index区别 https://blog.csdn.net/nanamasuda/article/details/5254317 ...

  3. R中去除为NA的行--转载

    下面用实例来说明这两个函数的作用: 这是一个数据框final: gene hsap mmul mmus rnor cfam 1 ENSG00000208234 0 NA NA NA NA 2 ENSG ...

  4. ubuntu如何U盘启 动制作

    先到出 U盘的位置, 方法有很多,terminal 用root权限,采用fdisk -f 命令 unmount /dev/sdb -I //U盘拨出 sdb为U盘挂载的位置 mkfs.fat /dev ...

  5. Asp.Net MVC向视图View传值的三种方法

    本文将总结Asp.Net MVC向视图View传值的三种常见的方法: ----------------------------------------------------------------- ...

  6. vue.js学习之组件(上篇)

    本文的Demo和源代码已放到GitHub,如果您觉得本篇内容不错,请点个赞,或在GitHub上加个星星! https://github.com/zwl-jasmine95/Vue_test 以下所有知 ...

  7. 理解JAVA MQ消息中间件

    MQ的几种消息传递方式 发布订阅模式 发布订阅模式有点类似于我们日常生活中订阅报纸.每年到年尾的时候,邮局就会发一本报纸集合让我们来选择订阅哪一个.在这个表里头列了所有出版发行的报纸,那么对于我们每一 ...

  8. C++:tinyxml的使用

    1. 简介 TinyXML2(最新版本)是一个开源的功能齐全的XML解析库 For C++,源码见:github. 2. 开始使用 首先从Github上获得源码,是一个完整的演示工程,我们只需要其中的 ...

  9. 007PHP基础知识——类型转换 外部变量

    <?php /**类型转换 */ /*1.自由转换*/ /*2.强制转换:不改变原变量,生成新的变量*/ //转换为字符串: /*$a=100; $b=(string)$a; var_dump( ...

  10. mac下mysql 1045 (28000): Access denied for user 'root'@'localhost' (using password:

    新入了mac pro,安装好mysql后,用终端进入mysql遇到个问题: 1045 (28000): Access denied for user 'root'@'localhost' (using ...