U125682
经典应用,注意到输入的时候就是按照坐标来排序给出的,所以直接把固定就好了(这里给固定)
#include <bits/stdc++.h>
using namespace std ;
long long n , f[20005] , c[33005] ;
struct node {
long long x , y ;
} a[20005] ;
bool cmp(node x , node y) {
return (x.x != y.x ? x.x < y.x : x.y < y.y) ;
}
long long query(long long x) {
long long ans = 0 ;
while(x > 0) {
ans += c[x] ;
x -= (x & -x) ;
}
return ans ;
}
void add(long long x) {
while(x <= 32001) {
c[x] ++ ;
x += (x & -x) ;
}
}
int main() {
cin >> n ;
for(int i = 1 ; i <= n ; i ++) {
cin >> a[i].x >> a[i].y ;
a[i].y ++ ;
}
sort(a + 1 , a + n + 1 , cmp) ;
for(int i = 1 ; i <= n ; i ++) {
f[query(a[i].y)] ++ ;
add(a[i].y) ;
}
for(int i = 0 ; i < n ; i ++) {
cout << f[i] << "\n" ;
}
return 0 ;
}- 时间复杂度:
- 空间复杂度:
P5094
- 首先把奶牛按照排序,这样子每头奶牛在他的前缀里都是最大的,取消了
- 然后对于每一头奶牛会提供,然后分成左边和右边
- 然后用两个树状数组,一个来维护数量,一个来维护坐标和
- 最后面展开推公式就好了。
#include <bits/stdc++.h>
using namespace std ;
long long n , c[2][50005] , ans , mod = 998244353 ;
struct node {
long long x , v ;
} a[200005] ;
bool cmp(node x , node y) {
return x.v < y.v ;
}
long long query(long long x , long long id) {
long long ans = 0 ;
while(x > 0) {
ans += c[id][x] ;
x -= (x & -x) ;
}
return ans ;
}
void add(long long x , long long id , long long y) {
while(x <= 50001) {
c[id][x] += y ;
x += (x & -x) ;
}
}
int main() {
cin >> n ;
for(int i = 1 ; i <= n ; i ++) {
cin >> a[i].v >> a[i].x ;
}
sort(a + 1 , a + n + 1 , cmp) ;
for(int i = 1 ; i <= n ; i ++) {
long long l = a[i].v * (a[i].x * query(a[i].x , 0) - query(a[i].x , 1)) % mod ;
long long r = a[i].v * ((query(50001 , 1) - query(a[i].x , 1)) - a[i].x * (query(50001 , 0) - query(a[i].x , 0))) % mod ;
ans = (ans + l + r) % mod ;
add(a[i].x , 0 , 1) ;
add(a[i].x , 1 , a[i].x) ;
}
cout << ans ;
return 0 ;
}- 时间复杂度:
- 空间复杂度:
P3608
按照上一题直接求出左边和右边比他高然后看符不符合条件就好了。
#include <bits/stdc++.h>
using namespace std ;
long long n , c[100005] , ans ;
struct node {
long long x , id ;
} a[100005] ;
bool cmp(node x , node y) {
return (x.x != y.x ? x.x > y.x : x.id < y.id) ;
}
long long query(long long x) {
long long ans = 0 ;
while(x > 0) {
ans += c[x] ;
x -= (x & -x) ;
}
return ans ;
}
void add(long long x , long long y) {
while(x <= 100001) {
c[x] += y ;
x += (x & -x) ;
}
}
int main() {
cin >> n ;
for(int i = 1 ; i <= n ; i ++) {
cin >> a[i].x ;
a[i].id = i ;
}
sort(a + 1 , a + n + 1 , cmp) ;
for(int i = 1 ; i <= n ; i ++) {
long long l = query(a[i].id - 1) ;
long long r = query(100000) - query(a[i].id) ;
add(a[i].id , 1) ;
ans += (min(l , r) * 2 < max(l , r)) ;
}
cout << ans ;
return 0 ;
}- 时间复杂度:
- 空间复杂度:
POJ2481
- 如果我们把一个区间当做二维平面的坐标,那么就跟摘星星一个样子了,这里我们按照第一关键字为从小到大第二关键字从大到小排序
- 注意这里的坐标会有重叠所以如果相邻的两个点重叠了那么这一个的答案直接继承上一个的,否则才计算
- 不管重叠没重叠都要加点不然后面的就会少算
- 因为他要你输出每颗星星的答案所以结构体里面还要接一个
#include <bits/stdc++.h>
using namespace std ;
long long t , n , c[200005] , ans[200005] ;
struct node {
long long x , y , id ;
} a[200005] ;
bool cmp(node x , node y) {
return (x.x != y.x ? x.x < y.x : x.y > y.y) ;
}
long long query(long long x) {
long long ans = 0 ;
while(x > 0) {
ans += c[x] ;
x -= (x & -x) ;
}
return ans ;
}
void add(long long x) {
while(x <= 1e5) {
c[x] ++ ;
x += (x & -x) ;
}
}
int main() {
while(cin >> n && n != 0) {
fill(c + 1 , c + 100000 + 1 , 0) ;
for(int i = 1 ; i <= n ; i ++) {
cin >> a[i].x >> a[i].y ;
a[i].id = i ;
}
sort(a + 1 , a + n + 1 , cmp) ;
for(int i = 1 ; i <= n ; i ++) {
if(a[i].x == a[i - 1].x && a[i].y == a[i - 1].y) {
ans[a[i].id] = ans[a[i - 1].id] ;
} else {
ans[a[i].id] = i - 1 - query(a[i].y - 1) ;
}
add(a[i].y) ;
}
for(int i = 1 ; i <= n ; i ++) {
cout << ans[i] << " " ;
}
cout << "\n" ;
}
return 0 ;
} - 时间复杂度:
- 空间复杂度:
UESTC383
#include <bits/stdc++.h>
using namespace std ;
long long t , n , m , k , c[10005] , ans ;
struct node {
long long x , y ;
} a[1000005] ;
bool cmp(node x , node y) {
return (x.x != y.x ? x.x < y.x : x.y < y.y) ;
}
long long query(long long x) {
long long ans = 0 ;
while(x > 0) {
ans += c[x] ;
x -= (x & -x) ;
}
return ans ;
}
void add(long long x) {
while(x <= m) {
c[x] ++ ;
x += (x & -x) ;
}
}
int main() {
cin >> t ;
for(int _ = 1 ; _ <= t ; _ ++) {
cin >> n >> m >> k ;
fill(c + 1 , c + 10001 , ans = 0) ;
for(int i = 1 ; i <= k ; i ++) {
cin >> a[i].x >> a[i].y ;
}
sort(a + 1 , a + k + 1 , cmp) ;
for(int i = 1 ; i <= k ; i ++) {
ans += i - 1 - query(a[i].y) ;
add(a[i].y) ;
}
cout << "Test case " << _ << ": " << ans << "\n" ;
}
return 0 ;
}P3657
#include <bits/stdc++.h>
using namespace std ;
long long n , a[100005] , c[100005] , b[100005] , pos[100005] ;
void add(long long x , long long y) {
for( ; x <= n ; ) {
c[x] = max(c[x] , y);
x += (x & -x) ;
}
}
long long query(long long x) {
long long ans = 0 ;
while(x) {
ans = max(ans , c[x]) ;
x -= (x & -x) ;
}
return ans ;
}
int main() {
cin >> n ;
for(int i = 1 ; i <= n ; i ++) {
cin >> a[i] ;
}
for(int i = 1 ; i <= n ; i ++) {
cin >> b[i] ;
pos[b[i]] = i ;
}
for(int i = 1 ; i <= n ; i ++) {
vector <pair <long long , long long>> g ;
for(long long j = max(1ll , a[i] - 4) ; j <= min(n , a[i] + 4) ; j ++) {
g.push_back({pos[j] , query(pos[j] - 1) + 1}) ;
}
for(auto x : g) {
add(x.first , x.second) ;
}
}
cout << query(n) ;
return 0 ;
}P14419
#include <bits/stdc++.h>
using namespace std;
long long n , tot , d[300005] , q[300005] , h[300005] , a[300005] , c[300005] , ans , sum ;
long long lowbit(long long x) {
return x & (-x) ;
}
void add(long long x , long long k) {
for(int i = x ; i <= tot ; i += lowbit(i)) {
c[i] += k ;
}
}
long long query(long long l , long long r) {
long long sum = 0 ;
for(int i = r ; i > 0 ; i -= lowbit(i)) {
sum += c[i] ;
}
for(int i = l - 1 ; i > 0 ; i -= lowbit(i)) {
sum -= c[i] ;
}
return sum ;
}
int main() {
cin >> n ;
for(int i = 1 ; i <= n ; i ++) {
cin >> d[i] , a[i] = d[i] ;
}
sort(a + 1 , a + n + 1) ;
tot = unique(a + 1 , a + n + 1) - (a + 1) ;
for(int i = 1 ; i <= n ; i ++) {
d[i] = lower_bound(a + 1 , a + tot + 1 , d[i]) - a ;
}
for(long long i = 1; i <= n; i++){
ans = query(d[i] + 1 , tot) ;
q[i] = ans ;
add(d[i] , 1) ;
}
memset(c , ans = 0 , sizeof c) ;
for(int i = n ; i >= 1 ; i --){
ans = query(d[i] + 1 , tot) ;
h[i] = ans ;
add(d[i] , 1) ;
}
for(int i = 1 ; i <= n + 1 ; i ++){
sum += min(q[i] , h[i]) ;
}
cout << sum ;
return 0 ;
}