最近要补的题目太多,来不及写博客,先贴一下代码。
cdq 分治 三维偏序
#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
#define maxm 200005
#define ll long long int
#define INF 0x3f3f3f3f
int n,m;
int ans[maxn];
struct node{int x,y,z,cnt;
bool operator == (const node &rid)const{
return (x==rid.x)&&(y==rid.y)&&(z==rid.z);
}
}a[maxn],b[maxn];
bool cmp(node p,node q){
if(p.x!=q.x)return p.x < q.x;
if(p.y!=q.y)return p.y < q.y;
return p.z < q.z;///为什么不能写 <= 头一次发现这个还能导致RE
}
int c[maxm];
void add(int x,int y)
{
while(x<maxm){
c[x]+=y;
x+=x&(-x);
}
}
int query(int x){
int res=0;
while(x){
res+=c[x];
x-=x&(-x);
}
return res;
}
void cdq(int l,int r)
{
if(l==r)return;
int mid=(l+r)>>1;
cdq(l,mid);cdq(mid+1,r);
int t=0,p1=l,p2=mid+1;
while(p1<=mid&&p2<=r){
if(a[p1].y<=a[p2].y){
add(a[p1].z,1);
b[++t]=a[p1++];
}
else {
a[p2].cnt+=query(a[p2].z);
b[++t]=a[p2++];
}
}
while(p1<=mid){add(a[p1].z,1);b[++t]=a[p1++];}
while(p2<=r){a[p2].cnt+=query(a[p2].z);b[++t]=a[p2++];}
for(int i=l;i<=mid;i++)add(a[i].z,-1);
for(int i=1;i<=t;i++)a[l+i-1]=b[i];
}
int main()
{
//freopen("E:\\ACM\\coding\\testdata.in","r",stdin);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);
}
sort(a+1,a+n+1,cmp);
node tt=a[n];
int num=1;
for(int i=n-1;i>=1;i--){
if(tt==a[i]){a[i].cnt+=num;num++;}
else {tt=a[i];num=1;}
}
cdq(1,n);
for(int i=1;i<=n;i++){
ans[a[i].cnt]++;
}
for(int i=0;i<n;i++){
printf("%d\n",ans[i]);
}
return 0;
}
cdq分治 单点修改 区间和查询
#include<bits/stdc++.h>
using namespace std;
#define maxn 50005
#define maxm 200005
#define ll long long int
#define INF 0x3f3f3f3f
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
#define mem(a) memset(a,0,sizeof(a))
#define sqr(x) (x*x)
#define inf (ll)2e18+1
#define mod 998244353
#define ls x<<1
#define rs x<<1|1
int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
int T,n,m,a[maxn],cnt,cntq,ans[maxn];
struct node{int id,v,type;
bool operator < (const node &rid)const{
if(id!=rid.id)return id < rid.id;
return type < rid.type;
}
}q[maxn<<2],q1[maxn<<2];
void cdq(int l,int r){
if(l==r)return ;
int mid=(l+r)>>1;
cdq(l,mid);cdq(mid+1,r);
int p1=l,p2=mid+1,t=0;
int sum=0;
while(p1<=mid&&p2<=r){
//printf("sum = %d\n",sum);
if(q[p1]<q[p2]){
if(q[p1].type==1)sum+=q[p1].v;
q1[t++]=q[p1++];
}
else {
if(q[p2].type==2)ans[q[p2].v]-=sum;
else if(q[p2].type==3)ans[q[p2].v]+=sum;
q1[t++]=q[p2++];
}
}
while(p1<=mid){
//printf("sum = %d\n",sum);
if(q[p1].type==1)sum+=q[p1].v;
q1[t++]=q[p1++];
}
while(p2<=r){
if(q[p2].type==2)ans[q[p2].v]-=sum;
else if(q[p2].type==3)ans[q[p2].v]+=sum;
q1[t++]=q[p2++];
}
inc(i,0,t-1)q[l+i]=q1[i];
}
int main()
{
T=read();
int kase=0;
while(T--){
printf("Case %d:\n",++kase);
cnt=cntq=0;
n=read();
inc(i,1,n){a[i]=read();q[++cnt]={i,a[i],1};}
char s[6];
int x,y;
while(true){
scanf("%s",s);
if(s[0]=='Q'){
x=read();y=read();
q[++cnt]={x-1,++cntq,2};
q[++cnt]={y,cntq,3};
}
else if(s[0]=='S'){
x=read();y=read();
q[++cnt]={x,-y,1};
}
else if(s[0]=='A'){
x=read();y=read();
q[++cnt]={x,y,1};
}
else if(s[0]=='E')break;
}
cdq(1,cnt);
inc(i,1,cntq)printf("%d\n",ans[i]);
inc(i,1,cntq)ans[i]=0;
}
return 0;
}
整体二分 带修改区间第k大查询
#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
#define maxm 200005
#define ll long long int
#define INF 0x3f3f3f3f
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
#define mem(a) memset(a,0,sizeof(a))
#define sqr(x) (x*x)
#define inf (ll)2e18+1
#define mod 998244353
#define ls x<<1
#define rs x<<1|1
int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
int n,m,cnt,cntq;
struct node{int x,k,type,l,r,id;}q[maxn*3],q1[maxn*3],q2[maxn*3];///因为那个修改操作算两个...所以不是两倍是三倍
int a[maxn],ans[maxn];
int c[maxn];
void add(int x,int y){
while(x<maxn){
c[x]+=y;
x+=x&(-x);
}
}
int ask(int x){
int res=0;
while(x){
res+=c[x];
x-=x&(-x);
}
return res;
}
void solve(int qx,int qy,int l,int r){
//printf("%d %d\n",l,r);
if(qx>qy)return ;
if(l==r){
//inc(i,qx,qy)printf("-----------%d\n",l);
inc(i,qx,qy)if(q[i].type==0)ans[q[i].id]=l;
return ;
}
int mid=(l+r)>>1,t1=0,t2=0;
inc(i,qx,qy){
if(q[i].type){
if(q[i].k<=mid){
add(q[i].x,q[i].type);
q1[++t1]=q[i];
}
else q2[++t2]=q[i];
}
else {
int tmp=ask(q[i].r)-ask(q[i].l-1);
if(q[i].k<=tmp)q1[++t1]=q[i];
else {
q[i].k-=tmp;
q2[++t2]=q[i];
}
}
}
inc(i,1,t1)if(q1[i].type)add(q1[i].x,-q1[i].type);
inc(i,1,t1)q[qx+i-1]=q1[i];
inc(i,1,t2)q[qx+t1-1+i]=q2[i];
solve(qx,qx+t1-1,l,mid);solve(qx+t1,qy,mid+1,r);
}
int main()
{
n=read();m=read();
int x,y,z;char s[2];
inc(i,1,n){a[i]=read();q[++cnt].k=a[i];q[cnt].type=1;q[cnt].x=i; }
inc(i,1,m){
scanf("%s",s);
if(s[0]=='Q'){
x=read();y=read();z=read();
q[++cnt].l=x;q[cnt].r=y;q[cnt].k=z;
q[cnt].id=++cntq;q[cnt].type=0;
}
else {
x=read();y=read();
q[++cnt].x=x;q[cnt].k=a[x];q[cnt].type=-1;
q[++cnt].x=x;q[cnt].k=y;q[cnt].type=1;
a[x]=y;///这个值要跟着改变因为可能多次修改他
}
}
solve(1,cnt,-INF,INF);
inc(i,1,cntq)printf("%d\n",ans[i]);
return 0;
}