第一周 2.29 --- 3.6
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第一周 2.29 --- 3.6相关的知识,希望对你有一定的参考价值。
2.29
cf 620 e New Year Tree
dfs序,线段树区间修改,颜色不超过60种
改了好久,还是写得太少
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 8 typedef long long LL; 9 const int maxn = 5e6+5; 10 int a[maxn],b[maxn],ql,qr,qv; 11 LL sum[maxn],add[maxn]; 12 int vis[maxn],id[maxn],last[maxn],cnt; 13 vector<int> g[maxn]; 14 int n,m; 15 16 void dfs(int u){ 17 id[u] = ++cnt; 18 for(int i = 0;i < g[u].size();i++){ 19 int v = g[u][i]; 20 if(id[v]) continue; 21 dfs(v); 22 } 23 last[u] = cnt; 24 } 25 26 void Push_up(int o){ 27 sum[o] = sum[o<<1]|sum[o<<1|1]; 28 } 29 30 void Push_down(int o){ 31 if(add[o]){ 32 add[o<<1] = add[o<<1|1] = add[o]; 33 sum[o<<1] = 1LL << (add[o]); 34 sum[o<<1|1] = 1LL << (add[o]); 35 add[o] = 0LL; 36 } 37 } 38 39 void Build(int o,int l,int r){ 40 if(l == r){ 41 sum[o] = 1LL << (b[l]); 42 return; 43 } 44 int mid = (l+r)/2; 45 Build(o<<1,l,mid); 46 Build(o<<1|1,mid+1,r); 47 Push_up(o); 48 } 49 50 void Update(int o,int l,int r){ 51 if(ql <= l && r <= qr){ 52 add[o] = qv*1LL; 53 sum[o] = 1LL << qv; 54 return; 55 } 56 Push_down(o); 57 int mid = (l+r)/2; 58 if(ql <= mid) Update(o<<1,l,mid); 59 if(qr > mid) Update(o<<1|1,mid+1,r); 60 Push_up(o); 61 } 62 63 LL Query(int o,int l,int r){ 64 if(ql <= l && r <= qr){ 65 return sum[o]; 66 } 67 Push_down(o); 68 LL ans = 0LL; 69 int mid = (l+r)/2; 70 if(ql <= mid) ans |= Query(o<<1,l,mid); 71 if(qr > mid) ans |= Query(o<<1|1,mid+1,r); 72 return ans; 73 } 74 75 void solve(){ 76 memset(add,0,sizeof(add)); 77 Build(1,1,n); 78 int cmd,c,v; 79 for(int i = 1;i <= m;i++){ 80 scanf("%d",&cmd); 81 if(cmd == 1){ 82 scanf("%d %d",&v,&c); 83 ql = id[v],qr = last[v],qv = c; 84 // printf("ql = %d qr = %d qv = %d\n",ql,qr,qv); 85 Update(1,1,n); 86 } 87 else{ 88 scanf("%d",&v); 89 LL res = 0LL; 90 ql = id[v],qr = last[v]; 91 res = Query(1,1,n); 92 // printf("i = %d res = %I64d\n",i,res); 93 int zz = 0; 94 for(int i = 1;i <= 61;i++){ 95 if(res & (1LL<<i)) zz++; 96 } 97 printf("%d\n",zz); 98 } 99 } 100 } 101 102 int main(){ 103 while(scanf("%d %d",&n,&m) != EOF){ 104 for(int i = 1;i <= n;i++) g[i].clear(); 105 for(int i = 1;i <= n;i++) scanf("%d",&a[i]); 106 int u,v; 107 for(int i = 1;i <= n-1;i++){ 108 scanf("%d %d",&u,&v); 109 g[u].push_back(v); 110 g[v].push_back(u); 111 } 112 memset(vis,0,sizeof(vis)); 113 cnt = 0; 114 dfs(1); 115 // for(int i = 1;i <= n;i++) printf("id[%d] = %d last[%d] = %d\n",i,id[i],i,last[i]); 116 memset(b,0,sizeof(b)); 117 for(int i = 1;i <= n;i++) b[id[i]] = a[i]; 118 solve(); 119 } 120 return 0; 121 }
cf 633d 633D - Fibonacci-ish
好神奇
又wa又t的
再用一个map存下已经枚举过的对
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<map> 6 using namespace std; 7 8 typedef long long LL; 9 int a[1005],n; 10 map<LL,int> h; 11 map<pair<int,int>,int> hh; 12 LL f[1005]; 13 int ans,len; 14 15 void dfs(int d){ 16 f[d] = f[d-1]+f[d-2]; 17 if(h[f[d]] <= 0){ 18 len = d-1; 19 return; 20 } 21 h[f[d]]--; 22 dfs(d+1); 23 h[f[d]]++; 24 } 25 26 void solve(){ 27 sort(a+1,a+n+1); 28 ans = 2; 29 hh.clear(); 30 for(int i = 1;i <= n;i++){ 31 for(int j = 1;j <= n;j++){ 32 if(i == j) continue; 33 if(hh[make_pair(a[i],a[j])] != 0) continue; 34 hh[make_pair(a[i],a[j])] = 1; 35 f[1] = 1LL*a[i];f[2] = 1LL*a[j]; 36 37 h[f[1]]--;h[f[2]]--; 38 len = 2; 39 dfs(3); 40 // printf("f[1] = %I64d f[2] = %I64d len = %d\n",f[1],f[2],len); 41 ans = max(ans,len); 42 h[f[1]]++;h[f[2]]++; 43 } 44 } 45 printf("%d\n",ans); 46 } 47 48 int main(){ 49 while(scanf("%d",&n) != EOF){ 50 h.clear(); 51 for(int i = 1;i <=n;i++){ 52 scanf("%d",&a[i]); 53 h[1LL*a[i]]++; 54 } 55 solve(); 56 } 57 return 0; 58 }
以上是关于第一周 2.29 --- 3.6的主要内容,如果未能解决你的问题,请参考以下文章