Submission #295918

# Submission time Handle Problem Language Result Execution time Memory
295918 2020-09-10T06:08:42 Z Hemimor Regions (IOI09_regions) C++14
60 / 100
8000 ms 25344 KB
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <cassert>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P,int> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

class Segment_Tree{
	private:
	int n;
	vi date;
	public:
	Segment_Tree(int n_){
		n=1;
		while(n<n_) n*=2;
		date=vi(2*n-1);
	}
	void Update(int k,int x){
		k+=n-1;
		date[k]+=x;
		while(k>0){
			k=(k-1)/2;
			date[k]=date[k*2+1]+date[k*2+2];
		}
	}
	int Query(int a,int b){
		a+=n-1;b+=n-1;
		int m=0;
		while(a<b){
			if(a%2==0) m+=date[a++];
			if(b%2==0) m+=date[--b];
			a/=2;b/=2;
		}
		return m;
	}
	int Open(int k){return date[k+n-1];}
};

const int M=300;
int n,m,q,id=0;
vvi g,b;
vi a,par,num,lt,rt;
map<P,int> mp;

void dfs(int v){
	lt[v]=id++;
	for(auto u:g[v]) dfs(u);
	rt[v]=id;
}

int main(){
	scanf("%d%d%d",&n,&m,&q);
	Segment_Tree st(n);
	g=vvi(n);
	b=vvi(m);
	a=lt=rt=vi(n);
	par=vi(n,-1);
	num=vi(m);
	for(int i=0;i<n;i++){
		if(i){
			scanf("%d",&par[i]);
			par[i]--;
			g[par[i]].push_back(i);
		}
		scanf("%d",&a[i]);
		a[i]--;
		num[a[i]]++;
		b[a[i]].push_back(i);
	}
	dfs(0);
/*	for(int i=0;i<m;i++) if(a[i]>M){
		
	}*/
	for(int i=0;i<q;i++){
		int x,y,res=0;
		scanf("%d%d",&x,&y);
		x--,y--;
		if(mp.find({x,y})!=mp.end()) res=mp[{x,y}];
		else{
			for(auto j:b[y]) st.Update(lt[j],1);
			for(auto j:b[x]) res+=st.Query(lt[j],rt[j]);
			for(auto j:b[y]) st.Update(lt[j],-1);
		}
		mp[{x,y}]=res;
		printf("%d\n",res);
		fflush(stdout);
	}
}

Compilation message

regions.cpp: In function 'int main()':
regions.cpp:82:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   82 |  scanf("%d%d%d",&n,&m,&q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~
regions.cpp:91:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   91 |    scanf("%d",&par[i]);
      |    ~~~~~^~~~~~~~~~~~~~
regions.cpp:95:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   95 |   scanf("%d",&a[i]);
      |   ~~~~~^~~~~~~~~~~~
regions.cpp:106:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  106 |   scanf("%d%d",&x,&y);
      |   ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 7 ms 512 KB Output is correct
6 Correct 22 ms 512 KB Output is correct
7 Correct 32 ms 760 KB Output is correct
8 Correct 36 ms 632 KB Output is correct
9 Correct 73 ms 1272 KB Output is correct
10 Correct 108 ms 1656 KB Output is correct
11 Correct 175 ms 2264 KB Output is correct
12 Correct 243 ms 2936 KB Output is correct
13 Correct 248 ms 3052 KB Output is correct
14 Correct 438 ms 3576 KB Output is correct
15 Correct 789 ms 6236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3236 ms 9452 KB Output is correct
2 Correct 3040 ms 8420 KB Output is correct
3 Correct 6537 ms 13404 KB Output is correct
4 Correct 581 ms 4512 KB Output is correct
5 Correct 741 ms 6816 KB Output is correct
6 Correct 992 ms 7372 KB Output is correct
7 Correct 1126 ms 8104 KB Output is correct
8 Correct 4790 ms 15584 KB Output is correct
9 Correct 7655 ms 23104 KB Output is correct
10 Execution timed out 8089 ms 24580 KB Time limit exceeded
11 Execution timed out 8084 ms 25344 KB Time limit exceeded
12 Execution timed out 8048 ms 18332 KB Time limit exceeded
13 Execution timed out 8026 ms 18400 KB Time limit exceeded
14 Execution timed out 8064 ms 18488 KB Time limit exceeded
15 Execution timed out 8076 ms 22032 KB Time limit exceeded
16 Execution timed out 8057 ms 25212 KB Time limit exceeded
17 Execution timed out 8093 ms 24876 KB Time limit exceeded