T1 [A. String] "bucket sort", "tree line"
Each letter segment tree maintenance interval appeared many times,
In the sort of time, first check the number of occurrences of each letter of a range, and then one by one interval assignment
Complexity $ O (mlog (n) * 26) $
Optimization of constant (26): the definition of f (labeled lazy): F! = 0, representing the subtree are assigned to the same value; f == 0, indicates unequal.
The range query to only access to the f! = 0 then return to the contribution ans, pushup, pushdown Similarly, avoid enumerated 26
Of course you can unroll the loop barely card too
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #define mid ((t[k].l+t[k].r)>>1) 6 #define lc (k<<1) 7 #define rc (k<<1|1) 8 #define R register 9 using namespace std; 10 int read() 11 { 12 int f=1,x=0;char ch=getchar(); 13 while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();} 14 while(ch<='9'&&ch>='0'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} 15 return f*x; 16 } 17 const int maxn=100005; Int19Node { Struct18 l,r,sum,f; 20 }t[maxn*4]; 21 char s[maxn]; 22 int a[maxn],ans[30]; 23 inline void up(R int k) 24 { 25 if(t[lc].f==t[rc].f&&t[lc].f) t[k].f=t[lc].f; 26 else t[k].f=0; 27 } 28 inline void down(R int k) 29 { 30 t[lc].f=t[rc].f=t[k].f; 31 } 32 void build(R int k,R int l,R int r) 33 { 34 t[k].l=l,t[k].r=r; 35 if(l==r) 36 { 37 t[k].f=a[l]; 38 t[k].sum=1; 39 return; 40 } 41 build(lc,l,mid); 42 build(rc,mid+1,r); 43 t[k].sum=t[lc].sum+t[rc].sum; 44 up(k); 45 } 46 void query(R int k,R int l,R int r) 47 { 48 if(t[k].l>=l&&t[k].r<=r&&t[k].f) 49 { 50 ans[t[k].f]+=t[k].sum; 51 return; 52 } 53 if(t[k].f)down(k); 54 if(l<=mid)query(lc,l,r); 55 if(r>mid)query(rc,l,r); 56 up(k); 57 } 58 void change(R int k,R int l,R int r,R int w) 59 { 60 if(t[k].l>=l&&t[k].r<=r) 61 { 62 t[k].f=w; 63 return; 64 } 65 if(t[k].f) down(k); 66 if(l<=mid)change(lc,l,r,w); 67 if(r>mid)change(rc,l,r,w); 68 up(k); 69 } 70 void print(R int k) 71 { 72 if(t[k].l==t[k].r) 73 { 74 char ch=t[k].f+'a'-1; 75 printf("%c",ch); 76 return; 77 } 78 if(t[k].f) down(k); 79 print(lc); 80 print(rc); 81 } 82 int main() 83 { 84 // freopen("data","r",stdin); 85 const int n=read(),m=read(); 86 scanf("%s",s+1); 87 for(R int i=1;i<=n;++i) 88 a[i]=s[i]-'a'+1; 89 build(1,1,n); 90 for(R int i=1;i<=m;++i) 91 { 92 R int l=read(),r=read(),x=read(); 93 memset(ans,0,sizeof ans); 94 query(1,l,r); 95 if(x) 96 { 97 for(R int i=1;i<=26;++i) 98 if(ans[i]) 99 { 100 change(1,l,l+ans[i]-1,i); 101 l=l+ans[i]; 102 } 103 } 104 else 105 { 106 for(R int i = 26 ; i> = 1 - i) 107 if (years old [i]) 108 { 109 exchange ( 1 , l, l + years old [i] - 1 , i); 110 l = l + years [i]; 111 } 112 } 113 } 114 print ( 1 ); 115 } 116 / * 117 g ++ 1.cpp -o 1 118 ./1 119 120 * /
T2 [B. matrix] "dynamic programming - two not a poison."
Definition of f [i] [j]: from left to right to the i-th row (i-1 is able to put there), there is a j-th on the right of the interval,
l [i]: up to i-th column, the number of left row interval has ended; r [i]: i until the column number of the right section has begun;
f[i][j]=f[i-1][j]+f[i-1][j-1]*(r[i]-(j-1));
To hold the former, the latter is a more put, j-1 is a right side section put j-1 has a total of r [i] empty
f[i][j]要乘上:A(i-j-l[i-1],l[i]-l[i-1])
l [i] -l [i-1] is able to put the empty column i, i is the number of total some 1, j on the right-interval a, l [i-1] th must be put has ended in the i-1 th left section, the rest of the elect come and be able to put on the air in
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #define int long long 6 using namespace std; 7 const int maxn=3005; 8 const int mod=998244353 ; 9 int read() 10 { 11 int f=1,x=0;char ch=getchar(); 12 while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();} 13 while(ch<='9'&&ch>='0'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} 14 return f*x; 15 } 16 int n,m,l[maxn],r[maxn],f[3005][3005]; 17 signed main() 18 { 19 // freopen("data","r",stdin); 20 n=read(),m=read(); 21 for(int i=1;i<=n;i++) 22 { 23 int li=read(),ri=read(); 24 l[li]++,r[ri]++; 25 } 26 for(int i=1;i<=m;i++) 27 l[i]=l[i-1]+l[i],r[i]=r[i-1]+r[i]; 28 // for(int i=1;i<=m;i++)cout<<l[i]<< 29 f[0][0]=1; 30 for(int i=1;i<=m;i++) 31 { 32 f[i][0]=f[i-1][0]; 33 for(int j=1;j<=r[i];j++) 34 f[i][j]=(f[i-1][j]+f[i-1][j-1]*(r[i]-j+1)%mod)%mod; 35 for(int j=0;j<=min(i,n);j++) 36 for(int k=l[i-1];k<=min(i-j,l[i]-1);k++) 37 f[i][j]=f[i][j]*(i-j-k)%mod; 38 } 39 printf("%lld\n",f[m][n]%mod); 40 } 41 /* 42 g++ 1.cpp -o 1 43 ./1 44 45 */
View Code
T3 [C. Big] "trie tree."
性质:(a^b^c)<<d=(a<<d)^(b<<d)^(c<<d)
So break the 1 ~ i i is the first left and then all doubts and