P10814 【模板】离线二维数点

原题链接

题解

对于一段区间 [l,r] 我们可以在 r 的位置查询一次,然后利用差分的思想跑到l-1再查一次

虽然这样不行,但是可以先在 l1 的位置查询一次,然后再在 r 的位置查询一次,然后顺序遍历,每次遍历就把对应位置上的数激活,可以用树状数组

code

#include<bits/stdc++.h>
using namespace std;
/*
#define int long long
#define double long double
const ll inf=1e18;
const ll mod=1e9+7;

const ll N=4e5;
ll qpow(ll a,ll n)
{
    ll res=1;
    while(n)
    {
        if(n&1) res=res*a%mod;
        a=a*a%mod;
        n>>=1;
    }
    return res;
}
ll inv(ll x)
{
    return qpow(x,mod-2);
}
ll fa[2000005];
ll finds(ll now){return now==fa[now]?now:finds(fa[now]);}

vector<ll> G[200005];

ll dfn[200005],low[200005];
ll cnt=0,num=0;
ll in_st[200005]={0};
stack<ll> st;
ll belong[200005]={0};

void scc(ll now,ll fa)
{
    dfn[now]=++cnt;
    low[now]=dfn[now];
    in_st[now]=1;
    st.push(now);

    for(auto next:G[now])
    {
        if(next==fa) continue;

        if(!dfn[next])
        {
            scc(next,now);
            low[now]=min(low[now],low[next]);
        }
        else if(in_st[next])
        {
            low[now]=min(low[now],dfn[next]);
        }
    }

    if(low[now]==dfn[now])
    {
        ll x;
        num++;
        do
        {
            x=st.top();
            st.pop();
            in_st[x]=0;
            belong[x]=num;
        }while(x!=now);
    }
}
vector<int> prime;
bool mark[200005]={0};
void shai()
{
    for(int i=2;i<=200000;++)
    {
        if(!mark[i]) prime.push_back(i);

        for(auto it:prime)
        {
            if(it*i>200000) break;

            mark[it*i]=1;
            if(it%i==0) break;
        }
    }
}
*/

#define lowbit(x) ((x)&(-x))

vector<tuple<int,int,int> > Q[2000006];
int ans[2000006];
int a[2000006];

int tree[2000006]={0};

void update(int x)
{
    while(x<=2000000)
    {
        tree[x]++;
        x+=lowbit(x);
    }
}

int query(int x)
{
    int res=0;
    while(x)
    {
        res+=tree[x];
        x-=lowbit(x);
    }

    return res;
}
void solve()
{
    int n,m;
    cin>>n>>m;

    for(int i=1;i<=n;i++) cin>>a[i];

    for(int i=1;i<=m;i++)
    {
        int l,r,x;
        cin>>l>>r>>x;

        Q[l-1].push_back({x,-1,i});
        Q[r].push_back({x,1,i});
    }

    for(int i=1;i<=n;i++)//0开始的贡献没有影响
    {
        update(a[i]);
        for(auto it:Q[i])
        {
            auto [line,ctr,id]=it;
            ans[id]+=ctr*query(line);
        }
    }

    for(int i=1;i<=m;i++)
    {
        cout<<ans[i]<<'\n';
    }
}
signed main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int TT=1;
    //cin>>TT;
    while(TT--) solve();
    return 0;
}


posted @   纯粹的  阅读(32)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示