1 /* 2 3 4 #include <bits/stdc++.h> 5 6 using namespace std; 7 typedef long long ll; 8 const int N=1e5+10; 9 const ll inf=0x3f3f3f3f3f3f3f3f; 10 struct node { 11 ll mi, mx; 12 }t[N]; 13 ll ans,a[N],b[N] 14 void build(int rt,int l,int r) { 15 t[rt].mx = -inf; 16 t[rt].mi = inf; 17 if (l == r) { 18 t[rt].mi = sum[l]; 19 t[rt].mx = sum[l]; 20 return; 21 } 22 int mid = (l + r) >> 1; 23 build(rt << 1, l, mid); 24 build(rt << 1 | 1, mid + 1, r); 25 t[rt].mx = max(t[rt << 1].mx, t[rt << 1 | 1].mx); 26 t[rt].mi = max(t[rt << 1].mi, t[rt << 1 | 1].mi); 27 } 28 29 ll Max(int rt,int L,int R,int l,int r) { 30 if (L <= l && r <= R) { 31 return t[rt].mx; 32 return; 33 } 34 ll ans = -inf; 35 int mid = (l + r) >> 1; 36 if (L <= mid) { 37 ans = max(ans, Max(rt << 1, L, R, l, mid)); 38 } 39 if (R > mid) { 40 ans = max(ans, Max(rt << 1 | 1, L, R, mid + 1, r)); 41 } 42 return ans; 43 } 44 45 ll Min(int rt,int L,int R,int l,int r) { 46 if (L <= l && r <= R) { 47 return t[rt].mi; 48 return; 49 } 50 ll ans = inf; 51 int mid = (l + r) >> 1; 52 if (L <= mid) { 53 ans = min(ans, Min(rt << 1, L, R, l, mid)); 54 } 55 if (R > mid) { 56 ans = min(ans, Min(rt << 1 | 1, L, R, mid + 1, r)); 57 } 58 return ans; 59 } 60 61 int main() { 62 ans=inf; 63 scanf("%d", &n); 64 for (int i = 1; i <= n; i++) { 65 scanf("%d", &a[i]); 66 } 67 for (int i = 1; i <= n; i++) { 68 scanf("%d", &b[i]); 69 sum[i] = sum[i - 1] + b[i]; 70 } 71 build(1, 1, n); 72 stack<int> sta; 73 for (int i = n; i >= 1; i++) { 74 while (!sta.empty() && a[sta.top()] <= a[i]) { 75 sta.pop(); 76 } 77 if (sta.empty()) { 78 r[i] = n + 1; 79 } else { 80 r[i] = sta.top(); 81 } 82 sta.push(i); 83 } 84 while (!sta.empty()) { 85 sta.pop(); 86 } 87 for (int i = 1; i <= n; i++) { 88 while (!sta.empty() && a[sta.top()] <= a[i]) { 89 sta.pop(); 90 } 91 if (sta.empty()) { 92 l[i] = 0; 93 } else { 94 l[i] = sta.top(); 95 } 96 sta.push(i); 97 } 98 for (int i = 1; i <= n; i++) { 99 int x = a[i], left = l[i] + 1, right = r[i] - 1, y; 100 if (x >= 0) { 101 y = Max(1, i, right, 1, n) - Min(1, left - 1, i - 1, 1, n); 102 } else { 103 y = Min(1, i, right, 1, n) - Max(1, left - 1, i - 1, 1, n); 104 } 105 ans=max(ans,x*y); 106 } 107 } 108 109 */ 110 111 #include<bits/stdc++.h> 112 113 using namespace std; 114 const int N=7e6; 115 typedef long long ll; 116 char s[N]; 117 int sum[N],len,cnt[N]; 118 int main(){ 119 scanf("%s",s+1); 120 len=strlen(s+1); 121 for (int i=1;i<=len;i++){ 122 sum[i]=(sum[i-1]+s[i]-'0')%3; 123 } 124 ll ans=0; 125 for (int i=1;i<=len;i++){ 126 if (s[i]=='0'){ 127 ans++; 128 if (s[i-1]=='0'){ 129 ans+=cnt[sum[i]]; 130 } 131 } 132 cnt[sum[i-1]]++; 133 } 134 printf("%d ",ans); 135 }
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N=1000100; 5 const int M=9e6+10; 6 struct node 7 { 8 int next,to,w; 9 } e[M]; 10 int h[N],v[N],d[N],tot; 11 12 int n,m,s,t,k,ans; 13 void add(int u,int v,int w){ 14 tot++; 15 e[tot].to=v; 16 e[tot].w=w; 17 e[tot].next=h[u]; 18 h[u]=tot; 19 } 20 21 priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >q; 22 void dij(int s){ 23 memset(d,0x3f3f3f3f,sizeof(d)); 24 d[s]=0; 25 q.push(make_pair(0,s)); 26 while (!q.empty()){ 27 int u=q.top().second; 28 q.pop(); 29 if (v[u]){ 30 continue; 31 } 32 v[u]=1; 33 for (int i=h[u];i;i=e[i].next){ 34 int to=e[i].to; 35 if (d[to]>d[u]+e[i].w){ 36 d[to]=d[u]+e[i].w; 37 q.push(make_pair(d[to],to)); 38 } 39 } 40 } 41 } 42 int main() 43 { 44 scanf("%d%d%d%d%d",&n,&m,&s,&t,&k); 45 for (int i=1; i<=m; i++) 46 { 47 int u,v,w; 48 scanf("%d%d%d",&u,&v,&w); 49 add(u,v,w); 50 add(v,u,w); 51 for (int j=1; j<=k; j++) 52 { 53 add(u+n*(j-1),v+j*n,0); 54 add(v+n*(j-1),u+j*n,0); 55 add(u+n*j,v+j*n,w); 56 add(v+n*j,u+j*n,w); 57 } 58 } 59 dij(s); 60 ans=0x3f3f3f3f; 61 for (int i=0;i<=k;i++){ 62 ans=min(ans,d[k*n+t]); 63 } 64 printf("%d ",ans); 65 return 0; 66 }
1 #include<bits/stdc++.h> 2 const int N=1e5+10; 3 using namespace std; 4 struct node{ 5 int t,n; 6 }e[N*2]; 7 int n,m,k,kk; 8 int d[N],a[N],h[N],t; 9 10 void add(int u,int v){ 11 t++; 12 e[t].t=v; 13 e[t].n=h[u]; 14 h[u]=t; 15 } 16 void bfs(int x) { 17 memset(d, 0, sizeof(d)); 18 d[x] = 1; 19 queue<int> q; 20 q.push(x); 21 while (!q.empty()) { 22 int u = q.front(); 23 q.pop(); 24 for (int i = h[u]; i; i = e[i].n) { 25 int v = e[i].t; 26 if (!d[v]) { 27 d[v] = d[u] + 1; 28 q.push(v); 29 } 30 } 31 } 32 } 33 int main(){ 34 scanf("%d%d",&n,&m); 35 for (int i=1;i<n;i++){ 36 int u,v; 37 scanf("%d%d",&u,&v); 38 add(u,v); 39 add(v,u); 40 } 41 for (int i=1;i<=m;i++){ 42 scanf("%d",&a[i]); 43 } 44 bfs(a[1]); 45 for (int i=1;i<=m;i++){ 46 if (d[a[i]]>k){ 47 k=d[a[i]]; 48 kk=a[i]; 49 } 50 } 51 bfs(kk); 52 k=0; 53 kk=0; 54 for (int i=1;i<=m;i++){ 55 if (d[a[i]]>k){ 56 k=d[a[i]]; 57 kk=a[i]; 58 } 59 } 60 printf("%d ",k/2); 61 }
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 typedef long long ll; 5 const int N=3e6+10; 6 const ll inf=0x3f3f3f3f3f3f3f3f; 7 struct node { 8 ll mi, mx; 9 }t[N*4]; 10 ll a[N],b,sum[N],n,r[N],l[N]; 11 12 void build(int rt,int l,int r) { 13 if (l == r) { 14 t[rt].mi = sum[l]; 15 t[rt].mx = sum[l]; 16 return; 17 } 18 int mid = (l + r) >> 1; 19 build(rt << 1, l, mid); 20 build(rt << 1 | 1, mid + 1, r); 21 t[rt].mx = max(t[rt << 1].mx, t[rt << 1 | 1].mx); 22 t[rt].mi = min(t[rt << 1].mi, t[rt << 1 | 1].mi); 23 } 24 25 ll Max(int rt,int L,int R,int l,int r) { 26 if (l==r){ 27 return t[rt].mx; 28 } 29 if (L <= l && r <= R) { 30 return t[rt].mx; 31 } 32 ll ans = -inf; 33 int mid = (l + r) >> 1; 34 if (L <= mid) { 35 ans=max(ans, Max(rt << 1, L, R, l, mid)); 36 } 37 if (R > mid) { 38 ans=max(ans, Max(rt << 1 | 1, L, R, mid + 1, r)); 39 } 40 return ans; 41 } 42 43 ll Min(int rt,int L,int R,int l,int r) { 44 if (l == r) { 45 return t[rt].mi; 46 } 47 if (L <= l && r <= R) { 48 return t[rt].mi; 49 } 50 ll ans = inf; 51 int mid = (l + r) >> 1; 52 if (L <= mid) { 53 ans = min(ans, Min(rt << 1, L, R, l, mid)); 54 } 55 if (R > mid) { 56 ans = min(ans, Min(rt << 1 | 1, L, R, mid + 1, r)); 57 } 58 return ans; 59 } 60 61 int main() { 62 scanf("%lld", &n); 63 for (int i = 1; i <= n; i++) { 64 scanf("%lld", &a[i]); 65 } 66 for (int i = 1; i <= n; i++) { 67 scanf("%lld", &b); 68 sum[i] = sum[i - 1] + b; 69 } 70 build(1, 1, n); 71 stack<int> sta; 72 for (int i = n; i >= 1; i--) { 73 while (!sta.empty() && a[sta.top()] >= a[i]) { 74 sta.pop(); 75 } 76 if (sta.empty()) { 77 r[i] = n + 1; 78 } else { 79 r[i] = sta.top(); 80 } 81 sta.push(i); 82 } 83 while (!sta.empty()) { 84 sta.pop(); 85 } 86 for (int i = 1; i <= n; i++) { 87 while (!sta.empty() && a[sta.top()] >= a[i]) { 88 sta.pop(); 89 } 90 if (sta.empty()) { 91 l[i] = 0; 92 } else { 93 l[i] = sta.top(); 94 } 95 sta.push(i); 96 } 97 ll ans = -inf; 98 for (int i = 1; i <= n; i++) { 99 ll left = l[i] + 1, right = r[i] - 1, y1, y2; 100 if (a[i] >= 0) { 101 y1 = Max(1, i, right, 1, n); 102 y2 = Min(1, left - 1, i - 1, 1, n); 103 } else { 104 y1 = Min(1, i, right, 1, n); 105 y2 = Max(1, left - 1, i - 1, 1, n); 106 } 107 ans = max(ans, a[i]*(y1 - y2)); 108 } 109 printf("%lld ", ans); 110 }
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 typedef long long ll; 6 7 vector<ll>vec[5]; 8 9 ll a; 10 int main() { 11 int t; 12 scanf("%d", &t); 13 while (t--) { 14 scanf("%lld", &a); 15 for (int i = 0; i < 3; i++) { 16 vec[i].clear(); 17 } 18 if (a % 3 == 0) { 19 printf("1 %lld ", a); 20 continue; 21 } 22 ll now = 1, x = a; 23 while (x) { 24 if (x & 1) { 25 vec[now % 3].push_back(now); 26 } 27 now <<= 1; 28 x = x >> 1; 29 } 30 printf("2 "); 31 if (a % 3 == 1) { 32 if (vec[1].size() >= 2) { 33 ll p = vec[1][0], q = vec[1][1]; 34 printf("%lld %lld ", a - p, a - q); 35 } 36 if (vec[1].size() == 1) { 37 ll p = vec[1][0], q = vec[2][0]; 38 printf("%lld %lld ", a - p, p + q); 39 } 40 if (vec[1].size() == 0) { 41 ll p = vec[2][0], q = vec[2][1], r = vec[2][2]; 42 printf("%lld %lld ", a - p - q, p + q + r); 43 } 44 } else { 45 if (vec[2].size() >= 2) { 46 ll p = vec[2][0], q = vec[2][1]; 47 printf("%lld %lld ", a - p, a - q); 48 } 49 if (vec[2].size() == 1) { 50 ll p = vec[2][0], q = vec[1][0]; 51 printf("%lld %lld ", a - p, p + q); 52 } 53 if (vec[2].size() == 0) { 54 ll p = vec[1][0], q = vec[1][1], r = vec[1][2]; 55 printf("%lld %lld ", a - p - q, p + q + r); 56 } 57 } 58 } 59 }