[BZOJ4994] [Usaco2017 Feb]Why Did the Cow Cross the Road III(树状数组)
生活随笔
收集整理的這篇文章主要介紹了
[BZOJ4994] [Usaco2017 Feb]Why Did the Cow Cross the Road III(树状数组)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
傳送門
?
1.每個數的左右位置預處理出來,按照左端點排序,因為左端點是從小到大的,我們只需要知道每條線段包含了多少個前面線段的右端點即可,可以用樹狀數組
2.如果 ai < bj < bi, 也就是說在兩個i之間只有一個j那么對答案的貢獻為1,所以可以用樹狀數組,當第一次出現一個數的時候,那個位置+1,當第二次出現的時候,第一次出現的位置再-1,也就是把它對答案的貢獻去掉,同樣是樹狀數組統計答案
?
#include <cstdio> #include <iostream> #define N 200001int n, ans; int sum[N], vis[N];inline int read() {int x = 0, f = 1;char ch = getchar();for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';return x * f; }inline int query(int x) {int ret = 0;for(; x; x -= x & -x) ret += sum[x];return ret; }inline void add(int x, int d) {for(; x <= n; x += x & -x) sum[x] += d; }int main() {int i, x;n = read() * 2;for(i = 1; i <= n; i++){x = read();if(!vis[x])vis[x] = i, add(i, 1);elseans += query(i) - query(vis[x]), add(vis[x], -1);}std::cout << ans;return 0; }
轉載于:https://www.cnblogs.com/zhenghaotian/p/7568676.html
總結
以上是生活随笔為你收集整理的[BZOJ4994] [Usaco2017 Feb]Why Did the Cow Cross the Road III(树状数组)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UWP入门(二) -- 基础笔记
- 下一篇: STM8,延时函数