General
 
 
# Author Problem Lang Verdict Time Memory Sent Judged  
86830100 Practice:
JKLover
283E - 25 GNU C++17 Accepted 560 ms 13488 KB 2020-07-14 15:30:54 2020-07-14 15:30:54
 
 
→ Source
//%std
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
	int out = 0, fh = 1;
	char jp = getchar();
	while ((jp > '9' || jp < '0') && jp != '-')
		jp = getchar();
	if (jp == '-')
		fh = -1, jp = getchar();
	while (jp >= '0' && jp <= '9')
		out = out * 10 + jp - '0', jp = getchar();
	return out * fh;
}
void print(int x)
{
	if (x >= 10)
		print(x / 10);
	putchar('0' + x % 10);
}
void write(int x, char c)
{
	if (x < 0)
		putchar('-'), x = -x;
	print(x);
	putchar(c);
}
const int N = 1e5 + 10;
ll C2(int x)
{
	return 1LL * x * (x - 1) / 2;
}
ll C3(int x)
{
	return 1LL * x * (x - 1) * (x - 2) / 6; 
}
int n, k, a[N], s[N << 2][2], tag[N << 2];
vector<pair<int, int> > opt[N];
void pushup(int x)
{
	for (int i = 0; i < 2; ++i)	
		s[x][i] = s[x << 1][i] + s[x << 1 | 1][i];
}
void BuildTree(int x, int l, int r)
{
	if (l == r)
	{
		s[x][0] = 1, s[x][1] = 0;
		return;
	}
	int mid = (l + r) >> 1;
	BuildTree(x << 1, l, mid);
	BuildTree(x << 1 | 1, mid + 1, r);
	pushup(x);
}
void modify(int x)
{
	swap(s[x][0], s[x][1]);
	tag[x] ^= 1;	
}
void pushdown(int x)
{
	if (tag[x])
	{
		modify(x << 1);
		modify(x << 1 | 1);
		tag[x] = 0;
	}
}
void upd(int x, int l, int r, int L, int R)
{
	if (L > R)
		return;
	if (L <= l && r <= R)
		return modify(x);
	int mid = (l + r) >> 1;
	pushdown(x);
	if (L <= mid)
		upd(x << 1, l, mid, L, R);
	if (R > mid)
		upd(x << 1 | 1, mid + 1, r, L, R);
	pushup(x);
}
int query(int x, int l, int r, int L, int R, int c)
{
	if (L > R)
		return 0;
	if (L <= l && r <= R)
		return s[x][c];
	int res = 0, mid = (l + r) >> 1;
	pushdown(x);
	if (L <= mid)
		res += query(x << 1, l, mid, L, R, c);
	if (R > mid)
		res += query(x << 1 | 1, mid + 1, r, L, R, c);
	return res;	
}
int main()
{
	n = read(), k = read();
	for (int i = 1; i <= n; ++i)
		a[i] = read();
	sort(a + 1, a + 1 + n);
	for (int i = 1; i <= k; ++i)
	{
		int l = read(), r = read();
		l = lower_bound(a + 1, a + 1 + n, l) - a;
		r = upper_bound(a + 1, a + 1 + n, r) - a - 1;
		opt[l].push_back(make_pair(l, r));
		opt[r + 1].push_back(make_pair(l, r));
	}
	BuildTree(1, 1, n);
	ll ans = C3(n);
	for (int i = 1; i <= n; ++i)
	{
		for (auto p : opt[i])
			upd(1, 1, n, p.first, p.second);
		int outdeg = query(1, 1, n, 1, i - 1, 0) + query(1, 1, n, i + 1, n, 1);
		ans -= C2(outdeg);
	}
	cout << ans << '\n';
	return 0;
}
 
 
?
Time: ? ms, memory: ? KB
Verdict: ?
Input
?
Participant's output
?
Jury's answer
?
Checker comment
?
Diagnostics
?
Click to see test details