bzoj1935
题意:给出n棵树的位置(xi,yi),有m次询问,每次询问一个以(Xi,Yi)为左下角,(Li,Ri)为右上角的矩形内树的个数
题解:显然的三维偏序问题,一维是询问的时间T,一维是横坐标,一维是纵坐标。把初始的n棵树的位置当作插入,把询问当成4个二维前缀和相加减,由于时间是按照输入的顺序,所以第一维不需要排序,直接cdq分治处理第二维,树状数组维护第三维即可
/**************************************************************
Problem: 1935
User: Altria_
Language: C++
Result: Accepted
Time:7168 ms
Memory:278892 kb
****************************************************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//#define debug(x) cout << #x << " is " << x << endl;
const int maxn = 5e5+1000;
const int maxnw = 1e7+1000;
struct Op{
ll x;
ll y;
int type;
int id;
}op[maxn*5],op2[maxn*5];
ll tot,ans[maxn],tim,sz[maxnw],fl[maxnw],maxx;
void update(int x){
for(int i=x;i<=maxx;i+=(i&(-i))){
if(fl[i]!=tim){
fl[i]=tim;
sz[i]=0;
}
sz[i]++;
}
}
ll query(int x){
ll ac=0;
for(int i=x;i>0;i-=(i&(-i))){
if(fl[i]==tim){
ac+=sz[i];
}
}
return ac;
}
void cdq(int l,int r){
if(l>=r)return;
int mid=(l+r)>>1;
cdq(l,mid);
cdq(mid+1,r);
int L=l;
int R=mid+1;
int t1=l;
tim++;
while(L<=mid&&R<=r){
if(op[L].x<=op[R].x){
if(op[L].type==0){
update(op[L].y);
}
op2[t1++]=op[L++];
}
else{
if(op[R].type)ans[op[R].id]+=op[R].type*query(op[R].y);
op2[t1++]=op[R++];
}
}
while(L<=mid){
op2[t1++]=op[L++];
}
while(R<=r){
if(op[R].type)ans[op[R].id]+=op[R].type*query(op[R].y);
op2[t1++]=op[R++];
}
for(int i=l;i<=r;i++){
op[i]=op2[i];
}
}
int main() {
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
++tot;
scanf("%lld%lld",&op[tot].x,&op[tot].y);
op[tot].x=op[tot].x+4;
op[tot].y=op[tot].y+4;
maxx=max(op[tot].y,maxx);
op[tot].type=0;
op[tot].id=0;
}
for(int i=1;i<=m;i++){
ll a,b,c,d;
ans[i]=0;
scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
a+=4;
b+=4;
c+=4;
d+=4;
maxx=max(max(b,d),maxx);
op[++tot].id=i;
op[tot].type=-1;
op[tot].x=a-1;
op[tot].y=d;
op[++tot].id=i;
op[tot].type=-1;
op[tot].x=c;
op[tot].y=b-1;
op[++tot].id=i;
op[tot].type=1;
op[tot].x=a-1;
op[tot].y=b-1;
op[++tot].id=i;
op[tot].type=1;
op[tot].x=c;
op[tot].y=d;
}
cdq(1,tot);
for(int i=1;i<=m;i++){
printf("%lld\n",ans[i]);
}
return 0;
}
bzoj1176
题意:有一个w*w的矩阵,每个格子内数字的初始值为s,有m次操作,操作有两种:1、格子(xi,yi)数字增加 2、求某个子矩阵格子数字之和
题解:和上题一样
/**************************************************************
Problem: 1176
User: Altria_
Language: C++
Result: Accepted
Time:6184 ms
Memory:46692 kb
****************************************************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout << #x << " is " << x << endl;
const int maxn = 2e5+1000;
const int maxnw = 2e6+1000;
struct Op{
ll x;
ll y;
int type;
ll val;
int id;
}op[maxn],op2[maxn];
ll ans[maxn