跳到正文

题解

题解:P10814 【模板】离线二维数点

kevinZ99约 4 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 4 分钟
获赞
2
题目
P10814

版本

  1. v1当前

题意

都读的懂吧

思路

很明显将询问离线,然后将询问的 xx 排序,然后将 aa,数组排序,每一次询问将小于 xx 的数的位置标成 1 ,然后求一下区间和就行了。

代码

C++
//Author:Kevin Z K Y
#include <bits/stdc++.h>
#define up(a,b,c) for(int (a)=(b);(a)<=(c);(a)++)
#define dn(a,b,c) for(int (a)=(b);(a)>=(c);(a)--)
#define fst first
#define sed second
using namespace std;
using ll = long long ;
using pii = pair<int,int> ;using pll = pair<ll,ll> ;
using vi = vector<int> ;using vl = vector<ll> ;
using vpi = vector<pii> ;using vpl = vector<pll> ;
using db = double ;namespace mystl{
	#define gc() getchar()
	ll qpow(ll a,ll b,const ll&p){if (a==0ll) return 0ll; ll c=1ll;
		while(b) { if(b & 1) c=a*c%p; a=a*a%p; b>>=1; } return c; }
	template<typename T>void read(T&x) {x=0; bool f=false; char ch;
		ch = gc(); while(ch<'0'||ch>'9') f |= ( ch=='-') , ch=gc();
		while(ch>='0'&&ch<='9') x=x*10+ch-'0' , ch=gc(); x=f?-x:x;}
	template<typename T>void write(T x){char s[40];short d=0;T y=x;
		if(x<0) putchar('-'),y=-y;if(x==0){ putchar('0'); return; }
		while(y){s[++d]=y%10+'0';y/=10;}while(d>0)putchar(s[d--]);}
	template<typename T>void wris(T x,char c){write(x);putchar(c);}
}using namespace mystl;
namespace my{
	const int N=2000005;
	int n,m;
	struct question{
		int l,r,x,id;
	};
	bool cmp(question x,question y){
		return x.x<y.x;
	}
	vector<question> q;
	int ans[N];
	struct node{
		int x,id;
	}a[N];
	bool cmpd(node x,node y){
		return x.x<y.x;
	}
	int c[N];
	void add(int x,int y){for(;x<=n;x+=x&-x)c[x]+=y;}
	int ask(int x){int y=0;for(;x;x=x-=x&(-x))y+=c[x];return y;}
	void solve(){
		cin>>n>>m;
		up(i,1,n)cin>>a[i].x,a[i].id=i;
		sort(a+1,a+1+n,cmpd);
		up(i,1,m){
			int l,r,x;cin>>l>>r>>x;
			q.emplace_back((question){l,r,x,i});
		}
		sort(q.begin(),q.end(),cmp);
		int idx=1;
		up(i,0,m-1){
			while(a[idx].x<=q[i].x&&idx<=n){
				add(a[idx].id,1);
				idx++;
			}
//			cout<<q[i].l<<' '<<q[i].r<<' '<<q[i].x<<"\n";
			ans[q[i].id]=ask(q[i].r)-ask(q[i].l-1);
		}up(i,1,m)cout<<ans[i]<<'\n';
	}
}
int main(){
//  freopen("","r",stdin);
//  freopen("","w",stdout);
	ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
	int _=1;while(_--)my::solve();return 0;
}
原文