LOJ #6284. Getting started with series partitioning 8,
#6284. sequence partitioning entry 8 memory limit: 256 MiB time limit: 500 ms standard input and output question type: traditional evaluation method: text comparison uploaded by: hzwer submit Record statistics discussion 1 Test Data question description
A series with a length of nnn and an nnn operation are provided. The operation involves the query of an element that is equal to a number ccc, and all the elements in this range are changed to ccc.
Input Format
Enter a number nnn in the first line.
Enter nnn numbers in the second row. the I-th digit is aia_iai, separated by spaces.
Next, enter the nnn line and enter three numbers lll, rrr, and ccc in each line, separated by spaces.
It indicates that the number in [l, r] [l, r] [l, r] Is ccc, and then the number in [l, r] [l, r, the numbers of r] [l, r] are changed to ccc.
Output Format
For each query, a row of numbers is output to indicate the answer.
Sample Input
41 2 2 41 3 11 4 41 2 21 4 2
Sample output
1102
Data range and prompt
For data of 100% 100 \ % 100%, 1 ≤ n ≤ 100000, − 231 ≤ others 1 \ leq n \ leq 100000, -2 ^ {31} \ leq \ mathrm {others} 1 ≤ n ≤ 100000, − 231 ≤ others, ans ≤ 231 − 1 \ mathrm {ans} \ leq 2 ^ {31}-1ans ≤ 231 − 1.
Classification label hzwer question set partitioning and data structure by size
Maintain the value of each block.
Fragmented chunks are marked as violent
Proof of time complexity:
#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;const int MAXN=1e5+10,mod=10007;inline char nc(){ static char buf[MAXN],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin)),p1==p2?EOF:*p1++;}inline int read(){ char c=nc();int x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1;c=nc();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=nc();} return x*f;}int a[MAXN],belong[MAXN],L[MAXN],R[MAXN],block,tag[MAXN];void reset(int x){ if(tag[x]==-1) return ; for(int i=L[x*block];i<=R[x*block];i++) a[i]=tag[x]; tag[x]=-1;}int Query(int l,int r,int val){ int ans=0; reset(belong[l]); for(int i=l;i<=min(r,R[l]);i++) { if(a[i]==val) ans++; a[i]=val; } if(belong[l]!=belong[r]) { reset(belong[r]); for(int i=L[r];i<=r;i++) { if(a[i]==val) ans++; a[i]=val; } } for(int i=belong[l]+1;i<=belong[r]-1;i++) { if(tag[i]==-1) { for(int j=L[i*block];j<=R[i*block];j++) if(a[j]==val) ans++; else a[j]=val; } else if(tag[i]==val) ans+=block; tag[i]=val; } return ans;}int main(){ #ifdef WIN32 freopen("a.in","r",stdin); #else #endif int N=read();block=sqrt(N); memset(tag,-1,sizeof(tag)); for(int i=1;i<=N;i++) a[i]=read(),belong[i]=(i-1)/block+1,L[i]=(belong[i]-1)*block+1,R[i]=belong[i]*block; for(int i=1;i<=N;i++) { int l=read(),r=read(),c=read(); printf("%d\n",Query(l,r,c)); } return 0;}