标签:isp ace 查询 can 节点 from lowbit put fst
给你一堆弹簧,每个弹簧有一定的弹跳距离
现在把球放到任意一个弹簧上,问需要几次球跳过所有弹簧,并且输出最后一个跳出去的位置
还要支持更改操作
一开始乱胡并查集,但是不行,一旦路径压缩就死亡,直接暴力搞又T
rhy后来说是LCT裸体,维护每个点的深度和最靠近根节点的祖先就行,根节点编号不妨设为n+1,甚至是某一年OI原题
没想到分块儿也可以搞,把同一块内的暴力维护,块间直接跳,省去了在块内跳来跳去的时间
修改和查询均摊下来都是sqrt(n)的时间复杂度,但是还是加了快读快写卡过去
代码:
#include <bits/stdc++.h> #define int long long #define sc(a) scanf("%lld",&a) #define scc(a,b) scanf("%lld %lld",&a,&b) #define sccc(a,b,c) scanf("%lld %lld %lld",&a,&b,&c) #define schar(a) scanf("%c",&a) #define pr(a) printf("%lld",a) #define fo(i,a,b) for(int i=a;i<b;++i) #define re(i,a,b) for(int i=a;i<=b;++i) #define rfo(i,a,b) for(int i=a;i>b;--i) #define rre(i,a,b) for(int i=a;i>=b;--i) #define prn() printf("\n") #define prs() printf(" ") #define mkp make_pair #define pii pair<int,int> #define pub(a) push_back(a) #define pob() pop_back() #define puf(a) push_front(a) #define pof() pop_front() #define fst first #define snd second #define frt front() #define bak back() #define mem0(a) memset(a,0,sizeof(a)) #define memmx(a) memset(a,0x3f3f,sizeof(a)) #define memmn(a) memset(a,-0x3f3f,sizeof(a)) #define debug #define db double #define yyes cout<<"YES"<<endl; #define nno cout<<"NO"<<endl; using namespace std; typedef vector<int> vei; typedef vector<pii> vep; typedef map<int,int> mpii; typedef map<char,int> mpci; typedef map<string,int> mpsi; typedef deque<int> deqi; typedef deque<char> deqc; typedef priority_queue<int> mxpq; typedef priority_queue<int,vector<int>,greater<int> > mnpq; typedef priority_queue<pii> mxpqii; typedef priority_queue<pii,vector<pii>,greater<pii> > mnpqii; const int maxn=500005; const int inf=0x3f3f3f3f3f3f3f3f; const int MOD=100000007; const db eps=1e-10; int qpow(int a,int b){int tmp=a%MOD,ans=1;while(b){if(b&1){ans*=tmp,ans%=MOD;}tmp*=tmp,tmp%=MOD,b>>=1;}return ans;} int lowbit(int x){return x&-x;} int max(int a,int b){return a>b?a:b;} int min(int a,int b){return a<b?a:b;} int mmax(int a,int b,int c){return max(a,max(b,c));} int mmin(int a,int b,int c){return min(a,min(b,c));} void mod(int &a){a+=MOD;a%=MOD;} bool chk(int now){} int half(int l,int r){while(l<=r){int m=(l+r)/2;if(chk(m))r=m-1;else l=m+1;}return l;} int ll(int p){return p<<1;} int rr(int p){return p<<1|1;} int mm(int l,int r){return (l+r)/2;} int lg(int x){if(x==0) return 1;return (int)log2(x)+1;} bool smleql(db a,db b){if(a<b||fabs(a-b)<=eps)return true;return false;} db len(db a,db b,db c,db d){return sqrt((a-c)*(a-c)+(b-d)*(b-d));} bool isp(int x){if(x==1)return false;if(x==2)return true;for(int i=2;i*i<=x;++i)if(x%i==0)return false;return true;} inline int read(){ char ch=getchar();int s=0,w=1; while(ch<48||ch>57){if(ch==‘-‘)w=-1;ch=getchar();} while(ch>=48&&ch<=57){s=(s<<1)+(s<<3)+ch-48;ch=getchar();} return s*w; } inline void write(int x){ if(x<0)putchar(‘-‘),x=-x; if(x>9)write(x/10); putchar(x%10+48); } int n,m,tot; int a[maxn],cnt[maxn],pos[maxn]; int id[maxn],from[maxn],to[maxn]; int o,x,y; void modify(int i){ if(i+a[i]>n) pos[i]=i,cnt[i]=0; else{ if(id[i]==id[i+a[i]]){ pos[i]=pos[i+a[i]]; cnt[i]=cnt[i+a[i]]+1; } else{ pos[i]=i+a[i]; cnt[i]=1; } } } void ask(int x){ int p=x,res=0; while(p!=pos[p]) res+=cnt[p],p=pos[p]; write(p),printf(" "),write(res+1),puts(""); } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0),cout.tie(0); n=read(),m=read(); tot=sqrt(n); re(i,1,tot){ from[i]=(i-1)*tot+1; to[i]=i*tot; } if(to[tot]<n) tot++,from[tot]=to[tot-1]+1,to[tot]=n; re(i,1,tot){ re(j,from[i],to[i]) id[j]=i; } re(i,1,n) a[i]=read(); rre(i,n,1) modify(i); while(m--){ o=read(); if(o==0){ x=read(),y=read(); a[x]=y; rre(i,x,from[id[x]]) modify(i); } else if(o==1) x=read(),ask(x); } return 0; }
标签:isp ace 查询 can 节点 from lowbit put fst
原文地址:https://www.cnblogs.com/oneman233/p/11511272.html