新的一周
话说这周开始考试了....
4.25
昨天去西电的校赛
过了一年,又去签到了....O_<
uestc的数据结构专题
A 卿学姐与公主
最基础的线段树的单点更新


1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 7 typedef long long LL; 8 const int maxn = 4e6+5; 9 int n,m,q; 10 LL maxx[maxn],a[maxn]; 11 int ql,qr,pos,v; 12 13 void pushup(int o){ 14 maxx[o] = max(maxx[o<<1],maxx[o<<1|1]); 15 } 16 17 void Build(int o,int l,int r){ 18 if(l == r){ 19 maxx[o] = 0; 20 return; 21 } 22 int mid = (l+r)/2; 23 Build(o<<1,l,mid); 24 Build(o<<1|1,mid+1,r); 25 pushup(o); 26 } 27 28 void Update(int o,int l,int r){ 29 if(l == r){ 30 maxx[o] += 1LL*v; 31 return; 32 } 33 int mid = (l+r)/2; 34 if(pos <= mid) Update(o<<1,l,mid); 35 else Update(o<<1|1,mid+1,r); 36 pushup(o); 37 } 38 39 LL Query(int o,int l,int r){ 40 if(ql <= l && r <= qr){ 41 return maxx[o]; 42 } 43 int mid = (l+r)/2; 44 LL ans = 0LL; 45 if(ql <= mid) ans = max(ans,Query(o<<1,l,mid)); 46 if(qr > mid) ans = max(ans,Query(o<<1|1,mid+1,r)); 47 return ans; 48 } 49 50 void solve(){ 51 Build(1,1,n); 52 int cmd; 53 for(int i = 1;i <= q;i++){ 54 scanf("%d",&cmd); 55 if(cmd == 1){ 56 scanf("%d %d",&pos,&v); 57 Update(1,1,n); 58 } 59 else{ 60 scanf("%d %d",&ql,&qr); 61 LL ans = Query(1,1,n); 62 printf("%lld\n",ans); 63 } 64 } 65 } 66 67 int main(){ 68 while(scanf("%d %d",&n,&q) != EOF){ 69 solve(); 70 } 71 return 0; 72 }
4.26
N 秋实大哥搞算数
算一个算术表达式的值,不含括号
啊.....不知道栈的经典应用(求算术表达式 TwT......),上来开始敲模拟.....越敲越敲不下去.....
搜了下题解.....又get一点点
先把题目给的中缀表达式 转化成 后缀表达式,再用一个栈去求解
wa 了两发,,是因为,每次碰到一个操作符的时候,需要从栈里面弹两个数出来,但是有时候栈里面不够两个数,就用0来算
还有就是 表达式 最开始 可能 是 -1+5这样


1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<stack> 6 #include<vector> 7 using namespace std; 8 9 typedef long long LL; 10 const int maxn = 1e6+5; 11 char s[maxn],t[maxn]; 12 int n; 13 LL p[20]; 14 15 LL suan(string tmp){ 16 int len = tmp.length(); 17 LL res = 0LL; 18 for(int i = 0;i < len;i++) { 19 res += 1LL*(tmp[i]-'0')*p[len-i-1]; 20 } 21 return res; 22 } 23 24 int Rank(char c){ 25 if(c == '+' || c == '-') return 1; 26 if(c == '*' || c == '/') return 2; 27 } 28 29 vector<string> num; 30 31 void solve(){ 32 num.clear(); 33 n = strlen(t+1); 34 if(t[1] == '-'){ 35 s[1] = '0'; 36 for(int i = 2;i <= n+1;i++) s[i] = t[i-1]; 37 n++; 38 } 39 else{ 40 for(int i = 1;i <= n;i++) s[i] = t[i]; 41 } 42 43 stack<char> ss; 44 for(int i = 1;i <= n;){ 45 if(s[i] >= '0' && s[i] <= '9'){ 46 int j = i; 47 string tmp; 48 while(j <= n && (s[j] >= '0' && s[j] <= '9')){ 49 tmp += s[j]; 50 j++; 51 } 52 i = j; 53 num.push_back(tmp); 54 } 55 else{ 56 int x = Rank(s[i]); 57 // printf("s[%d] = %c\n",i,s[i]); 58 while(!ss.empty() && x <= Rank(ss.top())){ 59 string now; 60 now += ss.top(); 61 num.push_back(now);ss.pop(); 62 } 63 ss.push(s[i]); 64 i++; 65 } 66 } 67 while(!ss.empty()){ 68 string now; 69 now += ss.top(); 70 num.push_back(now);ss.pop(); 71 } 72 //for(int i = 0;i < num.size();i++) printf("%s",num[i].c_str()); 73 //printf("\n"); 74 stack<LL> sss; 75 for(int i = 0;i < num.size();i++) { 76 string now = num[i]; 77 if(now != "+" && now != "-" && now != "*" && now != "/"){ 78 LL y = suan(now); 79 //printf("y = %I64d\n",y); 80 sss.push(y); 81 } 82 else{ 83 LL x,y; 84 if(sss.empty()) x = 0LL; 85 else { 86 x = sss.top();sss.pop(); 87 } 88 if(sss.empty()) y = 0LL; 89 else{ 90 y = sss.top();sss.pop(); 91 } 92 93 if(now == "+"){ 94 sss.push(x+y); 95 } 96 if(now == "-"){ 97 sss.push(y-x); 98 } 99 if(now == "*"){ 100 sss.push(x*y); 101 } 102 if(now == "/"){ 103 sss.push(y/x); 104 } 105 } 106 } 107 printf("%lld\n",sss.top()); 108 } 109 110 int main(){ 111 int T; 112 p[0] = 1LL; 113 for(int i = 1;i <= 18;i++) p[i] = p[i-1]*10LL; 114 scanf("%d",&T); 115 while(T--){ 116 scanf("%s",t+1); 117 solve(); 118 } 119 return 0; 120 }
4.27
看了一题,,不会捉....
4.28
wawawawawa......
4.29
补西电校赛的题
一神教的,用并查集维护个数,算贡献
话说用I64d wa,,要用 lld


1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 7 typedef long long LL; 8 const int maxn = 1e5+5; 9 int sz[maxn],fa[maxn]; 10 int n; 11 12 struct Edge{ 13 int u,v,w; 14 }e[maxn]; 15 16 int cmp1(Edge n1,Edge n2){ 17 return n1.w < n2.w; 18 } 19 20 int cmp2(Edge n1,Edge n2){ 21 return n1.w > n2.w; 22 } 23 24 int find(int x){return x == fa[x]? x : fa[x] = find(fa[x]);} 25 26 void solve(){ 27 LL maxx = 0LL; 28 LL minn = 0LL; 29 sort(e+1,e+n,cmp1); 30 for(int i = 1;i <= n;i++) fa[i] = i,sz[i] = 1; 31 32 for(int i = 1;i <= n-1;i++){ 33 int u = e[i].u; 34 int v = e[i].v; 35 int w = e[i].w; 36 int x = find(u); 37 int y = find(v); 38 LL tmp = 1LL*sz[x]*sz[y]*w; 39 if(x != y){ 40 fa[x] = y; 41 sz[y] += sz[x]; 42 } 43 maxx += tmp; 44 } 45 //printf("maxx = %I64d\n",maxx); 46 47 sort(e+1,e+n,cmp2); 48 for(int i = 1;i <= n;i++) fa[i] = i,sz[i] = 1; 49 50 for(int i = 1;i <= n-1;i++){ 51 int u = e[i].u; 52 int v = e[i].v; 53 int w = e[i].w; 54 int x = find(u); 55 int y = find(v); 56 LL tmp = 1LL*sz[x]*sz[y]*w; 57 if(x != y){ 58 fa[x] = y; 59 sz[y] += sz[x]; 60 } 61 minn += tmp; 62 } 63 //printf("minn = %I64d\n",minn); 64 LL ans = maxx - minn; 65 printf("%lld\n",ans); 66 67 } 68 69 int main(){ 70 while(scanf("%d",&n) != EOF){ 71 for(int i = 1;i <= n-1;i++){ 72 scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].w); 73 } 74 solve(); 75 } 76 return 0; 77 }
又好久没做 leetcode 了....
下周开始各种考试了TwT/
leetcode 225 Implement Stack using Queues
两个队列实现一个栈


1 class Stack{ 2 public: 3 queue<int>q,q2; 4 void push(int x){ 5 q.push(x); 6 } 7 8 void pop(){ 9 while(q.size() > 1){ 10 q2.push(q.front()); 11 q.pop(); 12 } 13 q.pop(); 14 while(q2.size() > 0){ 15 q.push(q2.front()); 16 q2.pop(); 17 } 18 } 19 20 int top(){ 21 int tt; 22 while(q.size() > 0){ 23 tt = q.front(); 24 q2.push(q.front()); 25 q.pop(); 26 } 27 while(q2.size() > 0){ 28 q.push(q2.front()); 29 q2.pop(); 30 } 31 return tt; 32 } 33 34 bool empty(){ 35 return q.empty(); 36 } 37 };
4.30
leetcode 155 Min Stack
实现一个能够知道当前最小值的栈
另外再维护一个最小值的栈就好了


1 class MinStack{ 2 public: 3 stack<int> s,minn; 4 void push(int x){ 5 if(s.empty()){ 6 s.push(x); 7 minn.push(x); 8 } 9 else{ 10 s.push(x); 11 if(x <= minn.top()) minn.push(x); 12 } 13 } 14 15 void pop(){ 16 if(s.top() == minn.top()){ 17 minn.pop(); 18 } 19 s.pop(); 20 } 21 22 int top(){ 23 return s.top(); 24 } 25 26 int getMin(){ 27 return minn.top(); 28 } 29 };
5.1
劳动节彩蛋....
bc 爆零....
诶....sbsbsbsbsb
leetcode 110 Balanced Binary Tree
判断一颗二叉树是否是平衡的二叉树


1 class Solution{ 2 public: 3 int h(TreeNode* root){ 4 if(root == NULL) return 0; 5 if(root->left == NULL && root->right == NULL) return 1; 6 return max(h(root->left),h(root->right))+1; 7 } 8 9 bool isBalanced(TreeNode* root){ 10 if(root == NULL) return true; 11 if(abs(h(root->left) - h(root->right)) > 1) return false; 12 return isBalanced(root->left) && isBalanced(root->right); 13 } 14 };