A.洛谷 P1637
题意
给定含有 个整数的序列 中,请你求出 且 的三元组个数。
思路
对于每个 ,用两个树状数组维护前后的数值,然后在移动时将左侧树状数组的元素增加,右侧的树状数组减少
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 10, V = 1e5 + 1;
int n, a[N], b[N], sum;
struct tree{
int n, a[N];
void init(int n){
this->n = n;
for(int i = 1; i <= n; i++){
a[i] = 0;
}
}
void add(int x, int v) {
for (; x <= n; x += x & -x) {
a[x] += v;
}
}
int sum(int x) {
int ans = 0;
for (; x; x -= x & -x) {
ans += a[x];
}
return ans;
}
}t1, t2;
signed main(){
cin >> n;
t1.init(V), t2.init(V);
for(int i = 1; i <= n; i++){
cin >> a[i];
if(i > 1)
t2.add(a[i], 1);
}
for(int i = 2; i < n; i++){
t2.add(a[i], -1);
t1.add(a[i - 1], 1);
sum += t1.sum(a[i] - 1) * (t2.sum(V - 1) - t2.sum(a[i]));
}
cout << sum;
return 0;
}
B.CodeForces 597C
题意
给定一个包含 个不同元素的序列,找出具有 个元素的递增子序列的数量。
思路
对于每个序列长度,求出每个长度的子序列数量,接着放入树状数组中,最后求和统计答案
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 10, V = 1e5 + 1;
int n, k, sum;
int cnt[15][N], a[N];
struct tree{
int n, a[N];
void init(int n){
this->n = n;
for(int i = 1; i <= n; i++){
a[i] = 0;
}
}
void add(int x, int v) {
for (; x <= n; x += x & -x) {
a[x] += v;
}
}
int sum(int x) {
int ans = 0;
for (; x; x -= x & -x) {
ans += a[x];
}
return ans;
}
}t[15];
signed main(){
cin >> n >> k;
k++;
for(int i = 1; i <= k; i++){
t[i].init(V);
}
for(int i = 1; i <= n; i++){
cin >> a[i];
cnt[1][i] = 1;
}
for(int i = 2; i <= k; i++){
for(int j = 1; j <= n; j++){
cnt[i][j] += t[i].sum(a[j]);
t[i].add(a[j], cnt[i - 1][j]);
}
}
for(int i = 1; i <= n; i++){
sum += cnt[k][i];
}
cout << sum;
return 0;
}C.洛谷 P2163
题意
给定 个节点坐标 ,接着给定 次询问,每次询问给出四个整数 ,求以 为左下角, 为右上角的矩形内部(包括边界)有多少个节点。
思路
- 将输入的节点与询问节点一起排序,标记类型,和加减
- 用容斥原理进行计算
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 5e7 + 10, V = 1e7 + 10;
int n, m, ans[N];
struct P{
int x, y, tag, id;
};
vector<P> g;
bool cmp(const P &a, const P &b){
if(a.y != b.y) return a.y < b.y;
return a.tag == 0;
}
struct tree{
int n, a[N];
void init(int n){
this->n = n;
for(int i = 1; i <= n; i++){
a[i] = 0;
}
}
void add(int x, int v) {
for (; x <= n; x += x & (-x)) {
a[x] += v;
}
}
int sum(int x) {
int ans = 0;
for (; x; x -= x & (-x)) {
ans += a[x];
}
return ans;
}
}t;
int main(){
cin >> n >> m;
t.init(V - 1);
for(int i = 1, x, y; i <= n; i++){
cin >> x >> y;
x += 5, y += 5;
g.push_back({x, y, 0, i});
}
for(int i = 1, l1, r1, l2, r2; i <= m; i++){
cin >> l1 >> r1 >> l2 >> r2;
l1 += 5, l2 += 5, r1 += 5, r2 += 5;
g.push_back({l1 - 1, r1 - 1, 1, i});
g.push_back({l2, r2, 1, i});
g.push_back({l1 - 1, r2, -1, i});
g.push_back({l2, r1 - 1, -1, i});
}
sort(g.begin(), g.end(), cmp);
for(int i = 0; i < g.size(); i++){
auto [x, y, tag, id] = g[i];
if(tag == 0){
t.add(x, 1);
}
else if(tag == 1){
ans[id] += t.sum(x);
}
else if(tag == -1){
ans[id] -= t.sum(x);
}
}
for(int i = 1; i <= m; i++){
cout << ans[i] << "\n";
}
return 0;
}D.AtCoder abc393_f
题意
给定一个长度为 的整数序列 。请处理 个查询。第 个查询如下:
- 给定整数 和 。求数列 的(不一定连续的)子序列中,满足严格单调递增且所有元素不超过 的最长长度。保证 。
思路
- 数据较大需要离散化
- 按 从小到大排序,用扫描线处理答案
注意
排序比较器 相同将点放在前面,如果都为点,则将 id 大的放前面
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, V = 2e5;
int n, q, h[N], ans[N];
vector<int> b;
int Q(int x){
return lower_bound(b.begin(), b.end(), x) - b.begin() + 1;
}
struct P{
int r, x;
}w[N];
struct tree{
int n = V, a[N];
void init(){
for(int i = 1; i <= n; i++){
a[i] = 0;
}
}
void add(int x, int v) {
for (; x <= n; x += x & (-x)) {
a[x] = max(a[x], v);
}
}
int sum(int x) {
int ans = 0;
for (; x; x -= x & (-x)) {
ans = max(a[x], ans);
}
return ans;
}
}t;
struct stc{
int x, tag, id;
};
vector<stc> g;
bool cmp(const stc &a, const stc &b){
if(a.x != b.x) return a.x < b.x;
if(a.tag != b.tag) return a.tag == 0;
return a.id > b.id;
}
int main(){
t.init();
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> h[i];
b.push_back(h[i]);
}
for(int i = 1; i <= q; i++){
cin >> w[i].r >> w[i].x;
b.push_back(w[i].x);
}
sort(b.begin(), b.end());
b.erase(unique(b.begin(), b.end()), b.end());
for(int i = 1; i <= n; i++){
h[i] = Q(h[i]);
//cout << h[i] << " ";
g.push_back({h[i], 0, i});
}
//cout << "****\n";
for(int i = 1; i <= q; i++){
w[i].x = Q(w[i].x);
//cout << w[i].x << " ";
g.push_back({w[i].x, 1, i});
}
//cout << "****\n";
sort(g.begin(), g.end(), cmp);
for(stc i : g){
auto [x, tag, id] = i;
if(tag == 0){
t.add(id, t.sum(id - 1) + 1);
}
else{
ans[id] = t.sum(w[id].r);
}
}
for(int i = 1; i <= q; i++){
cout << ans[i] << "\n";
}
return 0;
}