偏序问题的记录

初步学习偏序问题

1.Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama【树状数组】

#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
#define ull unsigned long long
#define PI acos(-1)
#define pb(x)   push_back(x)
#define il inline
#define re register
#define IO; ios::sync_with_stdio(0);cin.tie(0);
//#define ls (o<<1)
//#define rs (o<<1|1)
#define pii pair<int,int>
using namespace std;
const int maxn = 2e5+5;
const int maxm = 400010;
const int INF = 0x3f3f3f3f;
const ll LINF = 3e17+1;
const int mod = 1e9+7;
ll n, r, m;
inline int read(){
    
    
    register int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){
    
    if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){
    
    x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return (f==1)?x:-x;
}
ll c[maxn];
ll a[maxn];
vector<int>vec[maxn];
ll lowbit(ll x)
{
    
    
    return x&-x;
}
void update(ll x)
{
    
    
    while (x <= n)
    {
    
    
        c[x] ++;
        x += lowbit(x);
    }
}
ll getsum(ll x)
{
    
    
    ll res = 0;
    while (x)
    {
    
    
        res += c[x];
        x -= lowbit(x);
    }
    return res;
}
int main()
{
    
    
    IO;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
    
    
        cin >> a[i];
        a[i] = min(a[i],n);
        vec[a[i]].pb(i);
    }
    ll ans = 0;
    for (int i = n; i >= 1; i--)
    {
    
    
        for (auto in : vec[i])
        {
    
    
            update(in);
        }
        ans += getsum(min((ll)i-1,a[i]));
    }
    cout << ans << endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43563956/article/details/107287143