您的位置:首页 > 其它

4822: [Cqoi2017]老C的任务&&1935: [Shoi2007]Tree 园丁的烦恼

2017-09-19 17:10 471 查看
题目自己看吧,

题解:

把询问拆成四个,按x坐标扫描,按y坐标用树状数组维护。

4822:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int N = 1E5 + 10;
const int Max = ~0U>>1;
const int Min = -(1LL << 31LL);
typedef long long LL;
int n,m,n1,n2,cx,cy;
int px
,py
,p
,ax
,ay
;
struct node{
int y,typ,num;
node(int y,int num,int typ):
y(y),num(num),typ(typ){}
};
vector<pair<int,int> >P
;
vector<node>q
;
void ins(int x,int y,int Num,int typ)
{
int nx = lower_bound(px + 1,px + cx + 1,x) - px;
int ny = lower_bound(py + 1,py + cy + 1,y) - py;
if (px[nx] > x) --nx; if (py[ny] > y) --ny;
q[nx].push_back(node(ny,Num,typ));
}
void prepare()
{
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&px[i],&py[i],&p[i]);
ax[i]=px[i];ay[i]=py[i];
}
cx=1;px[++n1]=Min;px[++n1]=Max;
cy=1;py[++n2]=Min;py[++n2]=Max;
sort(px+1,px+1+n1);
for(int i=2;i<=n1;i++)
if(px[i]!=px[i-1]) px[++cx]=px[i];
sort(py+1,py+1+n2);
for(int i=2;i<=n2;i++)
if(py[i]!=py[i-1]) py[++cy]=py[i];
for(int i=1;i<=n;i++)
{
ax[i]=lower_bound(px+1,px+1+cx,ax[i])-px;
ay[i]=lower_bound(py+1,py+1+cy,ay[i])-py;
P[ax[i]].push_back(make_pair(ay[i],p[i]));
}
//*********************离散化********************
//printf("!");
for(int i=1;i<=m;i++)
{
int x1,x2,y1,y2;
scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
ins(y1,y2,i,1);
if(x1>Min) ins(x1-1,y2,i,-1);
if(x2>Min) ins(y1,x2-1,i,-1);
if(x1>Min&&x2>Min) ins(x1-1,x2-1,i,1);
}
/*cx = 1; ax[++n1] = Min; ax[++n1] = Max;
cy = 1; ay[++n2] = Min; ay[++n2] = Max;
sort(ax + 1,ax + n1 + 1);
for (int i = 2; i <= n1; i++)
if (ax[i] != ax[i - 1]) ax[++cx] = ax[i];
sort(ay + 1,ay + n2 + 1);
for (int i = 2; i <= n2; i++)
if (ay[i] != ay[i - 1]) ay[++cy] = ay[i];
for (int i = 1; i <= n; i++)
{
px[i] = lower_bound(ax + 1,ax + cx + 1,px[i]) - ax;
py[i] = lower_bound(ay + 1,ay + cy + 1,py[i]) - ay;
P[px[i]].push_back(make_pair(py[i],p[i]));
}
for (int i = 1; i <= m; i++)
{
int xa,ya,xb,yb;
scanf("%d%d%d%d",&xa,&ya,&xb,&yb);
Add(xb,yb,i,1);
if (xa > Min) Add(xa - 1,yb,i,-1);
if (ya > Min) Add(xb,ya - 1,i,-1);
if (xa > Min && ya > Min) Add(xa - 1,ya - 1,i,1);
} */
}
LL sum
,ans
;
int main()
{
scanf("%d%d",&n,&m);
n1=n2=n;
prepare();
//printf("!");
for(int i=1;i<=cx;i++)
{
for(int j=0;j<P[i].size();j++)
{
for(int u=P[i][j].first;u<=cy;u+=u&-u)
sum[u]+=1LL*P[i][j].second;
}
for(int j=0;j<q[i].size();j++)
{
node yu=q[i][j];LL tot=0;
for(int u=yu.y;u>0;u-=u&-u) tot+=sum[u];
ans[yu.num]+=1LL*tot*yu.typ;
}
}
for(int i=1;i<m;i++)
printf("%lld\n",ans[i]);
printf("%lld",ans[m]);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: