codeforces C. Count Triangles
程序员文章站
2022-07-15 16:09:31
...
题目
题意:
给你,并且有,问有多少个。
思路:
对于,如果遍历的话的话,肯定就会超时了,因为对于每一个,如果加上全部的话,那么对应的区间就是,我们就可以用一个前缀和维护求出最后的的所有区间,然后我们要知道有多少那么我们遍历一下,然后求出再算一个前缀和代表着在前有多少个,那么用代替,那么我们求出那么是不是就是。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <cctype>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return ;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1:1;
ret = (c == '-') ? 0:(c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return ;
}
inline void out(ll x) {
if (x > 9) out(x / 10);
putchar(x % 10 + '0');
}
const int maxn = 1e6 + 10;
ll cnt[maxn] = {0};
int main() {
int A, B, C, D;
read(A), read(B), read(C), read(D);
for (int i = A; i <= B; i++) {
cnt[i + B]++;
cnt[i + C + 1]--;
}
for (int i = 0; i < maxn; i++) cnt[i] += cnt[i - 1];
for (int i = 0; i < maxn; i++) cnt[i] += cnt[i - 1];
ll ans = 0;
for (int i = C; i <= D; i++) {
ans += 1ll * (cnt[maxn - 1] - cnt[i]);
}
out(ans);
return 0;
}
推荐阅读
-
codeforces C. Count Triangles
-
codeforces 643 C - Count Triangles
-
Codeforces 1355 C. Count Triangles
-
codeforces C. K-th Not Divisible by n
-
Codeforces Round #320 (Div. 2) C. A Problem about Polyline ( 数学 )
-
Codeforces Round #654 (Div. 2)-C. A Cookie for You
-
Educational Codeforces Round 85 (Rated for Div. 2) C. Circle of Monsters(前缀和 预处理 贪心)
-
codeforces C. Circle of Monsters
-
Codeforces Round #651 (Div. 2) C. Number Game
-
codeforces 979 C. Kuro and Walking Route