티스토리 뷰
Link
Tag
- Segment Tree
- Spare Table
Time Complexity
Sparse Table 전처리에
Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
typedef long long ll; | |
typedef pair<ll,ll> pll; | |
typedef pair<int,int> pii; | |
#define fi first | |
#define se second | |
#define endl '\n' | |
#define y1 holyshit | |
#define all(x) x.begin(),x.end() | |
const int inf=0x3f3f3f3f; | |
const int mod=100003; | |
int N,Q,A[500010],f[100010][20],g[100010][20]; | |
int tree[2][1<<20]; | |
int base=1<<19; | |
void update(int op,int l,int r){ | |
l+=base; r+=base; | |
while(l<=r){ | |
if(l&1) tree[op][l]++; | |
if(~r&1) tree[op][r]++; | |
l=(l+1)>>1; r=(r-1)>>1; | |
} | |
} | |
int query(int op,int p){ | |
int ret=0; | |
for(p+=base;p;p>>=1) ret+=tree[op][p]; | |
return ret; | |
} | |
int main(){ | |
ios_base::sync_with_stdio(0); cin.tie(0); | |
cin>>N>>Q; | |
for(int i=0;i<mod;i++){ | |
f[i][0]=(2LL*i*i-1+mod)%mod; | |
g[i][0]=(4LL*i*i-3+mod)*i%mod; | |
} | |
for(int i=1;i<20;i++) for(int j=0;j<mod;j++){ | |
f[j][i]=f[f[j][i-1]][i-1]; | |
g[j][i]=g[g[j][i-1]][i-1]; | |
} | |
for(int i=1;i<=N;i++) cin>>A[i]; | |
while(Q--){ | |
int t,l,r,x; cin>>t; | |
if(t==3){ | |
cin>>x; | |
int res=A[x], fcnt=query(0,x), gcnt=query(1,x); | |
for(int i=0;i<20;i++) if((1<<i)&fcnt) res=f[res][i]; | |
for(int i=0;i<20;i++) if((1<<i)&gcnt) res=g[res][i]; | |
cout<<res<<endl; | |
} | |
else{ | |
cin>>l>>r; | |
update(t-1,l,r); | |
} | |
} | |
return 0; | |
} |
'PS > 문제풀이' 카테고리의 다른 글
BOJ 17635: 다리 (0) | 2022.04.25 |
---|---|
BOJ 16909: 카드 구매하기 3 (0) | 2021.04.02 |
BOJ 14870: 조개 줍기 (0) | 2020.08.02 |
BOJ 16027: Global warming (0) | 2020.06.14 |
BOJ 17101: Dynamic Centroid (0) | 2020.06.13 |