Do Not Try This Problem(分块思想)

题意:https://codeforces.com/group/ikIh7rsWAl/contest/259944/problem/D

给你q个操作,4个数n,a,k,c,从n好位置开始每次加a的位置变成字符c,加到k次停止。

思路:

首先肯定是从下往上修改,改完就可以不再考虑该位置了。

1.对于a公差大于根号n的我们直接更新。

2.对于a公差小于根号n的,我们存根号n个数组,长度为n,对每个a公差分别用并查集维护下一个要的数,每个数最多使用一次。

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\Users\13606\Desktop\Input.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr strcat
 13 #include <string>
 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 #include <cassert>
 21 #include <iomanip>
 22 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 23 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 24 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 25 //******************
 26 clock_t __STRAT,__END;
 27 double __TOTALTIME;
 28 void _MS(){__STRAT=clock();}
 29 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
 30 //***********************
 31 #define rint register int
 32 #define fo(a,b,c) for(rint a=b;a<=c;++a)
 33 #define fr(a,b,c) for(rint a=b;a>=c;--a)
 34 #define mem(a,b) memset(a,b,sizeof(a))
 35 #define pr printf
 36 #define sc scanf
 37 #define ls rt<<1
 38 #define rs rt<<1|1
 39 typedef pair<int,int> PII;
 40 typedef vector<int> VI;
 41 typedef long long ll;
 42 const double E=2.718281828;
 43 const double PI=acos(-1.0);
 44 const ll INF=(1LL<<60);
 45 const int inf=(1<<30);
 46 const double ESP=1e-9;
 47 const int mod=(int)1e9+7;
 48 const int N=(int)1e5+10;
 49 int l[305][N],r[305][N],n;bool f[2*N];
 50 void Init(int n)
 51 {
 52     for(int i=1;i<=300;++i)
 53     {
 54         for(int j=1;j<=N-5;++j)
 55         {
 56             r[i][j]=j;
 57         }
 58     }
 59 }
 60 int findr(int pos,int x)
 61 {
 62     if(r[x][pos]==pos)
 63         return pos;
 64     else
 65         return r[x][pos]=findr(r[x][pos],x);
 66 }
 67 void del(int pos)
 68 {
 69     for(int i=1;i<=200;++i)
 70     {
 71         r[i][pos]=findr(min(pos+i,n+1),i);
 72     }
 73 }
 74 char s[N];
 75 struct node
 76 {
 77     int a,b,c;
 78     char d[3];
 79 }op[N];
 80 /*
 81 xaaaaaaaaaaaaaaaaaaa
 82 3
 83 4 2 8 b
 84 6 3 4 c
 85 10 5 2 d
 86  */
 87 int main()
 88 {
 89     sc("%s",s+1);
 90     n=strlen(s+1);
 91     Init(n);
 92     int q;
 93     sc("%d",&q);
 94  
 95     for(int i=1;i<=q;++i)
 96         sc("%d%d%d%1s",&op[i].a,&op[i].b,&op[i].c,&op[i].d[0]);
 97     int x=200;
 98     for(int i=q;i>=1;--i)
 99     {
100         if(op[i].b>x)
101         {
102             for(int j=op[i].a,k=0;j<=n&&k<=op[i].c;j+=op[i].b,++k)
103             {
104                 if(f[j])continue;
105                 s[j]=op[i].d[0];
106                 f[j]=1;
107                 del(j);
108             }
109         }
110         else
111         {
112             int j=op[i].a;
113             while(j<=op[i].a+op[i].b*op[i].c)
114             {
115                 if(!f[j])
116                 {
117                     f[j]=1;
118                     s[j]=op[i].d[0];
119                     del(j);
120                 }
121                 j=findr(j,op[i].b);
122             }
123         }
124     }
125     pr("%s
",s+1);
126     return 0;
127 }
128  
129 /**************************************************************************************/
原文地址:https://www.cnblogs.com/--HPY-7m/p/11873006.html