답안 #503577

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
503577 2022-01-08T11:08:50 Z mars4 Examination (JOI19_examination) C++17
100 / 100
1259 ms 17608 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 

using namespace std;
using namespace __gnu_pbds; 

#define ff              first
#define ss              second
#define ll              int64_t
#define ld              long double
#define nl              cout<<"\n"
#define all(v)          v.begin(),v.end()
#define mset(a,v)       memset((a),(v),sizeof(a))
#define forn(i,a,b)     for(int64_t i=int64_t(a);i<int64_t(b);++i)
#define forb(i,a,b)     for(int64_t i=int64_t(a);i>=int64_t(b);--i)
#define fastio()        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

#define mod         1'000'000'007
#define mod2        998'244'353 
#define inf         1'000'000'000'000'007
#define pi          3.14159265358979323846

template<class key,class cmp=std::less<key>>
using ordered_set=tree<key,null_type,cmp,rb_tree_tag,tree_order_statistics_node_update>;

template<class L,class R> ostream& operator<<(ostream& out,pair<L,R> &p)        {return out<<"("<<p.ff<<", "<<p.ss<<")";}
template<class T> ostream& operator<<(ostream& out,vector<T> &v)                {out<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())out<<", ";out<<*it;}return out<<"]";}
template<class T> ostream& operator<<(ostream& out,deque<T> &v)                 {out<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())out<<", ";out<<*it;}return out<<"]";}
template<class T> ostream& operator<<(ostream& out,set<T> &s)                   {out<<"{";for(auto it=s.begin();it!=s.end();++it){if(it!=s.begin())out<<", ";out<<*it;}return out<<"}";}
template<class T> ostream& operator<<(ostream& out,ordered_set<T> &s)           {out<<"{";for(auto it=s.begin();it!=s.end();++it){if(it!=s.begin())out<<", ";out<<*it;}return out<<"}";}
template<class L,class R> ostream& operator<<(ostream& out,map<L,R> &m)         {out<<"{";for(auto it=m.begin();it!=m.end();++it){if(it!=m.begin())out<<", ";out<<*it;}return out<<"}";}

void dbg_out() {cerr<<"]\n";}
template<typename Head,typename... Tail> 
void dbg_out(Head H,Tail... T) {cerr<<H;if(sizeof...(Tail))cerr<<", ";dbg_out(T...);}
#ifdef LOCAL
	#define dbg(...) cerr<<"["<<#__VA_ARGS__<<"] = [",dbg_out(__VA_ARGS__)
#else
	#define dbg(...)
#endif

//---------------------------------mars4---------------------------------

class Fentree
{
	ll N;
	ll logN;

	ll sum(ll i)
	{
		ll res=0;
		for(;i;i-=i&(-i))
			res+=fentree[i];
		return res;
	}

	public:
	vector<ll> fentree;

	void init(ll n)
	{
		N=n;
		logN=65-__builtin_clzll(n);
		fentree=vector<ll>(N+1);
	}

	void build(vector<ll> &a)
	{
		for(ll i=1;i<=N;i++)
		{
			fentree[i]+=a[i-1];
			if(i+(i&-i)<=N)
				fentree[i+(i&-i)]+=fentree[i];
		}
	}

	void update(ll i,ll val)
	{
		for(i++;i<=N;i+=i&(-i))
			fentree[i]+=val;
	}

	ll sum(ll l,ll r)
	{
		return sum(r+1)-sum(l);
	}

	ll kth_element(ll k)
	{
		ll val=0;
		ll pos=0;
		for(ll i=logN;i>=0;i--)
		{
			ll r=1ll<<i;
			if(pos+r<=N and val+fentree[pos+r]<k)
			{
				val+=fentree[pos+r];
				pos+=r;
			}
		}
		return pos;
	}
};

const ll B=320;

struct query 
{
	ll l,r,c,id;
	bool operator < (const query &o) const 
	{
		if(l/B==o.l/B) 
		{
			if((l/B)%2==0) 
				return r<o.r;
			else 
				return r>o.r;
		} 
		else 
			return l<o.l;
	}
};

vector<pair<ll,ll>> a;
vector<pair<ll,ll>> b;
vector<ll> c;
vector<ll> cnt;
Fentree f;
ll N,M;

class Mo
{
	ll L=-1;
	ll R=-1;

	void updatel(ll i)
	{
		auto [val,id]=a[i];
		cnt[id]++;
		if(cnt[id]==2)
		{
			f.update(c[id],1);
		}
	}

	void removel(ll i)
	{
		auto [val,id]=a[i];
		if(cnt[id]==2)
		{
			f.update(c[id],-1);
		}
		cnt[id]--;
	}

	void updater(ll i)
	{
		auto [val,id]=b[i];
		cnt[id]++;
		if(cnt[id]==2)
		{
			f.update(c[id],1);
		}
	}

	void remover(ll i)
	{
		auto [val,id]=b[i];
		if(cnt[id]==2)
		{
			f.update(c[id],-1);
		}
		cnt[id]--;
	}

	public:
	ll query(query q)
	{
		ll l=q.l;
		ll r=q.r;
		ll c=q.c;
		while(L>=0 and a[L].ff>=l)
		{
			updatel(L);
			L--;
		}
		while(L+1<N and a[L+1].ff<l)
		{
			L++;
			removel(L);
		}
		while(R>=0 and b[R].ff>=r)
		{
			updater(R);
			R--;
		}
		while(R+1<N and b[R+1].ff<r)
		{
			R++;
			remover(R);
		}
		return f.sum(c,N-1);
	}
};

int main()
{
	fastio();
	ll z,n,m,t,k,i,j,l,d,h,r;
	cin>>n>>m;
	N=n,M=m;
	a=vector<pair<ll,ll>>(n);
	b=vector<pair<ll,ll>>(n);
	c=vector<ll>(n);
	cnt=vector<ll>(n,2);
	vector<ll> a2;
	vector<ll> b2;
	vector<ll> c2;
	forn(i,0,n)
	{
		cin>>a[i].ff>>b[i].ff;
		a[i].ss=i;
		b[i].ss=i;
		c[i]=a[i].ff+b[i].ff;
		a2.push_back(a[i].ff);
		b2.push_back(b[i].ff);
		c2.push_back(c[i]);
	}
	vector<query> q(m);
	forn(i,0,m)
	{
		cin>>q[i].l>>q[i].r>>q[i].c;
		q[i].id=i;
	}
	sort(all(a2));
	sort(all(b2));
	sort(all(c2));
	for(auto &[l,r]:a)
	{
		l=lower_bound(all(a2),l)-a2.begin();
	}
	for(auto &[l,r]:b)
	{
		l=lower_bound(all(b2),l)-b2.begin();
	}
	for(auto &l:c)
	{
		l=lower_bound(all(c2),l)-c2.begin();
	}
	for(auto &[l,r,c,id]:q)
	{
		l=lower_bound(all(a2),l)-a2.begin();
		r=lower_bound(all(b2),r)-b2.begin();
		c=lower_bound(all(c2),c)-c2.begin();
	}
	f.init(n);
	for(ll i:c)
	{
		f.update(i,1);
	}
	sort(all(a));
	sort(all(b));
	sort(all(q));
	Mo mo;
	vector<ll> ans(m);
	for(auto qu:q)
	{
		ans[qu.id]=mo.query(qu);
	}
	for(ll i:ans)
	{
		cout<<i<<"\n";
	}
	cerr<<"\nTime elapsed: "<<1000*clock()/CLOCKS_PER_SEC<<"ms\n";
	return 0;
}

Compilation message

examination.cpp: In function 'int main()':
examination.cpp:210:5: warning: unused variable 'z' [-Wunused-variable]
  210 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |     ^
examination.cpp:210:11: warning: unused variable 't' [-Wunused-variable]
  210 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |           ^
examination.cpp:210:13: warning: unused variable 'k' [-Wunused-variable]
  210 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |             ^
examination.cpp:210:15: warning: unused variable 'i' [-Wunused-variable]
  210 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |               ^
examination.cpp:210:17: warning: unused variable 'j' [-Wunused-variable]
  210 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                 ^
examination.cpp:210:19: warning: unused variable 'l' [-Wunused-variable]
  210 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                   ^
examination.cpp:210:21: warning: unused variable 'd' [-Wunused-variable]
  210 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                     ^
examination.cpp:210:23: warning: unused variable 'h' [-Wunused-variable]
  210 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                       ^
examination.cpp:210:25: warning: unused variable 'r' [-Wunused-variable]
  210 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 316 KB Output is correct
2 Correct 0 ms 312 KB Output is correct
3 Correct 0 ms 312 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 9 ms 828 KB Output is correct
8 Correct 9 ms 844 KB Output is correct
9 Correct 9 ms 836 KB Output is correct
10 Correct 9 ms 780 KB Output is correct
11 Correct 6 ms 716 KB Output is correct
12 Correct 5 ms 712 KB Output is correct
13 Correct 8 ms 820 KB Output is correct
14 Correct 8 ms 816 KB Output is correct
15 Correct 9 ms 820 KB Output is correct
16 Correct 4 ms 716 KB Output is correct
17 Correct 7 ms 716 KB Output is correct
18 Correct 2 ms 716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1153 ms 12980 KB Output is correct
2 Correct 1140 ms 15224 KB Output is correct
3 Correct 1147 ms 15220 KB Output is correct
4 Correct 860 ms 14520 KB Output is correct
5 Correct 185 ms 14452 KB Output is correct
6 Correct 132 ms 13752 KB Output is correct
7 Correct 449 ms 15216 KB Output is correct
8 Correct 540 ms 15128 KB Output is correct
9 Correct 489 ms 15028 KB Output is correct
10 Correct 126 ms 14288 KB Output is correct
11 Correct 787 ms 14332 KB Output is correct
12 Correct 62 ms 13472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1153 ms 12980 KB Output is correct
2 Correct 1140 ms 15224 KB Output is correct
3 Correct 1147 ms 15220 KB Output is correct
4 Correct 860 ms 14520 KB Output is correct
5 Correct 185 ms 14452 KB Output is correct
6 Correct 132 ms 13752 KB Output is correct
7 Correct 449 ms 15216 KB Output is correct
8 Correct 540 ms 15128 KB Output is correct
9 Correct 489 ms 15028 KB Output is correct
10 Correct 126 ms 14288 KB Output is correct
11 Correct 787 ms 14332 KB Output is correct
12 Correct 62 ms 13472 KB Output is correct
13 Correct 1155 ms 15652 KB Output is correct
14 Correct 1145 ms 15600 KB Output is correct
15 Correct 1114 ms 15276 KB Output is correct
16 Correct 829 ms 14928 KB Output is correct
17 Correct 207 ms 14972 KB Output is correct
18 Correct 133 ms 13732 KB Output is correct
19 Correct 1203 ms 15680 KB Output is correct
20 Correct 1144 ms 15632 KB Output is correct
21 Correct 733 ms 15636 KB Output is correct
22 Correct 156 ms 14300 KB Output is correct
23 Correct 841 ms 14332 KB Output is correct
24 Correct 101 ms 13484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 316 KB Output is correct
2 Correct 0 ms 312 KB Output is correct
3 Correct 0 ms 312 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 9 ms 828 KB Output is correct
8 Correct 9 ms 844 KB Output is correct
9 Correct 9 ms 836 KB Output is correct
10 Correct 9 ms 780 KB Output is correct
11 Correct 6 ms 716 KB Output is correct
12 Correct 5 ms 712 KB Output is correct
13 Correct 8 ms 820 KB Output is correct
14 Correct 8 ms 816 KB Output is correct
15 Correct 9 ms 820 KB Output is correct
16 Correct 4 ms 716 KB Output is correct
17 Correct 7 ms 716 KB Output is correct
18 Correct 2 ms 716 KB Output is correct
19 Correct 1153 ms 12980 KB Output is correct
20 Correct 1140 ms 15224 KB Output is correct
21 Correct 1147 ms 15220 KB Output is correct
22 Correct 860 ms 14520 KB Output is correct
23 Correct 185 ms 14452 KB Output is correct
24 Correct 132 ms 13752 KB Output is correct
25 Correct 449 ms 15216 KB Output is correct
26 Correct 540 ms 15128 KB Output is correct
27 Correct 489 ms 15028 KB Output is correct
28 Correct 126 ms 14288 KB Output is correct
29 Correct 787 ms 14332 KB Output is correct
30 Correct 62 ms 13472 KB Output is correct
31 Correct 1155 ms 15652 KB Output is correct
32 Correct 1145 ms 15600 KB Output is correct
33 Correct 1114 ms 15276 KB Output is correct
34 Correct 829 ms 14928 KB Output is correct
35 Correct 207 ms 14972 KB Output is correct
36 Correct 133 ms 13732 KB Output is correct
37 Correct 1203 ms 15680 KB Output is correct
38 Correct 1144 ms 15632 KB Output is correct
39 Correct 733 ms 15636 KB Output is correct
40 Correct 156 ms 14300 KB Output is correct
41 Correct 841 ms 14332 KB Output is correct
42 Correct 101 ms 13484 KB Output is correct
43 Correct 1236 ms 17604 KB Output is correct
44 Correct 1259 ms 17608 KB Output is correct
45 Correct 1241 ms 17556 KB Output is correct
46 Correct 841 ms 16192 KB Output is correct
47 Correct 203 ms 16128 KB Output is correct
48 Correct 128 ms 13548 KB Output is correct
49 Correct 496 ms 17568 KB Output is correct
50 Correct 574 ms 17600 KB Output is correct
51 Correct 501 ms 17364 KB Output is correct
52 Correct 172 ms 15856 KB Output is correct
53 Correct 773 ms 15124 KB Output is correct