标签:nec tar only submit imu contain problem 数据 owb
4 6
10
5
6
2
7
FJ‘s four cows have heights 10, 5, 6, 2. We want to know how many contiguous subsequences have median at least 6.There are 10 possible contiguous subsequences to consider. Of these, only 7
have median at least 6. They are {10}, {6}, {10, 5}, {5, 6}, {6, 2}, {10,5, 6}, {10, 5, 6, 2}.
#include <bits/stdc++.h> //#define scan(x) scanf("%d",&x); //#define scan2(x,y) scanf("%d%d",&x,&y); //#define rg register int #define lowbit(x) x&(-x) using namespace std; typedef long long ll; const ll mod = 1e9 + 7; const ll INF = 1e18; const int M = 1e4 + 6; const int maxn = 6e5 + 10; struct BIT { ll n, c[maxn]; inline void init(ll _n_) { n = _n_; memset(c, 0, sizeof(c)); } inline void modify(int pos, ll val) { while (pos <= n) { c[pos] += val; pos += lowbit(pos); } } inline ll query(int pos) { ll res = 0; while (pos) { res += c[pos]; pos -= lowbit(pos); } return res; } } bit; int n, x, height[maxn]; int dp[maxn]; int main() { #ifndef ONLINE_JUDGE freopen("splay.txt", "r", stdin); #endif scanf("%d%d", &n, &x); for (register int i = 1; i <= n; ++i) { scanf("%d", &height[i]); dp[i] = dp[i - 1]; dp[i] += (height[i] >= x); } ll res = 0; bit.init(6 * n + 8); bit.modify(n + 1, 1); for (register int i = 1; i <= n; ++i) { res += bit.query(2 * dp[i] - i + n + 1); bit.modify(2 * dp[i] - i + n + 1, 1); } printf("%lld\n", res); return 0; }
5 7
3 4 7 1
1 3 2 20
1 4 17 18
4 5 25 3
1 2 10 1
3 5 4 14
2 4 6 5
1
There are 5 intersections and 7 directional roads. The first road connects from intersection 3 to intersection 4; the first GPS thinks this road takes 7 units of time to traverse, and the second GPS thinks it takes 1 unit of time, etc.
If FJ follows the path 1 -> 2 -> 4 -> 5, then the first GPS complains on the 1 -> 2 road (it would prefer the 1 -> 3 road instead). However, for the rest of the route 2 -> 4 -> 5, both GPSs are happy, since this is a shortest route from 2 to 5 according to each GPS.
#include <bits/stdc++.h> //#define scan(x) scanf("%d",&x); //#define scan2(x,y) scanf("%d%d",&x,&y); //#define rg register int #define lowbit(x) x&(-x) using namespace std; typedef long long ll; const ll mod = 1e9 + 7; const ll INF = 1e18; const int M = 1e4 + 6; const int maxn = 1e5 + 10; struct GPS_1 { int to, nx; ll val; } f[maxn]; struct GPS_2 { int to, nx; ll val; } s[maxn]; struct event { int to, nx; ll val; } o[maxn]; int n, m; ll dis1[M], dis2[M], dis[maxn]; int tot1, tot2, tot, head1[maxn], head2[maxn], head[maxn]; int vis[maxn]; inline void add_edge1(int u, int v, ll w) { f[++tot1].to = v; f[tot1].val = w; f[tot1].nx = head1[u]; head1[u] = tot1; } inline void add_edge2(int u, int v, ll w) { s[++tot2].to = v; s[tot2].val = w; s[tot2].nx = head2[u]; head2[u] = tot2; } inline void add_edge(int u, int v, ll w) { o[++tot].to = v; o[tot].val = w; o[tot].nx = head[u]; head[u] = tot; } inline void spfa1() { for (register int i = 1; i <= n; ++i)dis1[i] = INT_MAX / 2; queue<int> q; q.push(n); dis1[n] = 0; while (!q.empty()) { int cur = q.front(); q.pop(); vis[cur] = 0; for (register int i = head1[cur]; i; i = f[i].nx) { int to = f[i].to; if (dis1[to] > dis1[cur] + f[i].val) { dis1[to] = dis1[cur] + f[i].val; if (!vis[to]) { q.push(to); vis[to] = 1; } } } } } inline void spfa2() { for (register int i = 1; i <= n; ++i)dis2[i] = INT_MAX / 2, vis[i] = 0; queue<int> q; q.push(n); dis2[n] = 0; while (!q.empty()) { int cur = q.front(); q.pop(); vis[cur] = 0; for (register int i = head2[cur]; i; i = s[i].nx) { int to = s[i].to; if (dis2[to] > dis2[cur] + s[i].val) { dis2[to] = dis2[cur] + s[i].val; if (!vis[to]) { q.push(to); vis[to] = 1; } } } } } inline void spfa() { for (register int i = 1; i <= n; ++i)dis[i] = INT_MAX / 2, vis[i] = 0; queue<int> q; q.push(1); dis[1] = 0; while (!q.empty()) { int cur = q.front(); q.pop(); vis[cur] = 0; for (register int i = head[cur]; i; i = o[i].nx) { int to = o[i].to; if (dis[to] > dis[cur] + o[i].val) { dis[to] = dis[cur] + o[i].val; if (!vis[to]) { q.push(to); vis[to] = 1; } } } } } int main() { #ifndef ONLINE_JUDGE freopen("splay.txt", "r", stdin); #endif scanf("%d%d", &n, &m); ll p,q; for (register int i = 1, a, b; i <= m; ++i) { scanf("%d%d%lld%lld", &a, &b, &p, &q); add_edge1(b, a, p); add_edge2(b, a, q); } spfa1(); spfa2(); //printf("debug dis1[1] = %lld dis[2] = %lld\n", dis1[1], dis2[1]); for (register int i = 1; i <= n; ++i) { for (register int j = head1[i]; j; j = f[j].nx) { int to = f[j].to; int init = 2; if (dis1[to] == dis1[i] + f[j].val)--init; if (dis2[to] == dis2[i] + s[j].val)--init; add_edge(to, i, init); } } spfa(); printf("%lld\n", dis[n]); return 0; }
标签:nec tar only submit imu contain problem 数据 owb
原文地址:https://www.cnblogs.com/czy-power/p/11516760.html