diff --git a/.ACM-Templates/LCIS.cpp b/.ACM-Templates/LCIS.cpp new file mode 100644 index 0000000..23ca374 --- /dev/null +++ b/.ACM-Templates/LCIS.cpp @@ -0,0 +1,29 @@ +/// LCIS 最长公共上升子序列 +namespace LCIS +{ + +const int MAXLEN_A = 500; +const int MAXLEN_B = 500; +int dp[MAXLEN_A+5][MAXLEN_B+5]; +int deal(const char* a,const char* b) +{ + int lena=strlen(a); + int lenb=strlen(b); + for(int i=1;i<=lenb;i++) + { + int k=0; + for(int j=1;j<=lena;j++) + { + dp[i][j]=dp[i-1][j];/// when b[i-1] != a[j-1] + if(b[i-1]>a[j-1]) k=max(k,dp[i-1][j]); + else if(b[i-1]==a[j-1]) dp[i][j]=k+1; + } + } + int ans=0; + for(int i=1;i<=lena;i++) ans=max(ans,dp[lenb][i]); + return ans; +} + +} +//End of namespace LCIS + diff --git a/.ACM-Templates/LCS.cpp b/.ACM-Templates/LCS.cpp new file mode 100644 index 0000000..5660cc4 --- /dev/null +++ b/.ACM-Templates/LCS.cpp @@ -0,0 +1,30 @@ +/// LCS 最长子序列 +namespace LCS +{ +const int MAXLEN_A = 500; +const int MAXLEN_B = 500; +int dp[MAXLEN_A+5][MAXLEN_B+5]; +int deal(const char* a,const char* b) +{ + int lena=strlen(a); + int lenb=strlen(b); + for(int i=0;i +#define MAXN 1010 +int seq[MAXN]; +int seqlen[MAXN]; +int main() +{ + int N; + while(scanf("%d",&N)==1) + { + int i,j,k,max,maxlen=1; + for(i=1; i<=N; i++) + seqlen[i]=1; //seqlen数组存以第i个数为终点的最长上升序列 + for(i=1; i<=N; i++) + scanf("%d",&seq[i]); //seq数组保存序列数组 + for (i=2; i<=N; i++) + { + max=0; + for (j=1; j<=i-1; j++) + { + if(seq[j]max) //在前i-1个序列中,寻找以终点小于seq[i]的最长的子序列,即最优子状态 + max=seqlen[j]; + } + seqlen[i]=max+1; + if(seqlen[i]>maxlen) //seqlen中保存的是第i个数为终点的最长上升序列,找出这个数组中最大的值即为最优序列长度 + maxlen=seqlen[i]; + } + printf("%d\n",maxlen); + } + return 0; +} diff --git a/.ACM-Templates/RMQ-ST.cpp b/.ACM-Templates/RMQ-ST.cpp new file mode 100644 index 0000000..274ec68 --- /dev/null +++ b/.ACM-Templates/RMQ-ST.cpp @@ -0,0 +1,34 @@ +#include +#include +using namespace std; + +namespace RMQ_ST +{ +const int MAXN=10000; +int f[MAXN][MAXN]; +int a[MAXN]; +int n; +void init() +{ + for(int i = 1;i<=n;i++) + { + f[i][0]=a[i]; + } + for(int j=1;(1< +#include +#include +using namespace std; +#define lchild rt << 1, l, m +#define rchild rt << 1 | 1, m + 1, r +const int N=400010; +int tree[N],lazy[N],t,ans=1,L,R,delta,n,m; + +//区间求和 +void push_up(int rt){ + tree[rt]=tree[rt << 1]+tree[rt << 1 | 1]; +} + +void push_down(int rt, int len) { + if(lazy[rt]) + { + lazy[rt << 1]=lazy[rt << 1 | 1] = lazy[rt]; + tree[rt << 1] = lazy[rt << 1] * (len - (len >> 1)); + tree[rt << 1 | 1] = lazy[rt << 1 | 1] * (len >> 1); + lazy[rt] = 0; + } +} + +void build(int rt = 1, int l = 1, int r = n) { + lazy[rt]=0; + if (l == r) { tree[rt]=1; return; }//注意对树的初始赋值 + int m = (l + r) >> 1; + build(lchild); build(rchild); + push_up(rt); +} + +void update(int L, int R, int delta, int rt = 1, int l = 1, int r = n) { + if (L <= l && r <= R) { + tree[rt] = delta * (r - l + 1);//数据更新 + lazy[rt] = delta; + return; + } + push_down(rt, r - l + 1); + int m = (l + r) >> 1; + if (L <= m) update(L, R, delta, lchild); + if (R > m) update(L, R, delta, rchild); + push_up(rt); +} + +int main() +{ + for(scanf("%d",&t);t--;) + { + scanf("%d%d",&n,&m); + build(); + for(int i=0;i +#include +#include +using namespace std; +const int maxn = 100000+10; + +int n,sum; + +struct node +{ + int l,r,n; +} a[maxn<<2]; + +void init(int l,int r,int i) +{ + a[i].l = l; + a[i].r = r; + a[i].n = 1; + if(l!=r) + { + int mid = (l+r)>>1; + init(l,mid,2*i); + init(mid+1,r,2*i+1); + } +} + +void insert(int i,int x,int y,int m) +{ + if(a[i].n == m)//相同则不用修改了 + return ; + if(a[i].l == x && a[i].r == y)//找到了区间,直接更新 + { + a[i].n = m; + return ; + } + if(a[i].n != -1)//如果该区间只有一种颜色 + { + a[2*i].n = a[2*i+1].n = a[i].n;//由于后面必定对子树操作,所以更新子树的值等于父亲的值 + a[i].n = -1;//由于该区域颜色与修改不同,而且不是给定区域,所以该区域必定为杂色 + } + //父区间为杂色时对所有子节点进行操作 + int mid = (a[i].l+a[i].r)>>1; + if(x>mid) + insert(2*i+1,x,y,m); + else if(y<=mid) + insert(2*i,x,y,m); + else + { + insert(2*i,x,mid,m); + insert(2*i+1,mid+1,y,m); + } +} + +int find(int i)//区间求和 +{ + if(a[i].n != -1)//纯色直接算这个区间 + return (a[i].r - a[i].l+1)*a[i].n; + else//不存则左右子树去找 + return find(i*2)+find(i*2+1); +} + +int main() +{ + int t,i,k,x,y,m; + int cas = 1; + scanf("%d",&t); + while(t--) + { + scanf("%d%d",&n,&k); + init(1,n,1); + while(k--) + { + scanf("%d%d%d",&x,&y,&m); + insert(1,x,y,m); + } + printf("Case %d: The total value of the hook is %d.\n",cas++,find(1)); + } + + return 0; +}