little sub and his geometry problem

链接:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5852
思路:首先我们将所有点按x排序,然后枚举x,对于横坐标小于等于x的位置加入树状数组,然后y轴倒着枚举(因为随着x增大如果有解y一定在不断减少),然后用树状数组判断下当前x坐标上是否存在答案即可,复杂度O(nqlogn)

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

using namespace std;
int T;
typedef long long ll;

int n;
const int maxn = 1e5+100;
ll c[maxn];
int nu[maxn];

int (int x){
return x&(-x);
}

void add(int x,int d){
while(x<maxn){
c[x]+=d;
x+=lowbit(x);
}
}

void add1(int x,int d){
while(x<maxn){
nu[x]+=d;
x+=lowbit(x);
}
}

ll sum(int x){
ll ret = 0;
while(x){
ret+=c[x];
x-=lowbit(x);
}
return ret;
}
int sum1(int x){
int ret = 0;
while(x){
ret+=nu[x];
x-=lowbit(x);
}
return ret;
}

typedef pair<int,int> P;
P r[maxn];
int k;
int q;
int num;
ll s;

ll check(int x,int yy){
return 1LL*sum1(x)*(x+yy)-sum(x);
}

int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&k);
for(int i=0;i<k;i++)scanf("%d%d",&r[i].first,&r[i].second);
sort(r,r+k);
scanf("%d",&q);
while(q--){
scanf("%lld",&s);
ll res = 0;
num = 0;
for(int i=0;i<=n;i++)c[i] = 0,nu[i] = 0;
int j = n;
for(int i=1;i<=n;i++){
while(r[num].first<=i&&num<k){
add(r[num].second,r[num].second+r[num].first);
add1(r[num].second,1);//用来统计有多少个点,计算总贡献
num++;
}
while(check(j,i)>s&&j){
j--;
}
if(!j)break;
//如果有解,答案++
if(check(j,i)==s)res++;
}
printf("%lld%c",res,!q?'n':' ');
}
}
return 0;
}